Code Duplication    Length = 34-47 lines in 2 locations

src/Form/Field/HasMany.php 2 locations

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