Skip to content

Commit c63151f

Browse files
author
Eugene Tupikov
committed
Revert "move collecting js expressions to a separate class"
This reverts commit 48ce3a8.
1 parent 2c713e4 commit c63151f

File tree

1 file changed

+43
-15
lines changed

1 file changed

+43
-15
lines changed

src/renderers/BaseRenderer.php

+43-15
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
use unclead\multipleinput\TabularInput;
2525
use unclead\multipleinput\assets\MultipleInputAsset;
2626
use unclead\multipleinput\assets\MultipleInputSortableAsset;
27-
use unclead\multipleinput\components\JsCollector;
2827
use unclead\multipleinput\components\BaseColumn;
2928

3029
/**
@@ -356,35 +355,64 @@ protected function getExtraButtons($index, $item)
356355
return $content;
357356
}
358357

359-
/**
360-
* @return mixed
361-
*
362-
* @throws InvalidConfigException
363-
* @throws NotSupportedException
364-
*/
365358
public function render()
366359
{
367360
$this->initColumns();
368361

369362
$view = $this->context->getView();
370363
MultipleInputAsset::register($view);
371364

372-
$jsCollector = new JsCollector($view);
365+
// Collect all js scripts which were added before rendering of our widget
366+
$jsBefore= [];
367+
if (is_array($view->js)) {
368+
foreach ($view->js as $position => $scripts) {
369+
foreach ((array)$scripts as $key => $js) {
370+
if (!isset($jsBefore[$position])) {
371+
$jsBefore[$position] = [];
372+
}
373+
$jsBefore[$position][$key] = $js;
374+
}
375+
}
376+
}
373377

374-
$jsCollector->onBeforeRender();
378+
$content = $this->internalRender();
375379

376-
$content = $this->internalRender();
377-
$jsCollector->onAfterRender();
380+
// Collect all js scripts which has to be appended to page before initialization widget
381+
$jsInit = [];
382+
if (is_array($view->js)) {
383+
foreach ($this->jsPositions as $position) {
384+
foreach (ArrayHelper::getValue($view->js, $position, []) as $key => $js) {
385+
if (isset($jsBefore[$position][$key])) {
386+
continue;
387+
}
388+
$jsInit[$key] = $js;
389+
$jsBefore[$position][$key] = $js;
390+
unset($view->js[$position][$key]);
391+
}
392+
}
393+
}
378394

379395
$template = $this->prepareTemplate();
380-
$jsCollector->onAfterPrepareTemplate();
396+
397+
$jsTemplates = [];
398+
if (is_array($view->js)) {
399+
foreach ($this->jsPositions as $position) {
400+
foreach (ArrayHelper::getValue($view->js, $position, []) as $key => $js) {
401+
if (isset($jsBefore[$position][$key])) {
402+
continue;
403+
}
404+
$jsTemplates[$key] = $js;
405+
unset($view->js[$position][$key]);
406+
}
407+
}
408+
}
381409

382410
$options = Json::encode(array_merge([
383411
'id' => $this->id,
384412
'inputId' => $this->context->options['id'],
385413
'template' => $template,
386-
'jsInit' => $jsCollector->getJsInit(),
387-
'jsTemplates' => $jsCollector->getJsTemplates(),
414+
'jsInit' => $jsInit,
415+
'jsTemplates' => $jsTemplates,
388416
'max' => $this->max,
389417
'min' => $this->min,
390418
'attributes' => $this->prepareJsAttributes(),
@@ -416,7 +444,7 @@ private function registerJsSortable()
416444
/**
417445
* Returns an array of JQuery sortable plugin options.
418446
* You can override this method extend plugin behaviour.
419-
*
447+
*
420448
* @return array
421449
*/
422450
protected function getJsSortableOptions()

0 commit comments

Comments
 (0)