Code Duplication    Length = 34-47 lines in 2 locations

src/Form/Field/HasMany.php 2 locations

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