Code Duplication    Length = 34-47 lines in 2 locations

src/Form/Field/HasMany.php 2 locations

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