Code Duplication    Length = 34-47 lines in 2 locations

src/Form/Field/HasMany.php 2 locations

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