Code Duplication    Length = 34-47 lines in 3 locations

src/Form/Field/HasMany.php 3 locations

@@ 471-504 (lines=34) @@
468
     *
469
     * @return void
470
     */
471
    protected function setupScriptForDefaultView($templateScript)
472
    {
473
        $removeClass = NestedForm::REMOVE_FLAG_CLASS;
474
        $defaultKey = NestedForm::DEFAULT_KEY_NAME;
475
476
        /**
477
         * When add a new sub form, replace all element key in new sub form.
478
         *
479
         * @example comments[new___key__][title]  => comments[new_{index}][title]
480
         *
481
         * {count} is increment number of current sub form count.
482
         */
483
        $script = <<<EOT
484
var index = 0;
485
$('#has-many-{$this->column}').on('click', '.add', function () {
486
487
    var tpl = $('template.{$this->column}-tpl');
488
489
    index++;
490
491
    var template = tpl.html().replace(/{$defaultKey}/g, index);
492
    $('.has-many-{$this->column}-forms').append(template);
493
    {$templateScript}
494
});
495
496
$('#has-many-{$this->column}').on('click', '.remove', function () {
497
    $(this).closest('.has-many-{$this->column}-form').hide();
498
    $(this).closest('.has-many-{$this->column}-form').find('.$removeClass').val(1);
499
});
500
501
EOT;
502
503
        Admin::script($script);
504
    }
505
506
    /**
507
     * Setup tab template script.
@@ 513-559 (lines=47) @@
510
     *
511
     * @return void
512
     */
513
    protected function setupScriptForTabView($templateScript)
514
    {
515
        $removeClass = NestedForm::REMOVE_FLAG_CLASS;
516
        $defaultKey = NestedForm::DEFAULT_KEY_NAME;
517
518
        $script = <<<EOT
519
520
$('#has-many-{$this->column} > .nav').off('click', 'i.close-tab').on('click', 'i.close-tab', function(){
521
    var \$navTab = $(this).siblings('a');
522
    var \$pane = $(\$navTab.attr('href'));
523
    if( \$pane.hasClass('new') ){
524
        \$pane.remove();
525
    }else{
526
        \$pane.removeClass('active').find('.$removeClass').val(1);
527
    }
528
    if(\$navTab.closest('li').hasClass('active')){
529
        \$navTab.closest('li').remove();
530
        $('#has-many-{$this->column} > .nav > li:nth-child(1) > a').tab('show');
531
    }else{
532
        \$navTab.closest('li').remove();
533
    }
534
});
535
536
var index = 0;
537
$('#has-many-{$this->column} > .header').off('click', '.add').on('click', '.add', function(){
538
    index++;
539
    var navTabHtml = $('#has-many-{$this->column} > template.nav-tab-tpl').html().replace(/{$defaultKey}/g, index);
540
    var paneHtml = $('#has-many-{$this->column} > template.pane-tpl').html().replace(/{$defaultKey}/g, index);
541
    $('#has-many-{$this->column} > .nav').append(navTabHtml);
542
    $('#has-many-{$this->column} > .tab-content').append(paneHtml);
543
    $('#has-many-{$this->column} > .nav > li:last-child a').tab('show');
544
    {$templateScript}
545
});
546
547
if ($('.has-error').length) {
548
    $('.has-error').parent('.tab-pane').each(function () {
549
        var tabId = '#'+$(this).attr('id');
550
        $('li a[href="'+tabId+'"] i').removeClass('hide');
551
    });
552
    
553
    var first = $('.has-error:first').parent().attr('id');
554
    $('li a[href="#'+first+'"]').tab('show');
555
}
556
EOT;
557
558
        Admin::script($script);
559
    }
560
561
    /**
562
     * Setup default template script.
@@ 568-601 (lines=34) @@
565
     *
566
     * @return void
567
     */
568
    protected function setupScriptForTableView($templateScript)
569
    {
570
        $removeClass = NestedForm::REMOVE_FLAG_CLASS;
571
        $defaultKey = NestedForm::DEFAULT_KEY_NAME;
572
573
        /**
574
         * When add a new sub form, replace all element key in new sub form.
575
         *
576
         * @example comments[new___key__][title]  => comments[new_{index}][title]
577
         *
578
         * {count} is increment number of current sub form count.
579
         */
580
        $script = <<<EOT
581
var index = 0;
582
$('#has-many-{$this->column}').on('click', '.add', function () {
583
584
    var tpl = $('template.{$this->column}-tpl');
585
586
    index++;
587
588
    var template = tpl.html().replace(/{$defaultKey}/g, index);
589
    $('.has-many-{$this->column}-forms').append(template);
590
    {$templateScript}
591
});
592
593
$('#has-many-{$this->column}').on('click', '.remove', function () {
594
    $(this).closest('.has-many-{$this->column}-form').hide();
595
    $(this).closest('.has-many-{$this->column}-form').find('.$removeClass').val(1);
596
});
597
598
EOT;
599
600
        Admin::script($script);
601
    }
602
603
    /**
604
     * Disable create button.