Code Duplication    Length = 34-47 lines in 2 locations

src/Form/Field/HasMany.php 2 locations

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