Code Duplication    Length = 34-47 lines in 2 locations

src/Form/Field/HasMany.php 2 locations

@@ 474-507 (lines=34) @@
471
     *
472
     * @return void
473
     */
474
    protected function setupScriptForDefaultView($templateScript)
475
    {
476
        $removeClass = NestedForm::REMOVE_FLAG_CLASS;
477
        $defaultKey = NestedForm::DEFAULT_KEY_NAME;
478
479
        /**
480
         * When add a new sub form, replace all element key in new sub form.
481
         *
482
         * @example comments[new___key__][title]  => comments[new_{index}][title]
483
         *
484
         * {count} is increment number of current sub form count.
485
         */
486
        $script = <<<EOT
487
var index = 0;
488
$('#has-many-{$this->column}').on('click', '.add', function() {
489
490
    var tpl = $('template.{$this->column}-tpl');
491
492
    index++;
493
494
    var template = tpl.html().replace(/{$defaultKey}/g, index);
495
    $('.has-many-{$this->column}-forms').append(template);
496
    {$templateScript}
497
});
498
499
$('#has-many-{$this->column}').on('click', '.remove', function() {
500
    $(this).closest('.has-many-{$this->column}-form').hide();
501
    $(this).closest('.has-many-{$this->column}-form').find('.$removeClass').val(1);
502
});
503
504
EOT;
505
506
        Admin::script($script);
507
    }
508
509
    /**
510
     * Setup tab template script.
@@ 516-562 (lines=47) @@
513
     *
514
     * @return void
515
     */
516
    protected function setupScriptForTabView($templateScript)
517
    {
518
        $removeClass = NestedForm::REMOVE_FLAG_CLASS;
519
        $defaultKey = NestedForm::DEFAULT_KEY_NAME;
520
521
        $script = <<<EOT
522
523
$('#has-many-{$this->column} > .nav').off('click', 'i.close-tab').on('click', 'i.close-tab', function(){
524
    var \$navTab = $(this).siblings('a');
525
    var \$pane = $(\$navTab.attr('href'));
526
    if( \$pane.hasClass('new') ){
527
        \$pane.remove();
528
    }else{
529
        \$pane.removeClass('active').find('.$removeClass').val(1);
530
    }
531
    if(\$navTab.closest('li').hasClass('active')){
532
        \$navTab.closest('li').remove();
533
        $('#has-many-{$this->column} > .nav > li:nth-child(1) > a').tab('show');
534
    }else{
535
        \$navTab.closest('li').remove();
536
    }
537
});
538
539
var index = 0;
540
$('#has-many-{$this->column} > .header').off('click', '.add').on('click', '.add', function(){
541
    index++;
542
    var navTabHtml = $('#has-many-{$this->column} > template.nav-tab-tpl').html().replace(/{$defaultKey}/g, index);
543
    var paneHtml = $('#has-many-{$this->column} > template.pane-tpl').html().replace(/{$defaultKey}/g, index);
544
    $('#has-many-{$this->column} > .nav').append(navTabHtml);
545
    $('#has-many-{$this->column} > .tab-content').append(paneHtml);
546
    $('#has-many-{$this->column} > .nav > li:last-child a').tab('show');
547
    {$templateScript}
548
});
549
550
if ($('.has-error').length) {
551
    $('.has-error').parent('.tab-pane').each(function() {
552
        var tabId = '#'+$(this).attr('id');
553
        $('li a[href="'+tabId+'"] i').removeClass('hide');
554
    });
555
    
556
    var first = $('.has-error:first').parent().attr('id');
557
    $('li a[href="#'+first+'"]').tab('show');
558
}
559
EOT;
560
561
        Admin::script($script);
562
    }
563
564
    /**
565
     * Disable create button.