Code Duplication    Length = 34-47 lines in 2 locations

src/Form/Field/HasMany.php 2 locations

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