HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
TbTabs.php
Go to the documentation of this file.
1 <?php
10 Yii::import('booster.widgets.TbMenu');
11 
19 class TbTabs extends CWidget {
20 
21  // Tab placements.
22  const PLACEMENT_ABOVE = 'above';
23  const PLACEMENT_BELOW = 'below';
24  const PLACEMENT_LEFT = 'left';
25  const PLACEMENT_RIGHT = 'right';
26 
35 
41  public $placement;
42 
46  public $tabs = array();
47 
51  public $stacked = false;
52 
57  public $justified = false;
58 
62  public $encodeLabel = true;
63 
67  public $events = array();
68 
72  public $htmlOptions = array();
73 
77  public $tabContentHtmlOptions = array();
78 
82  public $tabMenuHtmlOptions = array();
83 
89  public function init() {
90 
91  if (!isset($this->htmlOptions['id'])) {
92  $this->htmlOptions['id'] = $this->getId();
93  }
94 
95  $classes = array();
96 
97  $validPlacements = array(
98  self::PLACEMENT_ABOVE,
99  self::PLACEMENT_BELOW,
100  self::PLACEMENT_LEFT,
101  self::PLACEMENT_RIGHT
102  );
103 
104  if (isset($this->placement) && in_array($this->placement, $validPlacements)) {
105  $classes[] = 'tabs-' . $this->placement;
106  }
107 
108  if (!empty($classes)) {
109  $classes = implode(' ', $classes);
110  if (isset($this->htmlOptions['class'])) {
111  $this->htmlOptions['class'] .= ' ' . $classes;
112  } else {
113  $this->htmlOptions['class'] = $classes;
114  }
115  }
116 
117  if (isset($this->tabContentHtmlOptions['class'])) {
118  $this->tabContentHtmlOptions['class'] .= ' tab-content';
119  } else {
120  $this->tabContentHtmlOptions['class'] = 'tab-content';
121  }
122  }
123 
129  public function run() {
130 
131  $id = $this->id;
132  $content = array();
133  $items = $this->normalizeTabs($this->tabs, $content);
134 
135  ob_start();
136  $this->controller->widget(
137  'booster.widgets.TbMenu',
138  array(
139  'stacked' => $this->stacked,
140  'justified' => $this->justified,
141  'type' => $this->type,
142  'encodeLabel' => $this->encodeLabel,
143  'htmlOptions' => $this->tabMenuHtmlOptions,
144  'items' => $items,
145  )
146  );
147  $tabs = ob_get_clean();
148 
149  ob_start();
150  echo CHtml::openTag('div', $this->tabContentHtmlOptions);
151  echo implode('', $content);
152  echo CHtml::closeTag('div');
153  $content = ob_get_clean();
154 
155  echo CHtml::openTag('div', $this->htmlOptions);
156  echo $this->placement === self::PLACEMENT_BELOW ? $content . $tabs : $tabs . $content;
157  echo CHtml::closeTag('div');
158 
160  $cs = Yii::app()->getClientScript();
161  $cs->registerScript(__CLASS__ . '#' . $id, "jQuery('#{$id}').tab('show');");
162 
163  foreach ($this->events as $name => $handler) {
164  $handler = CJavaScript::encode($handler);
165  $cs->registerScript(__CLASS__ . '#' . $id . '_' . $name, "jQuery('#{$id}').on('{$name}', {$handler});");
166  }
167  }
168 
180  protected function normalizeTabs($tabs, &$panes, &$i = 0) {
181 
182  $id = $this->getId();
183  $items = array();
184 
185  foreach ($tabs as $tab) {
186  $item = $tab;
187 
188  if (isset($item['visible']) && $item['visible'] === false) {
189  continue;
190  }
191 
192  if (!isset($item['itemOptions'])) {
193  $item['itemOptions'] = array();
194  }
195 
196  if (!isset($item['url'])) {
197  $item['linkOptions']['data-toggle'] = 'tab';
198  }
199 
200  if (isset($tab['items'])) {
201  $item['items'] = $this->normalizeTabs($item['items'], $panes, $i);
202  } else {
203  if (!isset($item['id'])) {
204  $item['id'] = $id . '_tab_' . ($i + 1);
205  }
206 
207  if (!isset($item['url'])) {
208  $item['url'] = '#' . $item['id'];
209  }
210 
211  if (!isset($item['content'])) {
212  $item['content'] = '';
213  }
214 
215  $content = $item['content'];
216  unset($item['content']);
217 
218  if (!isset($item['paneOptions'])) {
219  $item['paneOptions'] = array();
220  }
221 
222  $paneOptions = $item['paneOptions'];
223  unset($item['paneOptions']);
224 
225  $paneOptions['id'] = $item['id'];
226 
227  $classes = array('tab-pane fade');
228 
229  if (isset($item['active']) && $item['active']) {
230  $classes[] = 'active in';
231  }
232 
233  $classes = implode(' ', $classes);
234  if (isset($paneOptions['class'])) {
235  $paneOptions['class'] .= ' ' . $classes;
236  } else {
237  $paneOptions['class'] = $classes;
238  }
239 
240  $panes[] = CHtml::tag('div', $paneOptions, $content);
241 
242  $i++; // increment the tab-index
243  }
244 
245  $items[] = $item;
246  }
247  return $items;
248  }
249 }