Code Duplication    Length = 34-47 lines in 2 locations

src/Form/Field/HasMany.php 2 locations

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