Code Duplication    Length = 34-47 lines in 2 locations

src/Form/Field/HasMany.php 2 locations

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