Code Duplication    Length = 34-47 lines in 2 locations

src/Form/Field/HasMany.php 2 locations

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