Code Duplication    Length = 34-47 lines in 2 locations

src/Form/Field/HasMany.php 2 locations

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