Code Duplication    Length = 34-47 lines in 2 locations

src/Form/Field/HasMany.php 2 locations

@@ 437-470 (lines=34) @@
434
     *
435
     * @return void
436
     */
437
    protected function setupScriptForDefaultView($templateScript)
438
    {
439
        $removeClass = NestedForm::REMOVE_FLAG_CLASS;
440
        $defaultKey = NestedForm::DEFAULT_KEY_NAME;
441
442
        /**
443
         * When add a new sub form, replace all element key in new sub form.
444
         *
445
         * @example comments[new___key__][title]  => comments[new_{index}][title]
446
         *
447
         * {count} is increment number of current sub form count.
448
         */
449
        $script = <<<EOT
450
var index = 0;
451
$('#has-many-{$this->column}').on('click', '.add', function() {
452
453
    var tpl = $('template.{$this->column}-tpl');
454
455
    index++;
456
457
    var template = tpl.html().replace(/{$defaultKey}/g, index);
458
    $('.has-many-{$this->column}-forms').append(template);
459
    {$templateScript}
460
});
461
462
$('#has-many-{$this->column}').on('click', '.remove', function() {
463
    $(this).closest('.has-many-{$this->column}-form').hide();
464
    $(this).closest('.has-many-{$this->column}-form').find('.$removeClass').val(1);
465
});
466
467
EOT;
468
469
        Admin::script($script);
470
    }
471
472
    /**
473
     * Setup tab template script.
@@ 479-525 (lines=47) @@
476
     *
477
     * @return void
478
     */
479
    protected function setupScriptForTabView($templateScript)
480
    {
481
        $removeClass = NestedForm::REMOVE_FLAG_CLASS;
482
        $defaultKey = NestedForm::DEFAULT_KEY_NAME;
483
484
        $script = <<<EOT
485
486
$('#has-many-{$this->column} > .nav').off('click', 'i.close-tab').on('click', 'i.close-tab', function(){
487
    var \$navTab = $(this).siblings('a');
488
    var \$pane = $(\$navTab.attr('href'));
489
    if( \$pane.hasClass('new') ){
490
        \$pane.remove();
491
    }else{
492
        \$pane.removeClass('active').find('.$removeClass').val(1);
493
    }
494
    if(\$navTab.closest('li').hasClass('active')){
495
        \$navTab.closest('li').remove();
496
        $('#has-many-{$this->column} > .nav > li:nth-child(1) > a').tab('show');
497
    }else{
498
        \$navTab.closest('li').remove();
499
    }
500
});
501
502
var index = 0;
503
$('#has-many-{$this->column} > .header').off('click', '.add').on('click', '.add', function(){
504
    index++;
505
    var navTabHtml = $('#has-many-{$this->column} > template.nav-tab-tpl').html().replace(/{$defaultKey}/g, index);
506
    var paneHtml = $('#has-many-{$this->column} > template.pane-tpl').html().replace(/{$defaultKey}/g, index);
507
    $('#has-many-{$this->column} > .nav').append(navTabHtml);
508
    $('#has-many-{$this->column} > .tab-content').append(paneHtml);
509
    $('#has-many-{$this->column} > .nav > li:last-child a').tab('show');
510
    {$templateScript}
511
});
512
513
if ($('.has-error').length) {
514
    $('.has-error').parent('.tab-pane').each(function() {
515
        var tabId = '#'+$(this).attr('id');
516
        $('li a[href="'+tabId+'"] i').removeClass('hide');
517
    });
518
    
519
    var first = $('.has-error:first').parent().attr('id');
520
    $('li a[href="#'+first+'"]').tab('show');
521
}
522
EOT;
523
524
        Admin::script($script);
525
    }
526
527
    /**
528
     * Disable create button.