-
Notifications
You must be signed in to change notification settings - Fork 33
/
EBootstrapTabNavigation.php
144 lines (123 loc) · 3.98 KB
/
EBootstrapTabNavigation.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
<?php
Yii::import('zii.widgets.CMenu');
/**
* Wrapper for the tab navigation
* http://twitter.github.com/bootstrap/components.html#navs
*
* @author Tim Helfensdörfer <[email protected]>
* @version 0.3.0
* @package bootstrap.widgets.tabs
*/
class EBootstrapTabNavigation extends CMenu {
/**
* Style of the tabs
*/
public $pills = false;
/**
* Stacked navigation
*/
public $stacked = false;
/**
* JS File to tab through the pages
*
* If its set to false, no file will be included
*/
public $jsFile = null;
/**
* Init widget
*/
public function init() {
parent::init();
EBootstrap::mergeClass($this->htmlOptions, array('nav'));
if ($this->pills)
EBootstrap::mergeClass($this->htmlOptions, array('nav-pills'));
else
EBootstrap::mergeClass($this->htmlOptions, array('nav-tabs'));
if ($this->stacked)
EBootstrap::mergeClass($this->htmlOptions, array('nav-stacked'));
Yii::app()->clientScript->registerCoreScript('jquery');
if (is_null($this->jsFile)) {
$jsFile = dirname(__FILE__).'/js/bootstrap.min.js';
$this->jsFile = Yii::app()->getAssetManager()->publish($jsFile);
Yii::app()->clientScript->registerScriptFile($this->jsFile);
}
}
/**
* Render navigation menu
*
* @param array $items
*/
public function renderMenu($items) {
if (count($items)) {
echo EBootstrap::openTag('ul', $this->htmlOptions)."\n";
$this->renderMenuRecursive($items);
echo EBootstrap::closeTag('ul')."\n";
}
}
/**
* Render navigation menu items
*
* @param array $items
* @param bool $sub If a submenu should be rendered
*/
protected function renderMenuRecursive($items, $sub = false) {
$count=0;
$first = true;
$n=count($items);
foreach($items as $item) {
$count++;
$options=isset($item['itemOptions']) ? $item['itemOptions'] : array();
$class=array();
if($item['active'] && $this->activeCssClass!='')
$class[]=$this->activeCssClass;
if($count===1 && $this->firstItemCssClass!==null)
$class[]=$this->firstItemCssClass;
if($count===$n && $this->lastItemCssClass!==null)
$class[]=$this->lastItemCssClass;
if($this->itemCssClass!==null)
$class[]=$this->itemCssClass;
if($class!==array()) {
if(empty($options['class']))
$options['class']=implode(' ',$class);
else
$options['class'].=' '.implode(' ',$class);
}
if(isset($this->itemTemplate) || isset($item['template']))
$template = isset($item['template']) ? $item['template'] : $this->itemTemplate;
else
$template = '';
if(isset($item['items']) && count($item['items'])) {
EBootstrap::mergeClass($options, array('dropdown'));
$item['linkOptions'] = (isset($item['linkOptions'])) ? $item['linkOptions'] : array();
EBootstrap::mergeClass($item['linkOptions'], array('dropdown-toggle'));
$item['linkOptions']['data-toggle'] = 'dropdown';
$item['label'] .= '<b class="caret"></b>';
}
else {
if ($this->pills)
$item['linkOptions']['data-toggle'] = 'pill';
else
$item['linkOptions']['data-toggle'] = 'tab';
}
echo EBootstrap::openTag('li', $options);
$menu=$this->renderMenuItem($item);
if(!empty($template)) {
echo strtr($template,array('{menu}'=>$menu));
}
else
echo $menu;
if(isset($item['items']) && count($item['items'])) {
$options = isset($item['submenuOptions']) ? $item['submenuOptions'] : $this->submenuHtmlOptions;
if (isset($options['class']))
$options['class'] = implode(' ', explode(' ', $options['class'])+array('dropdown-menu'));
else
$options['class'] = 'dropdown-menu';
echo "\n".EBootstrap::openTag('ul', $options)."\n";
$this->renderMenuRecursive($item['items'], true);
echo EBootstrap::closeTag('ul')."\n";
}
echo EBootstrap::closeTag('li')."\n";
}
}
}
?>