forked from nonzod/yii2-foundation
-
Notifications
You must be signed in to change notification settings - Fork 1
/
IconBar.php
143 lines (123 loc) · 3.12 KB
/
IconBar.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
<?php
/**
* @link http://foundationize.com
* @package foundationize/yii2-foundation
* @version 1.0.0
*/
namespace foundationize\foundation;
use foundationize\foundation\helpers\Html;
use yii\helpers\ArrayHelper;
use yii\base\InvalidConfigException;
/**
* Description of IconBar
*
*/
class IconBar extends Widget {
/**
*
* @var type array
* 'icon' => string|array
* string: the icons name without prefix
* array: params for [[foundationize\foundation\helpers\Html::Icon]] object (['name' => '', 'options' => [], 'prefix' => '', 'tag' => ''])
* 'img' => 'path/to/img'
* ignored if 'icon' is set
* 'label' => 'label'
* 'url' => 'url'
* 'options' => []
*
* examples:
*
* Only icon's name
* ~~~
* 'items' => [
* ['icon' => 'home']
* ]
* ~~~
*
* Icon's with parameters
* ~~~
* 'items' => [
* [
* 'icon' => [
* 'name' => 'trash',
* 'options' => ['class' => 'icon-class']
* ],
* 'url' => ['site/index']
* ]
* ]
* ~~~
*
* Icon as img
*
* ~~~
* 'items' => [
* ['img' => '/images/fi-info.svg', 'url' => ['site/index']]
* ]
* ~~~
*
*
* @see [[foundationize\foundation\helpers\Html::icon()]]
*/
public $items = [];
/**
*
* @var boolean
*/
public $encodeLabels = true;
/**
*
*/
public function init() {
parent::init();
Html::addCssClass($this->options, 'icon-bar');
if (empty($this->options['role'])) {
$this->options['role'] = 'navigation';
}
FoundationIconAsset::register($this->getView());
}
/**
*
*/
public function run() {
echo Html::beginTag('div', $this->options);
echo implode("\n", $this->renderItems());
echo Html::endTag('div');
}
/**
*
* @return type
*/
public function renderItems() {
$out = [];
foreach ($this->items as $i => $item) {
if (!empty($item['label'])) {
$encodeLabel = isset($item['encode']) ? $item['encode'] : $this->encodeLabels;
$label = $encodeLabel ? Html::encode($item['label']) : $item['label'];
$label = Html::tag('label', $label);
} else {
$label = '';
}
$options = ArrayHelper::getValue($item, 'options', []);
$url = ArrayHelper::getValue($item, 'url', '#');
if (isset($item['icon'])) {
if (is_array($item['icon'])) {
$iconOptions = ArrayHelper::getValue($item['icon'], 'options', []);
$iconPrefix = ArrayHelper::getValue($item['icon'], 'prefix', 'fi-');
$iconTag = ArrayHelper::getValue($item['icon'], 'tag', 'i');
$icon = Html::icon($item['icon']['name'], $iconOptions, $iconPrefix, $iconTag);
} else {
$icon = Html::icon($item['icon']);
}
} elseif (isset($item['img'])) {
$icon = Html::img($item['img']);
} else {
throw new InvalidConfigException("'icon' or 'img' must be set");
}
Html::addCssClass($options, 'item');
$options['role'] = 'button';
$options['tabindex'] = 0;
$out[] = Html::a($icon.$label, $url, $options);
}
return $out;
}
}