Code Duplication    Length = 34-47 lines in 3 locations

src/Form/Field/HasMany.php 3 locations

@@ 433-466 (lines=34) @@
430
     *
431
     * @return void
432
     */
433
    protected function setupScriptForDefaultView($templateScript)
434
    {
435
        $removeClass = NestedForm::REMOVE_FLAG_CLASS;
436
        $defaultKey = NestedForm::DEFAULT_KEY_NAME;
437
438
        /**
439
         * When add a new sub form, replace all element key in new sub form.
440
         *
441
         * @example comments[new___key__][title]  => comments[new_{index}][title]
442
         *
443
         * {count} is increment number of current sub form count.
444
         */
445
        $script = <<<EOT
446
var index = 0;
447
$('#has-many-{$this->column}').on('click', '.add', function () {
448
449
    var tpl = $('template.{$this->column}-tpl');
450
451
    index++;
452
453
    var template = tpl.html().replace(/{$defaultKey}/g, index);
454
    $('.has-many-{$this->column}-forms').append(template);
455
    {$templateScript}
456
});
457
458
$('#has-many-{$this->column}').on('click', '.remove', function () {
459
    $(this).closest('.has-many-{$this->column}-form').hide();
460
    $(this).closest('.has-many-{$this->column}-form').find('.$removeClass').val(1);
461
});
462
463
EOT;
464
465
        Admin::script($script);
466
    }
467
468
    /**
469
     * Setup tab template script.
@@ 475-521 (lines=47) @@
472
     *
473
     * @return void
474
     */
475
    protected function setupScriptForTabView($templateScript)
476
    {
477
        $removeClass = NestedForm::REMOVE_FLAG_CLASS;
478
        $defaultKey = NestedForm::DEFAULT_KEY_NAME;
479
480
        $script = <<<EOT
481
482
$('#has-many-{$this->column} > .nav').off('click', 'i.close-tab').on('click', 'i.close-tab', function(){
483
    var \$navTab = $(this).siblings('a');
484
    var \$pane = $(\$navTab.attr('href'));
485
    if( \$pane.hasClass('new') ){
486
        \$pane.remove();
487
    }else{
488
        \$pane.removeClass('active').find('.$removeClass').val(1);
489
    }
490
    if(\$navTab.closest('li').hasClass('active')){
491
        \$navTab.closest('li').remove();
492
        $('#has-many-{$this->column} > .nav > li:nth-child(1) > a').tab('show');
493
    }else{
494
        \$navTab.closest('li').remove();
495
    }
496
});
497
498
var index = 0;
499
$('#has-many-{$this->column} > .header').off('click', '.add').on('click', '.add', function(){
500
    index++;
501
    var navTabHtml = $('#has-many-{$this->column} > template.nav-tab-tpl').html().replace(/{$defaultKey}/g, index);
502
    var paneHtml = $('#has-many-{$this->column} > template.pane-tpl').html().replace(/{$defaultKey}/g, index);
503
    $('#has-many-{$this->column} > .nav').append(navTabHtml);
504
    $('#has-many-{$this->column} > .tab-content').append(paneHtml);
505
    $('#has-many-{$this->column} > .nav > li:last-child a').tab('show');
506
    {$templateScript}
507
});
508
509
if ($('.has-error').length) {
510
    $('.has-error').parent('.tab-pane').each(function () {
511
        var tabId = '#'+$(this).attr('id');
512
        $('li a[href="'+tabId+'"] i').removeClass('hide');
513
    });
514
    
515
    var first = $('.has-error:first').parent().attr('id');
516
    $('li a[href="#'+first+'"]').tab('show');
517
}
518
EOT;
519
520
        Admin::script($script);
521
    }
522
523
    /**
524
     * Setup default template script.
@@ 530-563 (lines=34) @@
527
     *
528
     * @return void
529
     */
530
    protected function setupScriptForTableView($templateScript)
531
    {
532
        $removeClass = NestedForm::REMOVE_FLAG_CLASS;
533
        $defaultKey = NestedForm::DEFAULT_KEY_NAME;
534
535
        /**
536
         * When add a new sub form, replace all element key in new sub form.
537
         *
538
         * @example comments[new___key__][title]  => comments[new_{index}][title]
539
         *
540
         * {count} is increment number of current sub form count.
541
         */
542
        $script = <<<EOT
543
var index = 0;
544
$('#has-many-{$this->column}').on('click', '.add', function () {
545
546
    var tpl = $('template.{$this->column}-tpl');
547
548
    index++;
549
550
    var template = tpl.html().replace(/{$defaultKey}/g, index);
551
    $('.has-many-{$this->column}-forms').append(template);
552
    {$templateScript}
553
});
554
555
$('#has-many-{$this->column}').on('click', '.remove', function () {
556
    $(this).closest('.has-many-{$this->column}-form').hide();
557
    $(this).closest('.has-many-{$this->column}-form').find('.$removeClass').val(1);
558
});
559
560
EOT;
561
562
        Admin::script($script);
563
    }
564
565
    /**
566
     * Disable create button.