Code Duplication    Length = 34-47 lines in 2 locations

src/Form/Field/HasMany.php 2 locations

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