Code Duplication    Length = 34-47 lines in 2 locations

src/Form/Field/HasMany.php 2 locations

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