1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/DMGPage/yii2-materialize |
4
|
|
|
* @copyright Copyright (c) 2018 Dmitrijs Reinmanis |
5
|
|
|
* @license https://github.com/DMGPage/yii2-materialize/blob/master/LICENSE |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace dmgpage\yii2materialize\widgets; |
9
|
|
|
|
10
|
|
|
use yii\helpers\ArrayHelper; |
11
|
|
|
use dmgpage\yii2materialize\helpers\Html; |
12
|
|
|
use yii\base\InvalidConfigException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The tabs structure consists of an unordered list of tabs that have hashes corresponding to tab ids. |
16
|
|
|
* Then when you click on each tab, only the container with the corresponding tab id will become visible. |
17
|
|
|
* |
18
|
|
|
* You can use Tabs like this: |
19
|
|
|
* |
20
|
|
|
* ```php |
21
|
|
|
* echo Tabs::widget([ |
22
|
|
|
* 'items' => [ |
23
|
|
|
* [ |
24
|
|
|
* 'label' => 'Home', |
25
|
|
|
* 'content' => 'Home content...', |
26
|
|
|
* 'active' => true |
27
|
|
|
* ], |
28
|
|
|
* [ |
29
|
|
|
* 'label' => 'Profile', |
30
|
|
|
* 'content' => 'Profile content...' |
31
|
|
|
* ], |
32
|
|
|
* [ |
33
|
|
|
* 'label' => 'Messages', |
34
|
|
|
* 'content' => 'Messages content...', |
35
|
|
|
* 'disabled' => true |
36
|
|
|
* ], |
37
|
|
|
* [ |
38
|
|
|
* 'label' => 'Google', |
39
|
|
|
* 'url' => 'http://www.google.lv' |
40
|
|
|
* ], |
41
|
|
|
* [ |
42
|
|
|
* 'label' => 'Hidden', |
43
|
|
|
* 'content' => 'Hidden content...', |
44
|
|
|
* 'visible' => false |
45
|
|
|
* ], |
46
|
|
|
* ] |
47
|
|
|
* ]); |
48
|
|
|
* ``` |
49
|
|
|
* @property bool $initializePlugin |
50
|
|
|
* |
51
|
|
|
* @see https://materializecss.com/tabs.html |
52
|
|
|
* @package widgets |
53
|
|
|
*/ |
54
|
|
|
class Tabs extends Widget |
55
|
|
|
{ |
56
|
|
|
/** |
57
|
|
|
* @var array list of tabs in the tabs widget. Each array element represents a single |
58
|
|
|
* tab with the following structure: |
59
|
|
|
* |
60
|
|
|
* - label: string, required, the tab header label. |
61
|
|
|
* - encode: boolean, optional, whether this label should be HTML-encoded. This param will override |
62
|
|
|
* global `$this->encodeLabels` param. |
63
|
|
|
* - headerOptions: array, optional, the HTML attributes of the tab header. |
64
|
|
|
* - linkOptions: array, optional, the HTML attributes of the tab header link tags. |
65
|
|
|
* - content: string, optional, the content (HTML) of the tab pane. |
66
|
|
|
* - url: string, optional, an external URL. When this is specified, clicking on this tab will bring |
67
|
|
|
* the browser to this URL |
68
|
|
|
* - options: array, optional, the HTML attributes of the tab pane container. |
69
|
|
|
* - active: boolean, optional, whether this item tab header and pane should be active. If no item is marked as |
70
|
|
|
* 'active' explicitly - the first one will be activated. |
71
|
|
|
* - visible: boolean, optional, whether the item tab header and pane should be visible or not. Defaults to true. |
72
|
|
|
* - disabled: boolean, optional, whether the item tab header and pane should be disabled or not. Defaults to false. |
73
|
|
|
*/ |
74
|
|
|
public $items = []; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @var array list of HTML attributes for the header container tags. This will be overwritten |
78
|
|
|
* by the "headerOptions" set in individual [[items]]. |
79
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
80
|
|
|
*/ |
81
|
|
|
public $headerOptions = []; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @var array list of HTML attributes for the tab header link tags. This will be overwritten |
85
|
|
|
* by the "linkOptions" set in individual [[items]]. |
86
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
87
|
|
|
*/ |
88
|
|
|
public $linkOptions = []; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @var array list of HTML attributes for the item container tags. This will be overwritten |
92
|
|
|
* by the "options" set in individual [[items]]. The following special options are recognized: |
93
|
|
|
* |
94
|
|
|
* - tag: string, defaults to "div", the tag name of the item container tags. |
95
|
|
|
* |
96
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
97
|
|
|
*/ |
98
|
|
|
public $itemOptions = []; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @var array the HTML attributes for the tab container tag. |
102
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
103
|
|
|
*/ |
104
|
|
|
public $containerOptions = []; |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @var array the HTML attributes for the tab content container tag. |
108
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
109
|
|
|
*/ |
110
|
|
|
public $contentOptions = []; |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @var boolean whether to render the `tab-content` container and its content. You may set this property |
114
|
|
|
* to be false so that you can manually render `tab-content` yourself in case your tab contents are complex. |
115
|
|
|
*/ |
116
|
|
|
public $renderTabContent = true; |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @var boolean whether the labels for header items should be HTML-encoded. |
120
|
|
|
*/ |
121
|
|
|
public $encodeLabels = true; |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @var boolean whether the tab width for header items should be fixed. |
125
|
|
|
*/ |
126
|
|
|
public $fixedWidth = false; |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @var boolean whether the tab background should be transparent. |
130
|
|
|
*/ |
131
|
|
|
public $transparent = false; |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @var boolean whether the tab will be rendered inside card |
135
|
|
|
*/ |
136
|
|
|
public $insideCard = false; |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Initializes the widget. |
140
|
|
|
*/ |
141
|
|
|
public function init() |
142
|
|
|
{ |
143
|
|
|
parent::init(); |
144
|
|
|
|
145
|
|
|
Html::addCssClass($this->options, ['widget' => 'tabs']); |
146
|
|
|
|
147
|
|
|
if ($this->fixedWidth) { |
148
|
|
|
Html::addCssClass($this->options, ['width' => 'tabs-fixed-width']); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
if ($this->transparent) { |
152
|
|
|
Html::addCssClass($this->options, ['transparent' => 'tabs-transparent']); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Renders the widget. |
158
|
|
|
*/ |
159
|
|
|
public function run() |
160
|
|
|
{ |
161
|
|
|
$this->initializePlugin = true; |
162
|
|
|
$this->registerPlugin('tabs'); |
163
|
|
|
return $this->renderItems(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Renders tab items as specified on [[items]]. |
168
|
|
|
* |
169
|
|
|
* @return string the rendering result. |
170
|
|
|
* @throws InvalidConfigException. |
171
|
|
|
*/ |
172
|
|
|
protected function renderItems() |
173
|
|
|
{ |
174
|
|
|
$headers = []; |
175
|
|
|
$panes = []; |
176
|
|
|
|
177
|
|
|
if (!$this->hasActiveTab() && !empty($this->items)) { |
178
|
|
|
$this->items[0]['active'] = true; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
foreach ($this->items as $index => $item) { |
182
|
|
|
list($header, $content) = $this->renderItem($index, $item); |
183
|
|
|
|
184
|
|
|
$headers[] = $header; |
185
|
|
|
|
186
|
|
|
if (!empty($content)) { |
187
|
|
|
$panes[] = $content; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
if ($this->insideCard) { |
192
|
|
|
Html::addCssClass($this->containerOptions, 'card-tabs'); |
193
|
|
|
Html::addCssClass($this->contentOptions, 'card-content'); |
194
|
|
|
} else { |
195
|
|
|
Html::addCssClass($this->contentOptions, 'tab-content'); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
$html = $this->insideCard |
199
|
|
|
? "\n" . Html::beginTag('div', $this->containerOptions) |
200
|
|
|
: ''; |
201
|
|
|
$html .= Html::tag('ul', implode("\n", $headers), $this->options); |
202
|
|
|
$html .= $this->insideCard ? "\n" . Html::endTag('div') : ''; |
203
|
|
|
$html .= $this->renderTabContent |
204
|
|
|
? "\n" . Html::tag('div', implode("\n", $panes), $this->contentOptions) |
205
|
|
|
: ''; |
206
|
|
|
|
207
|
|
|
return $html; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Renders single tab item as specified on [[items]]. |
212
|
|
|
* |
213
|
|
|
* @param int $index tab serial number |
214
|
|
|
* @param array $item single tab element |
215
|
|
|
* @return array returns array containing tab header and content [header, content] |
216
|
|
|
* @throws InvalidConfigException. |
217
|
|
|
*/ |
218
|
|
|
protected function renderItem($index, $item) |
219
|
|
|
{ |
220
|
|
|
$header = null; |
221
|
|
|
$content = null; |
222
|
|
|
|
223
|
|
|
if (!array_key_exists('label', $item)) { |
224
|
|
|
throw new InvalidConfigException("The 'label' option is required."); |
225
|
|
|
} elseif (ArrayHelper::remove($item, 'visible', true)) { |
226
|
|
|
$encodeLabel = isset($item['encode']) ? $item['encode'] : $this->encodeLabels; |
227
|
|
|
$label = $encodeLabel ? Html::encode($item['label']) : $item['label']; |
228
|
|
|
$headerOptions = array_merge($this->headerOptions, ArrayHelper::getValue($item, 'headerOptions', [])); |
229
|
|
|
$linkOptions = array_merge($this->linkOptions, ArrayHelper::getValue($item, 'linkOptions', [])); |
230
|
|
|
$options = array_merge($this->itemOptions, ArrayHelper::getValue($item, 'options', [])); |
231
|
|
|
$options['id'] = ArrayHelper::getValue($options, 'id', $this->options['id'] . '-tab' . $index); |
232
|
|
|
|
233
|
|
|
if (!$this->insideCard) { |
234
|
|
|
Html::addCssClass($options, 'col s12'); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
// Add active tab and content |
238
|
|
|
if (ArrayHelper::remove($item, 'active')) { |
239
|
|
|
Html::addCssClass($options, 'active'); |
240
|
|
|
Html::addCssClass($headerOptions, ['active', 'tab']); |
241
|
|
|
} else { |
242
|
|
|
Html::addCssClass($headerOptions, 'tab'); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
// Add disabled tab and content |
246
|
|
|
if (ArrayHelper::remove($item, 'disabled')) { |
247
|
|
|
Html::addCssClass($headerOptions, 'disabled'); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
// Add tab header |
251
|
|
|
if (isset($item['url'])) { |
252
|
|
|
$linkOptions['target'] = isset($linkOptions['target']) ? $linkOptions['target'] : '_self'; |
253
|
|
|
$header = Html::a($label, $item['url'], $linkOptions); |
254
|
|
|
} else { |
255
|
|
|
$header = Html::a($label, '#' . $options['id'], $linkOptions); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
// Add tab content |
259
|
|
|
if ($this->renderTabContent) { |
260
|
|
|
$tag = ArrayHelper::remove($options, 'tag', 'div'); |
261
|
|
|
$content = Html::tag($tag, isset($item['content']) ? $item['content'] : '', $options); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
$header = Html::tag('li', $header, $headerOptions); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
return [$header, $content]; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Searches for active tab value in [[items]] |
272
|
|
|
* @return boolean if there's active tab defined |
273
|
|
|
*/ |
274
|
|
|
protected function hasActiveTab() |
275
|
|
|
{ |
276
|
|
|
foreach ($this->items as $item) { |
277
|
|
|
if (isset($item['active']) && $item['active'] === true) { |
278
|
|
|
return true; |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
return false; |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|