Code Duplication    Length = 77-89 lines in 2 locations

src/Grid/Displayers/BelongsTo.php 1 location

@@ 15-91 (lines=77) @@
12
    /**
13
     * @return $this
14
     */
15
    public function addScript()
16
    {
17
        $script = <<<SCRIPT
18
(function () {
19
    var modal = $('#{$this->modalID}');
20
    var related = null;
21
    var selected = {};
22
23
    var load = function (url) {
24
        $.get(url, function (data) {
25
            modal.find('.modal-body').html(data);
26
            modal.find('.select').iCheck({
27
                radioClass:'iradio_minimal-blue',
28
                checkboxClass:'icheckbox_minimal-blue'
29
            });
30
            modal.find('.box-header:first').hide();
31
32
            modal.find('input.select').each(function (index,    el) {
33
                if ($(el).val() == selected.id) {
34
                    $(el).iCheck('toggle');
35
                }
36
            });
37
        });
38
    };
39
40
    var update = function (callback) {
41
        $.ajax({
42
            url: "{$this->getResource()}/" + related.attr('key'),
43
            type: "POST",
44
            data: {
45
                {$this->columnName}: selected.id,
46
                _token: LA.token,
47
                _method: 'PUT'
48
            },
49
            success: function (data) {
50
                callback(data);
51
            }
52
        });
53
    };
54
55
    modal.on('show.bs.modal', function (e) {
56
        related = $(e.relatedTarget);
57
        selected.id = related.data('val');
58
        load("{$this->getLoadUrl()}");
59
    }).on('click', '.page-item a, .filter-box a', function (e) {
60
        load($(this).attr('href'));
61
        e.preventDefault();
62
    }).on('click', 'tr', function (e) {
63
        $(this).find('input.select').iCheck('toggle');
64
        e.preventDefault();
65
    }).on('submit', '.box-header form', function (e) {
66
        load($(this).attr('action')+'&'+$(this).serialize());
67
        e.preventDefault();
68
    }).on('ifChecked', 'input.select', function (e) {
69
        selected.id = $(this).val();
70
    }).find('.modal-footer .submit').click(function () {
71
        update(function (data) {
72
            related.data('val', selected);
73
            related.find('.text').html(data.display["{$this->columnName}"]);
74
            related.find('a').toggleClass('text-green text-muted');
75
76
            setTimeout(function () {
77
                related.find('a').toggleClass('text-green text-muted');
78
            }, 2000);
79
80
            modal.modal('toggle');
81
82
            toastr.success(data.message);
83
        });
84
    });
85
})();
86
SCRIPT;
87
88
        Admin::script($script);
89
90
        return $this;
91
    }
92
93
    /**
94
     * @return mixed

src/Grid/Displayers/BelongsToMany.php 1 location

@@ 24-112 (lines=89) @@
21
    /**
22
     * @return $this
23
     */
24
    public function addScript()
25
    {
26
        $script = <<<SCRIPT
27
(function () {
28
    var modal = $('#{$this->modalID}');
29
    var related = null;
30
    var selected = [];
31
32
    var load = function (url) {
33
        $.get(url, function (data) {
34
            modal.find('.modal-body').html(data);
35
            modal.find('.select').iCheck({
36
                radioClass:'iradio_minimal-blue',
37
                checkboxClass:'icheckbox_minimal-blue'
38
            });
39
            modal.find('.box-header:first').hide();
40
41
            modal.find('input.select').each(function (index, el) {
42
                if ($.inArray($(el).val().toString(), selected) >=0 ) {
43
                    $(el).iCheck('toggle');
44
                }
45
            });
46
        });
47
    };
48
49
    var update = function (callback) {
50
        $.ajax({
51
            url: "{$this->getResource()}/" + related.attr('key'),
52
            type: "POST",
53
            data: {
54
                {$this->columnName}: selected,
55
                _token: LA.token,
56
                _method: 'PUT'
57
            },
58
            success: function (data) {
59
                callback(data);
60
            }
61
        });
62
    };
63
64
    modal.on('show.bs.modal', function (e) {
65
        related = $(e.relatedTarget);
66
        selected = related.data('val').map(function (value) {
67
            return value.toString();
68
        });
69
70
        load("{$this->getLoadUrl(1)}");
71
    }).on('click', '.page-item a, .filter-box a', function (e) {
72
        load($(this).attr('href'));
73
        e.preventDefault();
74
    }).on('click', 'tr', function (e) {
75
        $(this).find('input.select').iCheck('toggle');
76
        e.preventDefault();
77
    }).on('submit', '.box-header form', function (e) {
78
        load($(this).attr('action')+'&'+$(this).serialize());
79
        e.preventDefault();
80
    }).on('ifChecked', 'input.select', function (e) {
81
        var val = $(this).val().toString();
82
        if (selected.indexOf(val) < 0) {
83
            selected.push(val);
84
        }
85
    }).on('ifUnchecked', 'input.select', function (e) {
86
           var val = $(this).val().toString();
87
           var index = selected.indexOf(val);
88
            if (index !== -1) {
89
                selected.splice(index, 1);
90
            }
91
    }).find('.modal-footer .submit').click(function () {
92
        update(function (data) {
93
            related.data('val', selected);
94
            related.find('.text').html(data.display["{$this->columnName}"]);
95
            related.find('a').toggleClass('text-green text-muted');
96
97
            setTimeout(function () {
98
                related.find('a').toggleClass('text-green text-muted');
99
            }, 2000);
100
101
            modal.modal('toggle');
102
103
            toastr.success(data.message);
104
        });
105
    });
106
})();
107
SCRIPT;
108
109
        Admin::script($script);
110
111
        return $this;
112
    }
113
114
    /**
115
     * Get other key for this many-to-many relation.