Completed
Push — master ( 4ca583...a67af2 )
by Song
02:43
created

BelongsTo   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 106
Duplicated Lines 84.91 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 90
loc 106
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
B addScript() 90 90 1
A getOptions() 0 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Encore\Admin\Form\Field;
4
5
use Encore\Admin\Admin;
6
use Encore\Admin\Form;
7
use Encore\Admin\Form\Field;
8
use Encore\Admin\Grid\Selectable;
9
10
class BelongsTo extends Select
11
{
12
    use BelongsToRelation;
13
14 View Code Duplication
    protected function addScript()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
15
    {
16
        $script = <<<SCRIPT
17
(function () {
18
19
    var grid = $('.belongsto-{$this->column()}');
20
    var modal = $('#{$this->modalID}');
21
    var table = grid.find('.grid-table');
22
    var selected = $("{$this->getElementClassSelector()}").val();
23
    var row = null;
24
25
    // open modal
26
    grid.find('.select-relation').click(function (e) {
27
        $('#{$this->modalID}').modal('show');
28
        e.preventDefault();
29
    });
30
31
    // remove row
32
    grid.on('click', '.grid-row-remove', function () {
33
        selected = null;
34
        $(this).parents('tr').remove();
35
        $("{$this->getElementClassSelector()}").val(null);
36
37
        var empty = $('.belongsto-{$this->column()}').find('template.empty').html();
38
39
        table.find('tbody').append(empty);
40
    });
41
42
    var load = function (url) {
43
        $.get(url, function (data) {
44
            modal.find('.modal-body').html(data);
45
            modal.find('.select').iCheck({
46
                radioClass:'iradio_minimal-blue',
47
                checkboxClass:'icheckbox_minimal-blue'
48
            });
49
            modal.find('.box-header:first').hide();
50
51
            modal.find('input.select').each(function (index, el) {
52
                if ($(el).val() == selected) {
53
                    $(el).iCheck('toggle');
54
                }
55
            });
56
        });
57
    };
58
59
    var update = function (callback) {
60
61
        $("{$this->getElementClassSelector()}")
62
            .select2({data: [selected]})
63
            .val(selected)
64
            .trigger('change')
65
            .next()
66
            .addClass('hide');
67
68
        if (row) {
69
            row.find('td:last a').removeClass('hide');
70
            row.find('td:first').remove();
71
            table.find('tbody').empty().append(row);
72
        }
73
74
        callback();
75
    };
76
77
    modal.on('show.bs.modal', function (e) {
78
        load("{$this->getLoadUrl()}");
79
    }).on('click', '.page-item a, .filter-box a', function (e) {
80
        load($(this).attr('href'));
81
        e.preventDefault();
82
    }).on('click', 'tr', function (e) {
83
        $(this).find('input.select').iCheck('toggle');
84
        e.preventDefault();
85
    }).on('submit', '.box-header form', function (e) {
86
        load($(this).attr('action')+'&'+$(this).serialize());
87
        e.preventDefault();
88
        return false;
89
    }).on('ifChecked', 'input.select', function (e) {
90
        row = $(e.target).parents('tr');
91
        selected = $(this).val();
92
    }).find('.modal-footer .submit').click(function () {
93
        update(function () {
94
            modal.modal('toggle');
95
        });
96
    });
97
})();
98
SCRIPT;
99
100
        Admin::script($script);
101
102
        return $this;
103
    }
104
105
    protected function getOptions()
106
    {
107
        $options = [];
108
109
        if ($value = $this->value()) {
110
            $options = [$value => $value];
111
        }
112
113
        return $options;
114
    }
115
}
116