Code Duplication    Length = 34-47 lines in 3 locations

src/Form/Field/HasMany.php 3 locations

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