-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dropdown.php
129 lines (114 loc) · 3.6 KB
/
Dropdown.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
<?php
/**
* @link http://foundationize.com
* @package shqear/yii2-foundation6
* @version 1.0.0
*/
namespace shqear\foundation6;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\base\InvalidConfigException;
/**
* Description of Dropdown
*/
class Dropdown extends Widget
{
/**
* @var array|string List of dropdown elements or html content
*
* if array set :
* - label: string, required, the label of the item link
* - url: string, optional, the url of the item link. Defaults to "#".
* - visible: boolean, optional, whether this menu item is visible. Defaults to true.
* - linkOptions: array, optional, the HTML attributes of the item link.
* - options: array, optional, the HTML attributes of the item.
*
* Use as a string for html content.
*/
public $items;
/**
* @var boolean whether the labels for header items should be HTML-encoded.
*/
public $encodeLabels = true;
/**
* @var array the HTML attributes for the widget container tag.
* @see \yii\helpers\Html::renderTagAttributes() for details on how attributes are being rendered.
*/
protected $_containerOptions = [];
/**
* Initializes the widget.
* If you override this method, make sure you call the parent implementation first.
*/
public function init()
{
parent::init();
Html::addCssClass($this->options, 'f-dropdown');
$this->options['data-dropdown-content'] = 1;
$this->options['tabindex'] = '-1';
$this->_containerOptions = $this->options;
}
/**
* Renders the widget.
*/
public function run()
{
echo $this->renderDropdown($this->items);
$this->registerPlugin('dropdown');
}
/**
*
* @param array $items
* @return string
*/
protected function renderDropdown($items)
{
if (is_string($items)) {
$items = $this->renderAsContent($items);
Html::addCssClass($this->_containerOptions, 'content');
$tag = 'div';
} else {
$items = $this->renderAsItems($items);
$tag = 'ul';
}
return Html::tag($tag, $items, $this->_containerOptions);
}
/**
* Renders menu items.
* @param array $items the menu items to be rendered
* @return string the rendering result.
* @throws InvalidConfigException if the label option is not specified in one of the items.
*/
protected function renderAsItems($items)
{
$lines = [];
foreach ($items as $i => $item) {
if (isset($item['visible']) && !$item['visible']) {
unset($items[$i]);
continue;
}
if (is_string($item)) {
$lines[] = $item;
continue;
}
if (!isset($item['label'])) {
throw new InvalidConfigException("The 'label' option is required.");
}
$encodeLabel = isset($item['encode']) ? $item['encode'] : $this->encodeLabels;
$label = $encodeLabel ? Html::encode($item['label']) : $item['label'];
$options = ArrayHelper::getValue($item, 'options', []);
$linkOptions = ArrayHelper::getValue($item, 'linkOptions', []);
$content = Html::a($label, ArrayHelper::getValue($item, 'url', '#'), $linkOptions);
$lines[] = Html::tag('li', $content, $options);
}
return implode("\n", $lines);
}
/**
*
* @param array $item
* @return string
*/
protected function renderAsContent($item)
{
return $item;
}
}