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

BelongsTo::addScript()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 77

Duplication

Lines 77
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 77
loc 77
rs 8.5018
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Encore\Admin\Grid\Displayers;
4
5
use Encore\Admin\Admin;
6
use Encore\Admin\Grid\Selectable;
7
8
class BelongsTo extends AbstractDisplayer
9
{
10
    use BelongsToRelation;
11
12
    /**
13
     * @return $this
14
     */
15 View Code Duplication
    public 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...
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
95
     */
96
    protected function getOriginalData()
97
    {
98
        return $this->getColumn()->getOriginal();
99
    }
100
}
101