Code Duplication    Length = 34-47 lines in 2 locations

src/Form/Field/HasMany.php 2 locations

@@ 397-430 (lines=34) @@
394
     *
395
     * @return void
396
     */
397
    protected function setupScriptForDefaultView($templateScript)
398
    {
399
        $removeClass = NestedForm::REMOVE_FLAG_CLASS;
400
        $defaultKey = NestedForm::DEFAULT_KEY_NAME;
401
402
        /**
403
         * When add a new sub form, replace all element key in new sub form.
404
         *
405
         * @example comments[new___key__][title]  => comments[new_{index}][title]
406
         *
407
         * {count} is increment number of current sub form count.
408
         */
409
        $script = <<<EOT
410
var index = 0;
411
$('#has-many-{$this->column}').on('click', '.add', function () {
412
413
    var tpl = $('template.{$this->column}-tpl');
414
415
    index++;
416
417
    var template = tpl.html().replace(/{$defaultKey}/g, index);
418
    $('.has-many-{$this->column}-forms').append(template);
419
    {$templateScript}
420
});
421
422
$('#has-many-{$this->column}').on('click', '.remove', function () {
423
    $(this).closest('.has-many-{$this->column}-form').hide();
424
    $(this).closest('.has-many-{$this->column}-form').find('.$removeClass').val(1);
425
});
426
427
EOT;
428
429
        Admin::script($script);
430
    }
431
432
    /**
433
     * Setup tab template script.
@@ 439-485 (lines=47) @@
436
     *
437
     * @return void
438
     */
439
    protected function setupScriptForTabView($templateScript)
440
    {
441
        $removeClass = NestedForm::REMOVE_FLAG_CLASS;
442
        $defaultKey = NestedForm::DEFAULT_KEY_NAME;
443
444
        $script = <<<EOT
445
446
$('#has-many-{$this->column} > .nav').off('click', 'i.close-tab').on('click', 'i.close-tab', function(){
447
    var \$navTab = $(this).siblings('a');
448
    var \$pane = $(\$navTab.attr('href'));
449
    if( \$pane.hasClass('new') ){
450
        \$pane.remove();
451
    }else{
452
        \$pane.removeClass('active').find('.$removeClass').val(1);
453
    }
454
    if(\$navTab.closest('li').hasClass('active')){
455
        \$navTab.closest('li').remove();
456
        $('#has-many-{$this->column} > .nav > li:nth-child(1) > a').tab('show');
457
    }else{
458
        \$navTab.closest('li').remove();
459
    }
460
});
461
462
var index = 0;
463
$('#has-many-{$this->column} > .header').off('click', '.add').on('click', '.add', function(){
464
    index++;
465
    var navTabHtml = $('#has-many-{$this->column} > template.nav-tab-tpl').html().replace(/{$defaultKey}/g, index);
466
    var paneHtml = $('#has-many-{$this->column} > template.pane-tpl').html().replace(/{$defaultKey}/g, index);
467
    $('#has-many-{$this->column} > .nav').append(navTabHtml);
468
    $('#has-many-{$this->column} > .tab-content').append(paneHtml);
469
    $('#has-many-{$this->column} > .nav > li:last-child a').tab('show');
470
    {$templateScript}
471
});
472
473
if ($('.has-error').length) {
474
    $('.has-error').parent('.tab-pane').each(function () {
475
        var tabId = '#'+$(this).attr('id');
476
        $('li a[href="'+tabId+'"] i').removeClass('hide');
477
    });
478
    
479
    var first = $('.has-error:first').parent().attr('id');
480
    $('li a[href="#'+first+'"]').tab('show');
481
}
482
EOT;
483
484
        Admin::script($script);
485
    }
486
487
    /**
488
     * Render the `HasMany` field.