1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace devgroup\JsTreeWidget\widgets; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use yii\base\InvalidConfigException; |
7
|
|
|
use yii\base\Widget; |
8
|
|
|
use yii\helpers\Html; |
9
|
|
|
use yii\helpers\Json; |
10
|
|
|
use yii\helpers\Url; |
11
|
|
|
use yii\web\JsExpression; |
12
|
|
|
use yii\helpers\ArrayHelper; |
13
|
|
|
use yii\web\View; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* JsTree widget for Yii Framework 2 |
17
|
|
|
*/ |
18
|
|
|
class TreeWidget extends Widget |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
const TREE_TYPE_ADJACENCY = 'adjacency'; |
22
|
|
|
const TREE_TYPE_NESTED_SET = 'nested-set'; |
23
|
|
|
|
24
|
|
|
public $treeType = self::TREE_TYPE_ADJACENCY; |
25
|
|
|
/** |
26
|
|
|
* @var array Enabled jsTree plugins |
27
|
|
|
* @see http://www.jstree.com/plugins/ |
28
|
|
|
*/ |
29
|
|
|
public $plugins = [ |
30
|
|
|
'wholerow', |
31
|
|
|
'contextmenu', |
32
|
|
|
'dnd', |
33
|
|
|
'types', |
34
|
|
|
'state', |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array Configuration for types plugin |
39
|
|
|
* @see http://www.jstree.com/api/#/?f=$.jstree.defaults.types |
40
|
|
|
*/ |
41
|
|
|
public $types = [ |
42
|
|
|
'show' => [ |
43
|
|
|
'icon' => 'fa fa-file-o', |
44
|
|
|
], |
45
|
|
|
'list' => [ |
46
|
|
|
'icon' => 'fa fa-list', |
47
|
|
|
], |
48
|
|
|
]; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Context menu actions configuration. |
52
|
|
|
* @var array |
53
|
|
|
*/ |
54
|
|
|
public $contextMenuItems = []; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Various options for jsTree plugin. Will be merged with default options. |
58
|
|
|
* @var array |
59
|
|
|
*/ |
60
|
|
|
public $options = []; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Route to action which returns json data for tree |
64
|
|
|
* @var array |
65
|
|
|
*/ |
66
|
|
|
public $treeDataRoute = null; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Translation category for Yii::t() which will be applied to labels. |
70
|
|
|
* If translation is not needed - use false. |
71
|
|
|
*/ |
72
|
|
|
public $menuLabelsTranslationCategory = false; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* JsExpression for action(callback function) on double click. You can use JsExpression or make custom expression. |
76
|
|
|
* Warning! Callback function differs from native jsTree function - it consumes only one attribute - node(similar to contextmenu action). |
|
|
|
|
77
|
|
|
* Use false if no action needed. |
78
|
|
|
* @var bool|JsExpression |
79
|
|
|
*/ |
80
|
|
|
public $doubleClickAction = false; |
81
|
|
|
|
82
|
|
|
/** @var bool|array route to change parent action (applicable to Adjacency List only) */ |
83
|
|
|
public $changeParentAction = false; |
84
|
|
|
|
85
|
|
|
/** @var bool|array route to reorder action */ |
86
|
|
|
public $reorderAction = false; |
87
|
|
|
|
88
|
|
|
/** @var bool plugin config option for allow multiple nodes selections or not */ |
89
|
|
|
public $multiSelect = false; |
90
|
|
|
|
91
|
|
|
/** @var string Default labels translation category */ |
92
|
|
|
private $defaultTranslationCategory = 'jstw.defaults'; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @inheritdoc |
96
|
|
|
*/ |
97
|
|
|
public function init() |
98
|
|
|
{ |
99
|
|
|
if (false === $this->menuLabelsTranslationCategory) { |
100
|
|
|
$this->menuLabelsTranslationCategory = $this->defaultTranslationCategory; |
|
|
|
|
101
|
|
|
} |
102
|
|
|
self::registerTranslations(); |
103
|
|
|
parent::init(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public static function registerTranslations() |
107
|
|
|
{ |
108
|
|
|
Yii::$app->i18n->translations['jstw*'] = [ |
109
|
|
|
'class' => 'yii\i18n\PhpMessageSource', |
110
|
|
|
'basePath' => dirname(__DIR__) . DIRECTORY_SEPARATOR . 'messages', |
111
|
|
|
]; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @inheritdoc |
116
|
|
|
*/ |
117
|
|
|
public function run() |
118
|
|
|
{ |
119
|
|
|
if (!is_array($this->treeDataRoute)) { |
120
|
|
|
throw new InvalidConfigException("Attribute treeDataRoute is required to use TreeWidget."); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$options = [ |
124
|
|
|
'plugins' => $this->plugins, |
125
|
|
|
'core' => [ |
126
|
|
|
'check_callback' => true, |
127
|
|
|
'multiple' => $this->multiSelect, |
128
|
|
|
'data' => [ |
129
|
|
|
'url' => new JsExpression( |
130
|
|
|
"function (node) { |
131
|
|
|
return " . Json::encode(Url::to($this->treeDataRoute)) . "; |
132
|
|
|
}" |
133
|
|
|
), |
134
|
|
|
'success' => new JsExpression( |
135
|
|
|
"function (node) { |
136
|
|
|
return { 'id' : node.id }; |
137
|
|
|
}" |
138
|
|
|
), |
139
|
|
|
'data' => new JsExpression( |
140
|
|
|
"function (node) { |
141
|
|
|
return { 'id' : node.id }; |
142
|
|
|
}" |
143
|
|
|
), |
144
|
|
|
'error' => new JsExpression( |
145
|
|
|
"function ( o, textStatus, errorThrown ) { |
146
|
|
|
alert(o.responseText); |
147
|
|
|
}" |
148
|
|
|
) |
149
|
|
|
] |
150
|
|
|
] |
151
|
|
|
]; |
152
|
|
|
|
153
|
|
|
// merge with attribute-provided options |
154
|
|
|
$options = ArrayHelper::merge($options, $this->options); |
155
|
|
|
if (false === empty($this->contextMenuItems)) { |
156
|
|
|
if (!in_array('contextmenu', $this->plugins)) { |
157
|
|
|
// add missing contextmenu plugin |
158
|
|
|
$options['plugins'] = ['contextmenu']; |
159
|
|
|
} |
160
|
|
|
$functionName = $this->getId() . 'ContextMenu'; |
161
|
|
|
$options['contextmenu'] = ['items' => new JsExpression($functionName)]; |
162
|
|
|
$this->contextMenuOptions($functionName); |
163
|
|
|
} |
164
|
|
|
$options = Json::encode($options); |
165
|
|
|
$this->getView()->registerAssetBundle('devgroup\JsTreeWidget\widgets\JsTreeAssetBundle'); |
166
|
|
|
|
167
|
|
|
$doubleClick = ''; |
168
|
|
|
if ($this->doubleClickAction !== false) { |
169
|
|
|
$doubleClick = " |
170
|
|
|
jsTree_{$this->getId()}.on('dblclick.jstree', function (e) { |
171
|
|
|
var node = $(e.target).closest('.jstree-node').children('.jstree-anchor'); |
172
|
|
|
var callback = " . $this->doubleClickAction . "; |
173
|
|
|
callback(node); |
174
|
|
|
return false; |
175
|
|
|
});\n"; |
176
|
|
|
} |
177
|
|
|
$treeJs = $this->prepareJs(); |
178
|
|
|
$this->getView()->registerJs(" |
179
|
|
|
var jsTree_{$this->getId()} = \$('#{$this->getId()}').jstree($options); |
180
|
|
|
$doubleClick $treeJs", View::POS_READY); |
181
|
|
|
return Html::tag('div', '', ['id' => $this->getId()]); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @param $functionName |
186
|
|
|
* @return array |
|
|
|
|
187
|
|
|
*/ |
188
|
|
|
private function contextMenuOptions($functionName) |
189
|
|
|
{ |
190
|
|
|
$items = []; |
191
|
|
|
$conditionItems = ""; |
192
|
|
|
foreach ($this->contextMenuItems as $index => $item) { |
193
|
|
|
$item['label'] = Yii::t($this->menuLabelsTranslationCategory, $item['label']); |
|
|
|
|
194
|
|
|
if (false === empty($item['showWhen'])) { |
195
|
|
|
if (true === is_array($item['showWhen'])) { |
196
|
|
|
$condition = []; |
197
|
|
|
foreach ($item['showWhen'] as $key => $value) { |
198
|
|
|
$key = (false !== strpos($key, 'data-')) ? $key : 'data-' . $key; |
199
|
|
|
$condition[] = "node.hasOwnProperty('a_attr') && node.a_attr['$key'] == {$value}"; |
200
|
|
|
} |
201
|
|
|
$condition = implode(' && ', $condition); |
202
|
|
|
} else { |
203
|
|
|
$condition = $item['showWhen']; |
204
|
|
|
} |
205
|
|
|
unset($item['showWhen']); |
206
|
|
|
$item = Json::encode($item); |
207
|
|
|
$conditionItems .= new JsExpression(" |
208
|
|
|
if ({$condition}) { |
209
|
|
|
items.{$index} = $item; |
210
|
|
|
} |
211
|
|
|
"); |
212
|
|
|
} else { |
213
|
|
|
$items[$index] = $item; |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
$items = Json::encode($items); |
217
|
|
|
$js = <<<JS |
218
|
|
|
function $functionName(node) { |
219
|
|
|
var items = $items; |
220
|
|
|
$conditionItems |
221
|
|
|
return items; |
222
|
|
|
} |
223
|
|
|
JS; |
224
|
|
|
$this->view->registerJs($js, View::POS_HEAD); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Prepares js according to given tree type |
229
|
|
|
* |
230
|
|
|
* @return string |
|
|
|
|
231
|
|
|
*/ |
232
|
|
|
private function prepareJs() |
233
|
|
|
{ |
234
|
|
|
switch ($this->treeType) { |
235
|
|
|
case self::TREE_TYPE_ADJACENCY : |
|
|
|
|
236
|
|
|
return $this->adjacencyJs(); |
237
|
|
|
case self::TREE_TYPE_NESTED_SET : |
|
|
|
|
238
|
|
|
return $this->nestedSetJs(); |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @return string |
244
|
|
|
*/ |
245
|
|
|
private function adjacencyJs() |
246
|
|
|
{ |
247
|
|
|
$changeParentJs = ''; |
248
|
|
|
if ($this->changeParentAction !== false) { |
249
|
|
|
$changeParentUrl = is_array($this->changeParentAction) ? Url::to($this->changeParentAction) : $this->changeParentAction; |
|
|
|
|
250
|
|
|
$changeParentJs = <<<JS |
251
|
|
|
jsTree_{$this->getId()}.on('move_node.jstree', function(e, data) { |
252
|
|
|
var \$this = $(this); |
253
|
|
|
$.get('$changeParentUrl', { |
254
|
|
|
'id': data.node.id, |
255
|
|
|
'parent_id': data.parent |
256
|
|
|
}, "json") |
257
|
|
|
.done(function (data) { |
258
|
|
|
if ('undefined' !== typeof(data.error)) { |
259
|
|
|
alert(data.error); |
260
|
|
|
} |
261
|
|
|
\$this.jstree('refresh'); |
262
|
|
|
}) |
263
|
|
|
.fail(function ( o, textStatus, errorThrown ) { |
264
|
|
|
alert(o.responseText); |
265
|
|
|
}); |
266
|
|
|
return false; |
267
|
|
|
}); |
268
|
|
|
JS; |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
$reorderJs = ''; |
272
|
|
|
if ($this->reorderAction !== false) { |
273
|
|
|
$reorderUrl = is_array($this->reorderAction) ? Url::to($this->reorderAction) : $this->reorderAction; |
274
|
|
|
$reorderJs = <<<JS |
275
|
|
|
jsTree_{$this->getId()}.on('move_node.jstree', function(e, data) { |
276
|
|
|
var params = []; |
277
|
|
|
var \$this = $(this); |
278
|
|
|
$('.jstree-node').each(function(i, e) { |
279
|
|
|
params[e.id] = i; |
280
|
|
|
}); |
281
|
|
|
$.post('$reorderUrl', { |
282
|
|
|
'order':params, |
283
|
|
|
'id': data.node.id |
284
|
|
|
}, |
285
|
|
|
"json") |
286
|
|
|
.done(function (data) { |
287
|
|
|
if ('undefined' !== typeof(data.error)) { |
288
|
|
|
alert(data.error); |
289
|
|
|
} |
290
|
|
|
\$this.jstree('refresh'); |
291
|
|
|
}) |
292
|
|
|
.fail(function ( o, textStatus, errorThrown ) { |
293
|
|
|
alert(o.responseText); |
294
|
|
|
}); |
295
|
|
|
return false; |
296
|
|
|
}); |
297
|
|
|
JS; |
298
|
|
|
} |
299
|
|
|
return $changeParentJs . "\n" . $reorderJs . "\n"; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* @return string |
304
|
|
|
*/ |
305
|
|
|
private function nestedSetJs() |
306
|
|
|
{ |
307
|
|
|
$js = ""; |
308
|
|
|
if (false !== $this->reorderAction || false !== $this->changeParentAction) { |
309
|
|
|
$action = $this->reorderAction ?: $this->changeParentAction; |
310
|
|
|
$url = is_array($action) ? Url::to($action) : $action; |
311
|
|
|
$js = <<<JS |
312
|
|
|
jsTree_{$this->getId()}.on('move_node.jstree', function(e, data) { |
313
|
|
|
var \$this = $(this), |
314
|
|
|
\$parent = \$this.jstree(true).get_node(data.parent), |
315
|
|
|
\$oldParent = \$this.jstree(true).get_node(data.old_parent), |
316
|
|
|
siblings = \$parent.children || {}; |
317
|
|
|
$.post('$url', { |
318
|
|
|
'node_id' : data.node.id, |
319
|
|
|
'parent': data.parent, |
320
|
|
|
'position': data.position, |
321
|
|
|
'old_parent': data.old_parent, |
322
|
|
|
'old_position': data.old_position, |
323
|
|
|
'is_multi': data.is_multi, |
324
|
|
|
'siblings': siblings |
325
|
|
|
}, "json") |
326
|
|
|
.done(function (data) { |
327
|
|
|
if ('undefined' !== typeof(data.error)) { |
328
|
|
|
alert(data.error); |
329
|
|
|
} |
330
|
|
|
\$this.jstree('refresh'); |
331
|
|
|
}) |
332
|
|
|
.fail(function ( o, textStatus, errorThrown ) { |
333
|
|
|
alert(o.responseText); |
334
|
|
|
}); |
335
|
|
|
return false; |
336
|
|
|
}); |
337
|
|
|
JS; |
338
|
|
|
} |
339
|
|
|
return $js . "\n"; |
340
|
|
|
} |
341
|
|
|
} |
342
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.