-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathToggle.php
119 lines (102 loc) · 2.98 KB
/
Toggle.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 demogorgorn\uikit;
use Yii;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use demogorgorn\uikit\Button;
/**
* Toggle renders a Toggle UIKit component.
* Hide, switch or change the appearence of different contents through a toggle.
*
* For example SubNav mode:
*
* ```php
* echo Toggle::widget([
* 'caption' => 'Toggle',
* 'items' => [
* [
* 'content' => 'Big text is written here!',
* 'itemOptions' => [
* 'class' => 'uk-animation-fade',
* ],
* ],
* [
* 'content' => 'Lorem ipsum...',
* 'hidden' => true,
* ],
* ],
* ]);
* ```
*
* Please note the 'options' array option is connected with button part of widget.
*
* @see http://www.getuikit.com/docs/toggle.html
*
* @author Oleg Martemjanov <[email protected]>
* @since 2.0
*/
class Toggle extends Widget
{
/**
* @var array list of items in toggle widget. Each array element represents a single
* item which can be an array with the following structure:
*
* - content: string, required, toggle panel content.
* - hidden: bool, not required, specify if the item is hidden by default.
* - itemOptions: array, optional, the HTML attributes of the panel's item (DIV).
*/
public $items = [];
/**
* @var string the toggle button caption.
*/
public $caption;
/**
* Initializes the widget.
*/
public function init()
{
parent::init();
echo $this->renderToggleButton();
}
/**
* Renders the widget.
*/
public function run()
{
$this->renderItems();
$this->registerAsset();
}
/**
* Renders a toggle button.
*/
public function renderToggleButton()
{
if (!isset($this->caption))
throw new InvalidConfigException("Option 'caption' can't be empty!");
// Nav options
$this->options = array_merge([
'data-uk-toggle' => "{target:'.{$this->options['id']}'}",
], $this->options);
return Button::widget([
'options' => $this->options,
]);
}
/**
* Render items.
*/
public function renderItems()
{
if (count($this->items) > 2)
throw new InvalidConfigException("There can not be specified more than two items!");
foreach ($this->items as $i => $item) {
if (!isset($item['content']))
throw new InvalidConfigException("Option 'content' can't be empty!");
$itemOptions = isset($item['itemOptions']) ? $item['itemOptions'] : [];
if (isset($item['hidden']) && ($item['hidden'] === true))
Html::addCssClass($itemOptions, 'uk-hidden');
Html::addCssClass($itemOptions, $this->options['id']);
echo Html::tag('div', $item['content'], $itemOptions) . "\n";
}
}
}