Skip to content

Commit 48ce3a8

Browse files
author
Eugene Tupikov
committed
move collecting js expressions to a separate class
1 parent 627c18d commit 48ce3a8

File tree

2 files changed

+128
-43
lines changed

2 files changed

+128
-43
lines changed

src/components/JsCollector.php

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
namespace unclead\multipleinput\components;
4+
5+
use yii\helpers\ArrayHelper;
6+
use yii\web\View;
7+
8+
class JsCollector
9+
{
10+
const POSITIONS_ORDER = [View::POS_HEAD, View::POS_BEGIN, View::POS_END, View::POS_READY, View::POS_LOAD];
11+
12+
/**
13+
* @var View
14+
*/
15+
private $view;
16+
17+
/**
18+
* @var array
19+
*/
20+
private $jsExclude = [];
21+
22+
/**
23+
* @var array
24+
*/
25+
private $jsInit = [];
26+
27+
/**
28+
* @var array
29+
*/
30+
private $jsTemplates = [];
31+
32+
public function __construct($view)
33+
{
34+
$this->view = $view;
35+
}
36+
37+
public function onBeforeRender()
38+
{
39+
if (!is_array($this->view->js)) {
40+
return;
41+
}
42+
43+
$this->jsExclude = [];
44+
foreach ($this->view->js as $position => $scripts) {
45+
foreach ((array)$scripts as $key => $js) {
46+
if (!isset($this->jsExclude[$position])) {
47+
$this->jsExclude[$position] = [];
48+
}
49+
50+
$this->jsExclude[$position][$key] = $js;
51+
}
52+
}
53+
}
54+
55+
public function onAfterRender()
56+
{
57+
if (!is_array($this->view->js)) {
58+
return;
59+
}
60+
61+
foreach (self::POSITIONS_ORDER as $position) {
62+
foreach (ArrayHelper::getValue($this->view->js, $position, []) as $key => $js) {
63+
if (isset($this->jsExclude[$position][$key])) {
64+
continue;
65+
}
66+
67+
$this->jsExclude[$position][$key] = $js;
68+
69+
$this->jsInit[$key] = $js;
70+
71+
unset($this->view->js[$position][$key]);
72+
}
73+
}
74+
}
75+
76+
public function onAfterPrepareTemplate()
77+
{
78+
if (!is_array($this->view->js)) {
79+
return;
80+
}
81+
82+
$this->jsTemplates = [];
83+
84+
foreach (self::POSITIONS_ORDER as $position) {
85+
foreach (ArrayHelper::getValue($this->view->js, $position, []) as $key => $js) {
86+
if (isset($this->jsExclude[$position][$key])) {
87+
continue;
88+
}
89+
90+
$this->jsTemplates[$key] = $js;
91+
92+
unset($this->view->js[$position][$key]);
93+
}
94+
}
95+
96+
}
97+
98+
/**
99+
* @return array
100+
*/
101+
public function getJsInit()
102+
{
103+
return $this->jsInit;
104+
}
105+
106+
/**
107+
* @return array
108+
*/
109+
public function getJsTemplates()
110+
{
111+
return $this->jsTemplates;
112+
}
113+
}

src/renderers/BaseRenderer.php

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use unclead\multipleinput\TabularInput;
2525
use unclead\multipleinput\assets\MultipleInputAsset;
2626
use unclead\multipleinput\assets\MultipleInputSortableAsset;
27+
use unclead\multipleinput\components\JsCollector;
2728
use unclead\multipleinput\components\BaseColumn;
2829

2930
/**
@@ -355,64 +356,35 @@ protected function getExtraButtons($index, $item)
355356
return $content;
356357
}
357358

359+
/**
360+
* @return mixed
361+
*
362+
* @throws InvalidConfigException
363+
* @throws NotSupportedException
364+
*/
358365
public function render()
359366
{
360367
$this->initColumns();
361368

362369
$view = $this->context->getView();
363370
MultipleInputAsset::register($view);
364371

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-
}
372+
$jsCollector = new JsCollector($view);
377373

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

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-
}
376+
$content = $this->internalRender();
377+
$jsCollector->onAfterRender();
394378

395379
$template = $this->prepareTemplate();
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-
}
380+
$jsCollector->onAfterPrepareTemplate();
409381

410382
$options = Json::encode(array_merge([
411383
'id' => $this->id,
412384
'inputId' => $this->context->options['id'],
413385
'template' => $template,
414-
'jsInit' => $jsInit,
415-
'jsTemplates' => $jsTemplates,
386+
'jsInit' => $jsCollector->getJsInit(),
387+
'jsTemplates' => $jsCollector->getJsTemplates(),
416388
'max' => $this->max,
417389
'min' => $this->min,
418390
'attributes' => $this->prepareJsAttributes(),
@@ -444,7 +416,7 @@ private function registerJsSortable()
444416
/**
445417
* Returns an array of JQuery sortable plugin options.
446418
* You can override this method extend plugin behaviour.
447-
*
419+
*
448420
* @return array
449421
*/
450422
protected function getJsSortableOptions()

0 commit comments

Comments
 (0)