-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNestable.php
246 lines (217 loc) · 7.26 KB
/
Nestable.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?php
namespace wonail\nestable;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\widgets\InputWidget;
/**
* Nestable widget
*/
class Nestable extends InputWidget
{
/**
* @var string 主题名称
*/
public $theme = '';
/**
* @var array
*/
public $ddOptions = [];
/**
* @var array
*
* - group
* - title
* - items
* - id
* - title
* - name
* - ...
* - children
* - ...
*/
public $items;
/**
* @var string
*/
public $childrenName = 'children';
/**
* @var string|\Closure 显示标题
*/
public $showTitle = 'title';
/**
* @var string|array 允许设置为`data-`的参数,默认为[[items[]]]数组下的全部数据
*/
public $allowParams;
/**
* @var boolean
*/
public $draggableHandles = false;
/**
* @var boolean 是否折叠,默认为`false`
*/
public $collapsed = false;
/**
* @var array widget plugin options.
*
* - maxDepth: number of levels an item can be nested (default 5)
* - group: group ID to allow dragging between lists (default 0)
* - listNodeName: The HTML element to create for lists (default 'ol')
* - itemNodeName: The HTML element to create for list items (default 'li')
* - rootClass: The class of the root element .nestable() was used on (default 'dd')
* - itemClass: The class of all list item elements (default 'dd-item')
* - dragClass: The class applied to the list element that is being dragged (default 'dd-dragel')
* - listClass: The class of all list elements (default 'dd-list')
* - handleClass: The class of the content element inside each list item (default 'dd-handle')
* - collapsedClass: The class applied to lists that have been collapsed (default 'dd-collapsed')
* - placeClass: The class of the placeholder element (default 'dd-placeholder')
* - emptyClass: The class used for empty list placeholder elements (default 'dd-empty')
* - expandBtnHTML: The HTML text used to generate a list item expand button (default '<button data-action="expand">Expand></button>')
* - collapseBtnHTML: The HTML text used to generate a list item collapse button (default '<button data-action="collapse">Collapse</button>')
*/
public $pluginOptions = [];
/**
* @inheritdoc
*/
public function init()
{
parent::init();
Html::addCssClass($this->options, ['nestable-lists']);
}
/**
* @inheritdoc
*/
public function run()
{
parent::run();
$this->registerPluginAssets();
$this->renderWidget();
}
/**
* Initializes and renders the widget
*/
public function renderWidget()
{
// BEGIN:nestable-box
echo Html::beginTag('div', ['class' => 'nestable-box']);
foreach ($this->items as $item) {
$this->renderGroup($item);
}
// END:nestable-box
echo Html::endTag('div');
if ($this->hasModel()) {
echo Html::activeHiddenInput($this->model, $this->attribute);
} else {
echo Html::hiddenInput($this->name);
}
}
protected function renderGroup($item)
{
// BEGIN:nestable-lists
$options['data-group'] = $item['group'];
$options['data-title'] = $item['title'];
$options = array_merge($this->options, $options);
unset($options['id']);
echo Html::beginTag('div', $options);
$this->id = static::$autoIdPrefix . static::$counter++;
// BEGIN:dd
$ddOptions = [
'class' => 'dd',
'id' => $this->id,
];
$this->ddOptions = array_merge($this->ddOptions, $ddOptions);
echo Html::beginTag('div', $this->ddOptions);
echo Html::tag('div', $item['title'], ['class' => 'nestable-title']);
// BEGIN:dd-list
$this->renderList(isset($item['items']) ? $item['items'] : []);
// END:dd-list
// END:dd
echo Html::endTag('div');
// END:nestable-lists
echo Html::endTag('div');
}
private function renderList($items)
{
if (empty($items)) {
echo Html::tag('div', '', ['class' => 'dd-empty']);
} else {
$options['class'] = 'dd-list';
if (!empty($this->theme)) {
Html::addCssClass($options, 'dd-' . $this->theme);
}
echo Html::beginTag('ol', $options);
foreach ($items as $item) {
$this->renderItem($item);
}
echo Html::endTag('ol');
}
}
private function renderItem($item)
{
if ($this->draggableHandles) {
$options = ['class' => 'dd-item dd3-item'];
} else {
$options = ['class' => 'dd-item'];
}
if ($this->collapsed) {
Html::addCssClass($options, 'dd-collapsed');
}
// 设置`data-`格式参数
if (is_string($this->allowParams)) {
$this->allowParams = explode(',', $this->allowParams);
}
foreach ($item as $k => $v) {
if (is_array($this->allowParams)) {
if (in_array($k, $this->allowParams)) {
$options['data'][$k] = $v;
}
} elseif ($k !== $this->childrenName) {
$options['data'][$k] = $v;
}
}
echo Html::beginTag('li', $options);
if ($this->showTitle instanceof \Closure) {
$title = call_user_func($this->showTitle, $item);
} else {
$title = $item[$this->showTitle];
}
if ($this->draggableHandles) {
echo Html::tag('div', Html::tag('span', 'Drag', ['class' => 'sr-only']), ['class' => 'dd-handle dd3-handle']);
echo Html::tag('div', $title, ['class' => 'dd3-content']);
} else {
echo Html::tag('div', $title, ['class' => 'dd-handle']);
}
if (isset($item[$this->childrenName]) && count($item[$this->childrenName])) {
$this->renderList($item[$this->childrenName]);
}
echo Html::endTag('li');
}
/**
* Register Asset manager
*/
private function registerPluginAssets()
{
$view = $this->getView();
NestableAsset::register($view);
$pluginOptions = Json::encode($this->pluginOptions);
$name = $this->hasModel() ? Html::getInputName($this->model, $this->attribute) : $this->name;
$js = <<<JS
$('.dd').nestable({$pluginOptions}).on('change', function () {
var obj = $(this).parents('.nestable-box'),
nestable = new Array();
obj.find('.nestable-lists').each(function (index, element) {
if ($(element).data('group')) {
nestable[index] = new Object();
nestable[index]['group'] = $(element).data('group');
nestable[index]['title'] = $(element).data('title');
nestable[index]['items'] = $(element).find('.dd').nestable('serialize');
}
});
$("[name=\"{$name}\"]").val(JSON.stringify(nestable));
});
JS;
if ($this->collapsed) {
$js .= "\n$('.dd').nestable('collapseAll')";
}
$view->registerJs($js);
}
}