1 | <?php |
||
18 | class TbNavbar extends CWidget { |
||
19 | |||
20 | const CONTAINER_PREFIX = 'yii_booster_collapse_'; |
||
21 | |||
22 | // Navbar types. |
||
23 | const TYPE_DEFAULT = 'default'; |
||
24 | const TYPE_INVERSE = 'inverse'; |
||
25 | |||
26 | // Navbar fix locations. |
||
27 | const FIXED_TOP = 'top'; |
||
28 | const FIXED_BOTTOM = 'bottom'; |
||
29 | |||
30 | /** |
||
31 | * @var string the navbar type. Valid values are 'inverse'. |
||
32 | * @since 1.0.0 |
||
33 | */ |
||
34 | public $type = self::TYPE_DEFAULT; |
||
35 | |||
36 | /** |
||
37 | * @var string the text for the brand. |
||
38 | */ |
||
39 | public $brand; |
||
40 | |||
41 | /** |
||
42 | * @var string the URL for the brand link. |
||
43 | */ |
||
44 | public $brandUrl; |
||
45 | |||
46 | /** |
||
47 | * @var array the HTML attributes for the brand link. |
||
48 | */ |
||
49 | public $brandOptions = array(); |
||
50 | |||
51 | /** |
||
52 | * @var array navigation items. |
||
53 | * @since 0.9.8 |
||
54 | */ |
||
55 | public $items = array(); |
||
56 | |||
57 | /** |
||
58 | * @var mixed fix location of the navbar if applicable. |
||
59 | * Valid values are 'top' and 'bottom'. Defaults to 'top'. |
||
60 | * Setting the value to false will make the navbar static. |
||
61 | * @since 0.9.8 |
||
62 | */ |
||
63 | public $fixed = self::FIXED_TOP; |
||
64 | |||
65 | /** |
||
66 | * @var boolean whether the nav span over the full width. Defaults to false. |
||
67 | * @since 0.9.8 |
||
68 | */ |
||
69 | public $fluid = false; |
||
70 | |||
71 | /** |
||
72 | * @var boolean whether to enable collapsing on narrow screens. Default to true. |
||
73 | */ |
||
74 | public $collapse = true; |
||
75 | |||
76 | /** |
||
77 | * @var array the HTML attributes for the widget container. |
||
78 | */ |
||
79 | public $htmlOptions = array(); |
||
80 | |||
81 | /** |
||
82 | * @var array the widget options for the collapsed toggle button. |
||
83 | */ |
||
84 | public $toggleButtonWidgetOptions = array(); |
||
85 | |||
86 | /** |
||
87 | *### .init() |
||
88 | * |
||
89 | * Initializes the widget. |
||
90 | */ |
||
91 | public function init() { |
||
92 | |||
93 | if ($this->brand !== false) { |
||
94 | if (!isset($this->brand)) { |
||
95 | $this->brand = CHtml::encode(Yii::app()->name); |
||
96 | } |
||
97 | |||
98 | if (!isset($this->brandUrl)) { |
||
99 | $this->brandUrl = Yii::app()->homeUrl; |
||
100 | } |
||
101 | |||
102 | $this->brandOptions['href'] = CHtml::normalizeUrl($this->brandUrl); |
||
103 | |||
104 | if (isset($this->brandOptions['class'])) { |
||
105 | $this->brandOptions['class'] .= ' navbar-brand'; |
||
106 | } else { |
||
107 | $this->brandOptions['class'] = 'navbar-brand'; |
||
108 | } |
||
109 | } |
||
110 | |||
111 | $classes = array('navbar'); |
||
112 | |||
113 | if (isset($this->type) && in_array($this->type, array(self::TYPE_DEFAULT, self::TYPE_INVERSE))) { |
||
114 | $classes[] = 'navbar-' . $this->type; |
||
115 | } else { |
||
116 | $classes[] = 'navbar-' . self::TYPE_DEFAULT; |
||
117 | } |
||
118 | |||
119 | if ($this->fixed !== false && in_array($this->fixed, array(self::FIXED_TOP, self::FIXED_BOTTOM))) { |
||
120 | $classes[] = 'navbar-fixed-' . $this->fixed; |
||
121 | } |
||
122 | |||
123 | if (!empty($classes)) { |
||
124 | $classes = implode(' ', $classes); |
||
125 | if (isset($this->htmlOptions['class'])) { |
||
126 | $this->htmlOptions['class'] .= ' ' . $classes; |
||
127 | } else { |
||
128 | $this->htmlOptions['class'] = $classes; |
||
129 | } |
||
130 | } |
||
131 | |||
132 | if ($this->collapse) { |
||
133 | if (!isset($this->toggleButtonWidgetOptions['label'])) { |
||
134 | $this->toggleButtonWidgetOptions['label'] = '<span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span>'; |
||
135 | } |
||
136 | if (!isset($this->toggleButtonWidgetOptions['encodeLabel'])) { |
||
137 | $this->toggleButtonWidgetOptions['encodeLabel'] = false; |
||
138 | } |
||
139 | if (!isset($this->toggleButtonWidgetOptions['htmlOptions'])) { |
||
140 | $this->toggleButtonWidgetOptions['htmlOptions'] = array( |
||
141 | 'class' => 'navbar-toggle', |
||
142 | 'data-toggle' => 'collapse', |
||
143 | 'data-target' => '#'.self::CONTAINER_PREFIX.$this->id, |
||
144 | ); |
||
145 | } |
||
146 | } |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | *### .run() |
||
151 | * |
||
152 | * Runs the widget. |
||
153 | */ |
||
154 | public function run() { |
||
155 | |||
156 | echo CHtml::openTag('nav', $this->htmlOptions); |
||
157 | echo '<div class="' . $this->getContainerCssClass() . '">'; |
||
158 | |||
159 | echo '<div class="navbar-header">'; |
||
160 | if($this->collapse) { |
||
161 | $this->controller->widget('booster.widgets.TbButton', $this->toggleButtonWidgetOptions); |
||
162 | } |
||
163 | |||
164 | if ($this->brand !== false) { |
||
165 | if ($this->brandUrl !== false) { |
||
166 | echo CHtml::openTag('a', $this->brandOptions) . $this->brand . '</a>'; |
||
167 | } else { |
||
168 | unset($this->brandOptions['href']); // spans cannot have a href attribute |
||
169 | echo CHtml::openTag('span', $this->brandOptions) . $this->brand . '</span>'; |
||
170 | } |
||
171 | } |
||
172 | echo '</div>'; |
||
173 | |||
174 | echo '<div class="collapse navbar-collapse" id="'.self::CONTAINER_PREFIX.$this->id.'">'; |
||
175 | foreach ($this->items as $item) { |
||
176 | if (is_string($item)) { |
||
177 | echo $item; |
||
178 | } else { |
||
179 | if (!isset($item['type'])) { |
||
180 | $item['type'] = 'navbar'; |
||
181 | } |
||
182 | |||
183 | if (isset($item['class'])) { |
||
184 | $className = $item['class']; |
||
185 | unset($item['class']); |
||
186 | |||
187 | $this->controller->widget($className, $item); |
||
188 | } |
||
189 | } |
||
190 | } |
||
191 | echo '</div></div></nav>'; |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | *### .getContainerCssClass() |
||
196 | * |
||
197 | * Returns the navbar container CSS class. |
||
198 | * @return string the class |
||
199 | */ |
||
200 | protected function getContainerCssClass() { |
||
204 | } |
||
205 |