Code Duplication    Length = 34-47 lines in 2 locations

src/Form/Field/HasMany.php 2 locations

@@ 531-564 (lines=34) @@
528
     *
529
     * @return void
530
     */
531
    protected function setupScriptForDefaultView($templateScript)
532
    {
533
        $removeClass = NestedForm::REMOVE_FLAG_CLASS;
534
        $defaultKey = NestedForm::DEFAULT_KEY_NAME;
535
536
        /**
537
         * When add a new sub form, replace all element key in new sub form.
538
         *
539
         * @example comments[new___key__][title]  => comments[new_{index}][title]
540
         *
541
         * {count} is increment number of current sub form count.
542
         */
543
        $script = <<<EOT
544
var index = 0;
545
$('#has-many-{$this->column}').on('click', '.add', function () {
546
547
    var tpl = $('template.{$this->column}-tpl');
548
549
    index++;
550
551
    var template = tpl.html().replace(/{$defaultKey}/g, index);
552
    $('.has-many-{$this->column}-forms').append(template);
553
    {$templateScript}
554
});
555
556
$('#has-many-{$this->column}').on('click', '.remove', function () {
557
    $(this).closest('.has-many-{$this->column}-form').hide();
558
    $(this).closest('.has-many-{$this->column}-form').find('.$removeClass').val(1);
559
});
560
561
EOT;
562
563
        Admin::script($script);
564
    }
565
566
    /**
567
     * Setup tab template script.
@@ 573-619 (lines=47) @@
570
     *
571
     * @return void
572
     */
573
    protected function setupScriptForTabView($templateScript)
574
    {
575
        $removeClass = NestedForm::REMOVE_FLAG_CLASS;
576
        $defaultKey = NestedForm::DEFAULT_KEY_NAME;
577
578
        $script = <<<EOT
579
580
$('#has-many-{$this->column} > .nav').off('click', 'i.close-tab').on('click', 'i.close-tab', function(){
581
    var \$navTab = $(this).siblings('a');
582
    var \$pane = $(\$navTab.attr('href'));
583
    if( \$pane.hasClass('new') ){
584
        \$pane.remove();
585
    }else{
586
        \$pane.removeClass('active').find('.$removeClass').val(1);
587
    }
588
    if(\$navTab.closest('li').hasClass('active')){
589
        \$navTab.closest('li').remove();
590
        $('#has-many-{$this->column} > .nav > li:nth-child(1) > a').tab('show');
591
    }else{
592
        \$navTab.closest('li').remove();
593
    }
594
});
595
596
var index = 0;
597
$('#has-many-{$this->column} > .header').off('click', '.add').on('click', '.add', function(){
598
    index++;
599
    var navTabHtml = $('#has-many-{$this->column} > template.nav-tab-tpl').html().replace(/{$defaultKey}/g, index);
600
    var paneHtml = $('#has-many-{$this->column} > template.pane-tpl').html().replace(/{$defaultKey}/g, index);
601
    $('#has-many-{$this->column} > .nav').append(navTabHtml);
602
    $('#has-many-{$this->column} > .tab-content').append(paneHtml);
603
    $('#has-many-{$this->column} > .nav > li:last-child a').tab('show');
604
    {$templateScript}
605
});
606
607
if ($('.has-error').length) {
608
    $('.has-error').parent('.tab-pane').each(function () {
609
        var tabId = '#'+$(this).attr('id');
610
        $('li a[href="'+tabId+'"] i').removeClass('hide');
611
    });
612
    
613
    var first = $('.has-error:first').parent().attr('id');
614
    $('li a[href="#'+first+'"]').tab('show');
615
}
616
EOT;
617
618
        Admin::script($script);
619
    }
620
621
    /**
622
     * Disable create button.