/
Area.php
190 lines (151 loc) · 4.49 KB
/
Area.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
<?php
namespace kak\widgets\area;
use yii\base\Model;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
/**
* use example
*/
/*
```php
```;
*/
/**
* Class Area
* @package kak\widgets\area
*/
class Area extends \yii\base\Widget
{
/**
* @var array the HTML attributes for the widget container tag.
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
public $options = [];
/** @var string text button */
public $buttonLabel = '+';
/** @var array html options button add item */
public $buttonOptions = [];
/** @var String|Closure render view file templated */
public $viewItem;
/** @var array addintion view params */
public $viewParams;
/** @var string template btn layout */
public $template = '{button} {label}';
/** @var array default item or model */
public $item;
/** @var array data array or models */
public $items = [];
/** @var array html options block item */
public $itemOptions = [];
/** @var string text label */
public $label = 'add item';
/** @var array html options label */
public $labelOptions = [];
public function init()
{
parent::init();
$this->initDefaultOptions();
$this->renderButtonAdd();
echo Html::beginTag('script', [
'id' => $this->options['id'],
'type' => 'text/x-tmpl'
]);
$this->getView()->beginBlock($this->getAreaId(), true);
echo Html::beginTag('div', $this->itemOptions);
}
/**
* @return string generate uuid block id
*/
public function getAreaId()
{
return 'areaId' . $this->options['id'];
}
/**
* set default html options
*/
protected function initDefaultOptions()
{
if (!isset($this->options['id'])) {
$this->options['id'] = $this->getId();
}
Html::addCssClass($this->itemOptions, 'areaItem');
Html::addCssClass($this->buttonOptions, 'btn');
}
/**
* render html template placeholder
*/
protected function renderButtonAdd()
{
$this->buttonOptions = ArrayHelper::merge($this->buttonOptions, [
'role' => 'area.add',
'data-tmpl' => $this->options['id']
]);
$template = strtr($this->template, [
'{button}' => Html::button($this->buttonLabel, $this->buttonOptions),
'{label}' => Html::label($this->label, $this->labelOptions)
]);
echo Html::tag('div', $template, ['class' => 'form-group']);
}
/**
* render default item, view
* @return mixed|string
*/
protected function renderTemplateItemForm()
{
if($this->viewItem===null){
return '';
}
return $this->renderTemplateItem($this->item);
}
/**
* render store items, view
* @param $item
* @return mixed|string
*/
protected function renderTemplateItem($item)
{
$params = $this->viewParams;
$params['item'] = $item;
if($this->viewItem instanceof \Closure){
return call_user_func($this->viewItem, $params, $this);
}
return $this->render($this->viewItem, $params);
}
/**
* render store items, block template
* @param $item
* @return mixed
*/
protected function renderTemplateBlock($item)
{
$keys = $item instanceof Model ? $item->getAttributes() : array_keys($item);
$clip = (string)$this->getView()->blocks[$this->getAreaId()];
foreach ($keys as $key) {
$value = Html::encode($item[$key]);
$clip = str_replace('{%=o.' . $key . '%}', $value, $clip);
}
return $clip = preg_replace('#\{\%\=o(.*)\%\}#ism', '', $clip);
}
public function run()
{
echo $this->renderTemplateItemForm();
echo Html::endTag('div');
$this->getView()->endBlock();
echo Html::endTag('script');
echo Html::beginTag('div', [
'id' => $this->getAreaId()
]);
foreach ($this->items as $item) {
echo Html::beginTag('div', $this->itemOptions);
if($this->viewItem !==null){
echo $this->renderTemplateItem($item);
}else{
echo $this->renderTemplateBlock($item);
}
echo Html::endTag('div');
}
echo Html::endTag('div');
TmplAsset::register($this->getView());
AreaAsset::register($this->getView());
}
}