Code Duplication    Length = 34-47 lines in 2 locations

src/Form/Field/HasMany.php 2 locations

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