|
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() |
|
|
|
|
|
|
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
|
|
|
|
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.