|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Encore\Admin\Form\Field; |
|
4
|
|
|
|
|
5
|
|
|
use Encore\Admin\Admin; |
|
6
|
|
|
use Encore\Admin\Form\Field; |
|
7
|
|
|
use Encore\Admin\Grid\Selectable; |
|
8
|
|
|
|
|
9
|
|
|
class BelongsToMany extends MultipleSelect |
|
10
|
|
|
{ |
|
11
|
|
|
use BelongsToRelation; |
|
12
|
|
|
|
|
13
|
|
View Code Duplication |
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
|
|
|
{ |
|
137
|
|
|
$options = []; |
|
138
|
|
|
|
|
139
|
|
|
if ($this->value()) { |
|
140
|
|
|
$options = array_combine($this->value(), $this->value()); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return $options; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
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.