@@ -339,7 +339,7 @@ public function render()
339
339
$ jsBefore = [];
340
340
if (is_array ($ view ->js )) {
341
341
foreach ($ view ->js as $ position => $ scripts ) {
342
- foreach ($ scripts as $ key => $ js ) {
342
+ foreach (( array ) $ scripts as $ key => $ js ) {
343
343
if (!isset ($ jsBefore [$ position ])) {
344
344
$ jsBefore [$ position ] = [];
345
345
}
@@ -354,7 +354,7 @@ public function render()
354
354
$ jsInit = [];
355
355
if (is_array ($ view ->js )) {
356
356
foreach ($ view ->js as $ position => $ scripts ) {
357
- foreach ($ scripts as $ key => $ js ) {
357
+ foreach (( array ) $ scripts as $ key => $ js ) {
358
358
if (isset ($ jsBefore [$ position ][$ key ])) {
359
359
continue ;
360
360
}
@@ -368,14 +368,15 @@ public function render()
368
368
$ template = $ this ->prepareTemplate ();
369
369
370
370
$ jsTemplates = [];
371
- if (is_array ($ view ->js ) && isset ($ view ->js [View::POS_READY ])) {
372
- foreach ($ view ->js [View::POS_READY ] as $ key => $ js ) {
373
- if (isset ($ jsBefore [View::POS_READY ][$ key ])) {
374
- continue ;
371
+ if (is_array ($ view ->js )) {
372
+ foreach ($ view ->js as $ position => $ scripts ) {
373
+ foreach ((array )$ scripts as $ key => $ js ) {
374
+ if (isset ($ jsBefore [$ position ][$ key ])) {
375
+ continue ;
376
+ }
377
+ $ jsTemplates [$ key ] = $ js ;
378
+ unset($ view ->js [$ position ][$ key ]);
375
379
}
376
-
377
- $ jsTemplates [$ key ] = $ js ;
378
- unset($ view ->js [View::POS_READY ][$ key ]);
379
380
}
380
381
}
381
382
@@ -392,32 +393,41 @@ public function render()
392
393
]);
393
394
394
395
$ js = "jQuery('# {$ this ->id }').multipleInput( $ options); " ;
396
+ $ view ->registerJs ($ js );
395
397
396
- if ($ this ->sortable ) {
397
- MultipleInputSortableAsset::register ($ view );
398
- $ js .= <<<JS
399
- $('# {$ this ->id } table').sorting({
400
- containerSelector: 'table',
401
- itemPath: '> tbody',
402
- itemSelector: 'tr',
403
- placeholder: '<tr class="placeholder">',
404
- handle:'.drag-handle',
405
- onDrop: function(item, container, _super, event) {
406
- _super(item, container, _super, event);
407
-
408
- var wrapper = item.closest('.multiple-input').first();
409
- event = $.Event('afterDropRow');
410
- wrapper.trigger(event, [item]);
411
- }
412
- });
413
- JS ;
398
+ if ($ this ->sortable ) {
399
+ $ this ->registerJsSortable ();
414
400
}
415
401
416
- $ view ->registerJs ($ js );
417
-
418
402
return $ content ;
419
403
}
420
404
405
+ private function registerJsSortable ()
406
+ {
407
+ $ view = $ this ->context ->getView ();
408
+ MultipleInputSortableAsset::register ($ view );
409
+
410
+ // todo override when ListRenderer will use div markup
411
+ $ options = Json::encode ([
412
+ 'containerSelector ' => 'table ' ,
413
+ 'itemPath ' => '> tbody ' ,
414
+ 'itemSelector ' => 'tr ' ,
415
+ 'placeholder ' => '<tr class="placeholder"> ' ,
416
+ 'handle ' => '.drag-handle ' ,
417
+ 'onDrop ' => new \yii \web \JsExpression ("
418
+ function(item, container, _super, event) {
419
+ _super(item, container, _super, event);
420
+
421
+ var wrapper = item.closest('.multiple-input').first();
422
+ event = $.Event('afterDropRow');
423
+ wrapper.trigger(event, [item]);
424
+ }
425
+ " )
426
+ ]);
427
+ $ js = "$('# {$ this ->id } table').sorting( $ options); " ;
428
+ $ view ->registerJs ($ js );
429
+ }
430
+
421
431
/**
422
432
* @return mixed
423
433
* @throws NotSupportedException
0 commit comments