-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSwitcher.php
158 lines (135 loc) · 3.92 KB
/
Switcher.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
<?php
namespace demogorgorn\uikit;
use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use demogorgorn\uikit\SubNav;
/**
* Switcher renders a Switcher UIKit component, Dynamically transition through different content panes.
*
* For example SubNav mode:
*
* ```php
* echo Switcher::widget([
* 'navOptions' => [
* 'class' => 'uk-subnav-pill',
* ],
* 'items' => [
* [
* 'label' => 'Tab1',
* 'content' => 'Big text is written here!',
* 'itemOptions' => [
* 'class' => 'uk-animation-fade',
* ],
* ],
* [
* 'label' => 'Tab2',
* 'content' => 'Hello',
* 'itemOptions' => [
* 'class' => 'uk-animation-slide-left',
* ],
* ],
* [
* 'label' => 'Tab3',
* 'content' => 'Lorem ipsum...',
* ],
* ],
*
* ]);
* ```
*
* Please note the 'options' array option is connected with panels part of widget.
*
* @see http://www.getuikit.com/docs/switcher.html
*
* @author Oleg Martemjanov <[email protected]>
* @since 2.0
*/
class Switcher extends Widget
{
/**
* @var array list of items in the description list widget. Each array element represents a single
* item which can be an array with the following structure:
*
* - label: string, required, the title of the nav item.
* - url: string, not required, nav required.
* - content: string, required, panel content.
* - itemOptions: array, optional, the HTML attributes of the panel's item (LI).
*/
public $items = [];
/**
* @var bool to use SubNav or Tab component
*/
public $useSubNav = true;
/**
* @var array the HTML attributes of the nav bar
*/
public $navOptions = [];
/**
* Initializes the widget.
*/
public function init()
{
parent::init();
$this->initOptions();
}
/**
* Renders the widget.
*/
public function run()
{
echo $this->renderItems();
$this->registerAsset();
}
/**
* Renders a Nav bar.
*/
public function renderItems()
{
$navItems = [];
$panelItems = [];
foreach ($this->items as $i => $item) {
if (!isset($item['label']))
throw new InvalidConfigException("Option 'content' can't be empty!");
$label = $item['label'];
$url = isset($item['url']) ? $item['url'] : '#';
$navItems[] = [
'label' => $label,
'url' => $url,
];
if (!isset($item['content']))
throw new InvalidConfigException("Option 'content' can't be empty!");
$panelItems[] = Html::tag('li', $item['content'], isset($item['itemOptions']) ? $item['itemOptions'] : []);
}
echo $this->renderNav($navItems);
echo $this->renderPanels($panelItems);
}
/**
* Renders Nav.
*/
public function renderNav($navItems)
{
return SubNav::widget([
'items' => $navItems,
'options' => $this->navOptions,
]);
}
/**
* Renders Content.
*/
public function renderPanels($panelItems)
{
return Html::tag('ul', implode("\n", $panelItems), $this->options);
}
/**
* Initializes the widget options.
* This method sets the default values for various options.
*/
protected function initOptions()
{
// Panels options
Html::addCssClass($this->options, 'uk-switcher');
$this->navOptions['data-uk-switcher'] = \yii\helpers\Json::encode(['connect' => '#' . $this->options['id'] ]);
}
}