-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathTreeView.php
119 lines (101 loc) · 2.87 KB
/
TreeView.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
<?php
namespace execut\widget;
use execut\yii\jui\Widget;
use yii\base\Exception;
use yii\helpers\Html;
/**
* Bootstrap Tree View wrapper for yii2
*
* @property string $size Widget size mode TreeView::SIZE_SMALL | TreeView::SIZE_MIDDLE | TreeView::SIZE_NORMAL
* @property array $data Tree data
*
* @author eXeCUT
*/
class TreeView extends Widget {
const SIZE_SMALL = 'small';
const SIZE_MIDDLE = 'middle';
const SIZE_NORMAL = 'normal';
const TEMPLATE_ADVANCED = '<div class="tree-view-wrapper">
<div class="row tree-header">
<div class="col-sm-6">
<div class="tree-heading-container">{header}</div>
</div>
<div class="col-sm-6">
{search}
</div>
</div>
<div class="row">
<div class="col-sm-12">
{tree}
</div>
</div>
</div>';
const TEMPLATE_SIMPLE = '{tree}';
/**
* @var string Widget size
*/
public $size = TreeView::SIZE_NORMAL;
/**
* @var array Items list for widget
*/
public $data = [];
/**
* Main container html options
*
* @var array
*/
public $containerOptions = [];
/**
* Searvh widget options
*
* @var string
*/
public $searchOptions = [];
/**
* Widget header
*
* @var string
*/
public $header = 'Select from tree';
/**
* Template for render widget
*
* @var string
*/
public $template = self::TEMPLATE_ADVANCED;
/**
* Run widget
*/
public function run() {
if ($this->size !== self::SIZE_NORMAL) {
Html::addCssClass($this->options, $this->size);
}
if (!empty($this->options['id']) && $this->options['id'] !== $this->id) {
throw new Exception('Set id directly to the widget via id config key instead redefine widget config options key');
}
if (strpos($this->template, '{tree}') === false) {
throw new Exception('{tree} not found in widget template');
}
$parts = [
'{tree}' => Html::tag('div', '', $this->options),
'{header}' => $this->header,
];
if (strpos($this->template, '{search}') !== false) {
$parts['{search}'] = $this->renderSearchWidget();
}
echo Html::tag('div', strtr($this->template, $parts), $this->containerOptions);
$this->clientOptions['data'] = $this->data;
$this->registerWidget('treeview');
}
protected function renderSearchWidget() {
$options = $this->searchOptions;
$options['treeViewId'] = $this->id;
if (empty($options['inputOptions'])) {
$options['inputOptions'] = [];
}
if (empty($options['inputOptions']['placeholder'])) {
$options['inputOptions']['placeholder'] = 'Search...';
}
return TreeFilterInput::widget($options);
}
}