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
|
|
|
* |
22
|
|
|
* ``` |
23
|
|
|
* @see https://materializecss.com/tabs.html |
24
|
|
|
* @package widgets |
25
|
|
|
*/ |
26
|
|
|
class Tabs extends Widget |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var array list of tabs in the tabs widget. Each array element represents a single |
30
|
|
|
* tab with the following structure: |
31
|
|
|
* |
32
|
|
|
* - label: string, required, the tab header label. |
33
|
|
|
* - encode: boolean, optional, whether this label should be HTML-encoded. This param will override |
34
|
|
|
* global `$this->encodeLabels` param. |
35
|
|
|
* - headerOptions: array, optional, the HTML attributes of the tab header. |
36
|
|
|
* - linkOptions: array, optional, the HTML attributes of the tab header link tags. |
37
|
|
|
* - content: string, optional, the content (HTML) of the tab pane. |
38
|
|
|
* - url: string, optional, an external URL. When this is specified, clicking on this tab will bring |
39
|
|
|
* the browser to this URL |
40
|
|
|
* - options: array, optional, the HTML attributes of the tab pane container. |
41
|
|
|
* - active: boolean, optional, whether this item tab header and pane should be active. If no item is marked as |
42
|
|
|
* 'active' explicitly - the first one will be activated. |
43
|
|
|
* - visible: boolean, optional, whether the item tab header and pane should be visible or not. Defaults to true. |
44
|
|
|
*/ |
45
|
|
|
public $items = []; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var array list of HTML attributes for the header container tags. This will be overwritten |
49
|
|
|
* by the "headerOptions" set in individual [[items]]. |
50
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
51
|
|
|
*/ |
52
|
|
|
public $headerOptions = []; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var array list of HTML attributes for the tab header link tags. This will be overwritten |
56
|
|
|
* by the "linkOptions" set in individual [[items]]. |
57
|
|
|
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered. |
58
|
|
|
*/ |
59
|
|
|
public $linkOptions = []; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var boolean whether to render the `tab-content` container and its content. You may set this property |
63
|
|
|
* to be false so that you can manually render `tab-content` yourself in case your tab contents are complex. |
64
|
|
|
*/ |
65
|
|
|
public $renderTabContent = true; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Initializes the widget. |
69
|
|
|
*/ |
70
|
|
|
public function init() |
71
|
|
|
{ |
72
|
|
|
parent::init(); |
73
|
|
|
Html::addCssClass($this->options, ['class' => 'tabs']); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Renders the widget. |
78
|
|
|
*/ |
79
|
|
|
public function run() |
80
|
|
|
{ |
81
|
|
|
$this->registerPlugin('tabs'); |
82
|
|
|
return $this->renderItems(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Renders tab items as specified on [[items]]. |
87
|
|
|
* |
88
|
|
|
* @return string the rendering result. |
89
|
|
|
* @throws InvalidConfigException. |
90
|
|
|
*/ |
91
|
|
|
protected function renderItems() |
92
|
|
|
{ |
93
|
|
|
$headers = []; |
94
|
|
|
$panes = []; |
95
|
|
|
|
96
|
|
|
if (!$this->hasActiveTab() && !empty($this->items)) { |
97
|
|
|
$this->items[0]['active'] = true; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
foreach ($this->items as $index => $item) { |
101
|
|
|
if (!array_key_exists('label', $item)) { |
102
|
|
|
throw new InvalidConfigException("The 'label' option is required."); |
103
|
|
|
} elseif (ArrayHelper::remove($item, 'visible', true)) { |
104
|
|
|
$encodeLabel = isset($item['encode']) ? $item['encode'] : $this->encodeLabels; |
|
|
|
|
105
|
|
|
$label = $encodeLabel ? Html::encode($item['label']) : $item['label']; |
|
|
|
|
106
|
|
|
$headerOptions = array_merge($this->headerOptions, ArrayHelper::getValue($item, 'headerOptions', [])); |
|
|
|
|
107
|
|
|
$linkOptions = array_merge($this->linkOptions, ArrayHelper::getValue($item, 'linkOptions', [])); |
|
|
|
|
108
|
|
|
$options = array_merge($this->itemOptions, ArrayHelper::getValue($item, 'options', [])); |
|
|
|
|
109
|
|
|
$options['id'] = ArrayHelper::getValue($options, 'id', $this->options['id'] . '-tab' . $index); |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
|
113
|
|
|
|
114
|
|
|
|
115
|
|
|
|
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$html = Html::tag('ul', implode("\n", $headers), $this->options); |
120
|
|
|
$html .= $this->renderTabContent |
121
|
|
|
? "\n" . Html::tag('div', implode("\n", $panes), ['class' => 'tab-content']) |
122
|
|
|
: ''; |
123
|
|
|
|
124
|
|
|
return $html; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Searches for active tab value in [[items]] |
129
|
|
|
* @return boolean if there's active tab defined |
130
|
|
|
*/ |
131
|
|
|
protected function hasActiveTab() |
132
|
|
|
{ |
133
|
|
|
foreach ($this->items as $item) { |
134
|
|
|
if (isset($item['active']) && $item['active'] === true) { |
135
|
|
|
return true; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return false; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|