Code Duplication    Length = 34-47 lines in 2 locations

src/Form/Field/HasMany.php 2 locations

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