Code Duplication    Length = 34-47 lines in 2 locations

src/Form/Field/HasMany.php 2 locations

@@ 473-506 (lines=34) @@
470
     *
471
     * @return void
472
     */
473
    protected function setupScriptForDefaultView($templateScript)
474
    {
475
        $removeClass = NestedForm::REMOVE_FLAG_CLASS;
476
        $defaultKey = NestedForm::DEFAULT_KEY_NAME;
477
478
        /**
479
         * When add a new sub form, replace all element key in new sub form.
480
         *
481
         * @example comments[new___key__][title]  => comments[new_{index}][title]
482
         *
483
         * {count} is increment number of current sub form count.
484
         */
485
        $script = <<<EOT
486
var index = 0;
487
$('#has-many-{$this->column}').on('click', '.add', function() {
488
489
    var tpl = $('template.{$this->column}-tpl');
490
491
    index++;
492
493
    var template = tpl.html().replace(/{$defaultKey}/g, index);
494
    $('.has-many-{$this->column}-forms').append(template);
495
    {$templateScript}
496
});
497
498
$('#has-many-{$this->column}').on('click', '.remove', function() {
499
    $(this).closest('.has-many-{$this->column}-form').hide();
500
    $(this).closest('.has-many-{$this->column}-form').find('.$removeClass').val(1);
501
});
502
503
EOT;
504
505
        Admin::script($script);
506
    }
507
508
    /**
509
     * Setup tab template script.
@@ 515-561 (lines=47) @@
512
     *
513
     * @return void
514
     */
515
    protected function setupScriptForTabView($templateScript)
516
    {
517
        $removeClass = NestedForm::REMOVE_FLAG_CLASS;
518
        $defaultKey = NestedForm::DEFAULT_KEY_NAME;
519
520
        $script = <<<EOT
521
522
$('#has-many-{$this->column} > .nav').off('click', 'i.close-tab').on('click', 'i.close-tab', function(){
523
    var \$navTab = $(this).siblings('a');
524
    var \$pane = $(\$navTab.attr('href'));
525
    if( \$pane.hasClass('new') ){
526
        \$pane.remove();
527
    }else{
528
        \$pane.removeClass('active').find('.$removeClass').val(1);
529
    }
530
    if(\$navTab.closest('li').hasClass('active')){
531
        \$navTab.closest('li').remove();
532
        $('#has-many-{$this->column} > .nav > li:nth-child(1) > a').tab('show');
533
    }else{
534
        \$navTab.closest('li').remove();
535
    }
536
});
537
538
var index = 0;
539
$('#has-many-{$this->column} > .header').off('click', '.add').on('click', '.add', function(){
540
    index++;
541
    var navTabHtml = $('#has-many-{$this->column} > template.nav-tab-tpl').html().replace(/{$defaultKey}/g, index);
542
    var paneHtml = $('#has-many-{$this->column} > template.pane-tpl').html().replace(/{$defaultKey}/g, index);
543
    $('#has-many-{$this->column} > .nav').append(navTabHtml);
544
    $('#has-many-{$this->column} > .tab-content').append(paneHtml);
545
    $('#has-many-{$this->column} > .nav > li:last-child a').tab('show');
546
    {$templateScript}
547
});
548
549
if ($('.has-error').length) {
550
    $('.has-error').parent('.tab-pane').each(function() {
551
        var tabId = '#'+$(this).attr('id');
552
        $('li a[href="'+tabId+'"] i').removeClass('hide');
553
    });
554
    
555
    var first = $('.has-error:first').parent().attr('id');
556
    $('li a[href="#'+first+'"]').tab('show');
557
}
558
EOT;
559
560
        Admin::script($script);
561
    }
562
563
    /**
564
     * Disable create button.