Code Duplication    Length = 34-47 lines in 2 locations

src/Form/Field/HasMany.php 2 locations

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