Code Duplication    Length = 34-47 lines in 2 locations

src/Form/Field/HasMany.php 2 locations

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