-
Notifications
You must be signed in to change notification settings - Fork 3
/
IconBar.php
177 lines (158 loc) · 4.4 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
/**
* @link http://www.linchpinstudios.com/
* @copyright Copyright (c) 2014 Linchpin Studios LLC
* @license http://opensource.org/licenses/MIT
*/
namespace linchpinstudios\foundation;
use yii\helpers\Html;
use yii\base\InvalidConfigException;
use yii\helpers\ArrayHelper;
/**
* An Icon Bar provides a menu to quickly navigate an app. Use the Icon Bar
* horizontally or vertically, with the labels below the icons or to the
* right. Have it your way.
*
* For example,
*
* ```php
* // a button group using Dropdown widget
* echo IconBar::widget([
* 'direction' => 'vertical',
* 'labelRight' => true,
* 'icons' => [
* 'items' => [
* ['label' => 'One', 'url' => '/', 'icon' => 'fi-address-book'],
* ['label' => 'Two', 'url' => '#', 'icon' => 'fi-alert'],
* ],
* ],
* ]);
* ```
* @see http://foundation.zurb.com/docs/components/icon-bar.html
* @see http://zurb.com/playground/foundation-icon-fonts-3
* @author Josh Hagel <joshhagel@linchpinstudios.com>
* @since 0.1
*/
class IconBar extends \yii\base\Widget
{
/**
* @var string the bar direction
*/
public $direction = 'horizontal';
/**
* @var array the configuration array for [[icons]].
*/
public $icons = [];
/**
* @var intiger the number of icons to display
*/
public $iconsUp = 0;
/**
* @var boolean whether the label be positioned on the right.
*/
public $labelRight = false;
/**
* @var boolean whether the label should be HTML-encoded.
*/
public $encodeLabel = true;
public $options = [];
/**
* run function.
*
* @access public
* @return void
*/
public function run()
{
$classes = $this->generateClass();
echo Html::beginTag('div', ['class' => $classes])."\n";
echo $this->renderItems()."\n";
echo Html::endTag('div')."\n";
}
/**
* generateClass function.
*
* @access private
* @return void
*/
private function generateClass()
{
$class[] = 'icon-bar';
if($this->iconsUp === 0){
$class[] = $this->getCountUp(count($this->icons['items']));
}else{
$class[] = $this->getCountUp($this->iconsUp);
}
if($this->direction != 'horizontal'){
$class[] = $this->direction;
}
if($this->labelRight){
$class[] = 'label-right';
}
return implode(" ", $class);
}
/**
* getCountUp function.
*
* @access private
* @param mixed $itemCount
* @return void
*/
private function getCountUp($itemCount){
if($itemCount > 6){
return 'six-up';
}else{
switch($itemCount){
case 1:
return 'one-up';
break;
case 2:
return 'two-up';
break;
case 3:
return 'three-up';
break;
case 4:
return 'four-up';
break;
case 5:
return 'five-up';
break;
case 6:
return 'six-up';
break;
default:
return '';
}
}
}
/**
* renderItems function.
*
* @access private
* @return void
*/
private function renderItems()
{
$items = [];
$icons = $this->icons['items'];
foreach($icons as $i){
$labelContent = $i['label'];
if ($this->encodeLabel) {
$labelContent = Html::encode($labelContent);
}
$label = Html::tag('label',$labelContent)."\n";
$icon = Html::tag('i','',['class'=>$i['icon']])."\n";
$options = ArrayHelper::getValue($i, 'options', []);
if(isset($options['class'])){
$addClass = 'item '.$options['class'];
$options['class'] = $addClass;
}else{
$options['class'] = 'item';
}
$url = $i['url'];
$items[] = Html::a($icon.$label,$url,$options)."\n";
}
return implode("\n", $items);
}
}