-
Notifications
You must be signed in to change notification settings - Fork 2
/
SimpleTreeBehavior.php
executable file
·116 lines (106 loc) · 3.46 KB
/
SimpleTreeBehavior.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
<?php
/**
* SimpleTreeBehavior class file.
* @copyright (c) 2014, Galament
* @license http://www.opensource.org/licenses/bsd-license.php
*/
namespace bariew\nodeTree;
use yii\base\Behavior;
/**
* Generates jstree tree view from parent children nodes.
*
* 1. Attach behavior in yii2 common way, define $actionPath and $id attribute name.
* 2.1 Call widget from model like self::findOne(1)->menuWidget()
* 2.2 Call checkbox widget like self::findOne(1)->checkboxWidget('node', $selectedItemIds)
*
* @author Pavel Bariev <[email protected]>
*/
class SimpleTreeBehavior extends Behavior
{
/**
* @var string url to model update action. Ends with '/update'
*/
public $actionPath = '/path/to/update-action';
/**
* @var string primaryKey attribute name
*/
public $id = 'id';
/**
* @var array selected node ids for checkbox widget.
*/
protected $selectedNodes = [];
/**
* @var integer incremential key for jstree items unique ids.
*/
public static $uniqueKey = 0;
/**
* @var array jstree icon item types.
* @link http://www.jstree.com/api/#/?f=$.jstree.defaults.types
*/
public $types = [
"folder" => ["icon" => "glyphicon glyphicon-folder"],
"user" => ["icon" => "glyphicon glyphicon-user"],
"flag" => ["icon" => "glyphicon glyphicon-flag"],
];
/**
* Generates attributes for jstree item from owner model.
* @param mixed $model model.
* @param int|string $pid view item parent id.
* @param bool $uniqueKey
* @return array attributes
*/
public function nodeAttributes($model = false, $pid = '', $uniqueKey = false)
{
$uniqueKey = $uniqueKey ? $uniqueKey : self::$uniqueKey++;
$model = ($model) ? $model : $this->owner;
$id = $model[$this->id];
$nodeId = $uniqueKey . '-id-' . $id;
return array(
'id' => $nodeId,
'model' => $model,
'children'=>$model->children,
'text' => $model['title'],
'type' => 'folder',
'selected' => in_array($model->{$this->id}, $this->selectedNodes),
'a_attr' => array(
'class' => 'jstree-clicked',
'data-id' => $nodeId,
'href' => [$this->actionPath, $this->id => $id, 'pid'=>$pid]
)
);
}
/**
* Generates jstree menu from owner children.
* @param array $data data for widget
* @param bool|string $callback $this method name for data processing
* - define it for variable options and bind functions attaching.
* @return Widget menu widget
*/
public function menuWidget($data = [], $callback = false)
{
$data = array_merge([
'items' => [$this->owner],
'behavior' => $this,
'view' => 'node'
], $data);
if ($callback) {
$data = $this->$callback($data);
}
$widget = new ARTreeMenuWidget($data);
return $widget->run();
}
/**
* Callback example for $this->menuWidget() method.
* @param array $data data to process.
* @return array processed data.
*/
public function defaultCallback($data)
{
$data['options'] = [
'types' => $this->types,
'plugins' => ['checkbox', 'search', 'types'],
'checkbox' => ["keep_selected_style" => false]
];
return $data;
}
}