Code Duplication    Length = 90-121 lines in 2 locations

src/Form/Field/BelongsTo.php 1 location

@@ 14-103 (lines=90) @@
11
{
12
    use BelongsToRelation;
13
14
    protected function addScript()
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
    {

src/Form/Field/BelongsToMany.php 1 location

@@ 13-133 (lines=121) @@
10
{
11
    use BelongsToRelation;
12
13
    protected function addScript()
14
    {
15
        $script = <<<SCRIPT
16
(function () {
17
18
    var grid = $('.belongstomany-{$this->column()}');
19
    var modal = $('#{$this->modalID}');
20
    var table = grid.find('.grid-table');
21
    var selected = $("{$this->getElementClassSelector()}").val() || [];
22
    var rows = {};
23
24
    table.find('tbody').children().each(function (index, tr) {
25
        if ($(tr).find('.grid-row-remove')) {
26
            rows[$(tr).find('.grid-row-remove').data('key')] = $(tr);
27
        }
28
    });
29
30
    // open modal
31
    grid.find('.select-relation').click(function (e) {
32
        $('#{$this->modalID}').modal('show');
33
        e.preventDefault();
34
    });
35
36
    // remove row
37
    grid.on('click', '.grid-row-remove', function () {
38
        val = $(this).data('key').toString();
39
40
        var index = selected.indexOf(val);
41
        if (index !== -1) {
42
           selected.splice(index, 1);
43
        }
44
45
        $(this).parents('tr').remove();
46
        $("{$this->getElementClassSelector()}").val(selected);
47
48
        if (selected.length == 0) {
49
            var empty = $('.belongstomany-{$this->column()}').find('template.empty').html();
50
            table.find('tbody').append(empty);
51
        }
52
    });
53
54
    var load = function (url) {
55
        $.get(url, function (data) {
56
            modal.find('.modal-body').html(data);
57
            modal.find('.select').iCheck({
58
                radioClass:'iradio_minimal-blue',
59
                checkboxClass:'icheckbox_minimal-blue'
60
            });
61
            modal.find('.box-header:first').hide();
62
63
            modal.find('input.select').each(function (index, el) {
64
                if ($.inArray($(el).val().toString(), selected) >=0 ) {
65
                    $(el).iCheck('toggle');
66
                }
67
            });
68
        });
69
    };
70
71
    var update = function (callback) {
72
73
        $("{$this->getElementClassSelector()}")
74
            .select2({data: selected})
75
            .val(selected)
76
            .trigger('change')
77
            .next()
78
            .addClass('hide');
79
80
        table.find('tbody').empty();
81
82
        Object.values(rows).forEach(function (row) {
83
            row.find('td:last a').removeClass('hide');
84
            row.find('td.column-__modal_selector__').remove();
85
            table.find('tbody').append(row);
86
        });
87
88
        if (selected.length == 0) {
89
            var empty = $('.belongstomany-{$this->column()}').find('template.empty').html();
90
            table.find('tbody').append(empty);
91
        } else {
92
            table.find('.empty-grid').parent().remove();
93
        }
94
95
        callback();
96
    };
97
98
    modal.on('show.bs.modal', function (e) {
99
        load("{$this->getLoadUrl(1)}");
100
    }).on('click', '.page-item a, .filter-box a', function (e) {
101
        load($(this).attr('href'));
102
        e.preventDefault();
103
    }).on('click', 'tr', function (e) {
104
        $(this).find('input.select').iCheck('toggle');
105
        e.preventDefault();
106
    }).on('submit', '.box-header form', function (e) {
107
        load($(this).attr('action')+'&'+$(this).serialize());
108
        e.preventDefault();
109
        return false;
110
    }).on('ifChecked', 'input.select', function (e) {
111
        if (selected.indexOf($(this).val()) < 0) {
112
            selected.push($(this).val());
113
            rows[$(e.target).val()] = $(e.target).parents('tr');
114
        }
115
    }).on('ifUnchecked', 'input.select', function (e) {
116
           var val = $(this).val();
117
           var index = selected.indexOf(val);
118
           if (index !== -1) {
119
               selected.splice(index, 1);
120
               delete rows[$(e.target).val()];
121
           }
122
    }).find('.modal-footer .submit').click(function () {
123
        update(function () {
124
            modal.modal('toggle');
125
        });
126
    });
127
})();
128
SCRIPT;
129
130
        Admin::script($script);
131
132
        return $this;
133
    }
134
135
    protected function getOptions()
136
    {