1: <?php
2: /**
3: * TbTabs class file.
4: * @author Antonio Ramirez <[email protected]>
5: * @copyright Copyright © Christoffer Niska 2013-
6: * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
7: * @package bootstrap.widgets
8: */
9:
10: Yii::import('bootstrap.behaviors.TbWidget');
11:
12: /**
13: * Boostrap tabs widget.
14: */
15: class TbTabs extends CWidget
16: {
17: /**
18: * @var string the type of tabs to display. Valid values are 'tabs' and 'pills' (defaults to 'tabs').
19: * @see TbHtml::$navStyles
20: */
21: public $type = TbHtml::NAV_TYPE_TABS;
22: /**
23: * @var string the placement of the tabs. Valid values are 'right, 'left' and 'below'.
24: * @see TbHtml::tabPlacements
25: */
26: public $placement;
27: /**
28: * @var array the tab configuration.
29: */
30: public $tabs = array();
31: /**
32: * @var array additional data submitted to the views.
33: */
34: public $viewData;
35: /**
36: * @var string a javascript function that This event fires on tab show, but before the new tab has been shown.
37: * Use `event.target` and `event.relatedTarget` to target the active tab and the previous active tab (if available)
38: * respectively.
39: */
40: public $onShow;
41: /**
42: * @var string a javascript function that fires on tab show after a tab has been shown. Use `event.target` and
43: * `event.relatedTarget` to target the active tab and the previous active tab (if available) respectively.
44: */
45: public $onShown;
46: /**
47: * @var array the HTML attributes for the widget container.
48: */
49: public $htmlOptions = array();
50: /**
51: * @var string[] the Javascript event handlers.
52: */
53: protected $events = array();
54:
55: /**
56: * Widget's initialization method
57: */
58: public function init()
59: {
60: $this->attachBehavior('TbWidget', new TbWidget);
61: $this->copyId();
62: TbArray::defaultValue('placement', $this->placement, $this->htmlOptions);
63: $this->initEvents();
64: }
65:
66: /**
67: * Initialize events if any
68: */
69: public function initEvents()
70: {
71: foreach (array('onShow', 'onShown') as $event) {
72: if ($this->$event !== null) {
73: $modalEvent = strtolower(substr($event, 2));
74: if ($this->$event instanceof CJavaScriptExpression) {
75: $this->events[$modalEvent] = $this->$event;
76: } else {
77: $this->events[$modalEvent] = new CJavaScriptExpression($this->$event);
78: }
79: }
80: }
81: }
82:
83: /**
84: * Widget's run method
85: */
86: public function run()
87: {
88: $this->tabs = $this->normalizeTabs($this->tabs);
89: echo TbHtml::tabbable($this->type, $this->tabs, $this->htmlOptions);
90: $this->registerClientScript();
91: }
92:
93: /**
94: * Normalizes the tab configuration.
95: * @param array $tabs a reference to the tabs tab configuration.
96: */
97: protected function normalizeTabs($tabs)
98: {
99: $controller = $this->getController();
100: if (isset($controller)) {
101: foreach ($tabs as &$tabOptions) {
102: $items = TbArray::getValue('items', $tabOptions, array());
103: if (!empty($items)) {
104: $tabOptions['items'] = $this->normalizeTabs($items);
105: } else {
106: if (isset($tabOptions['view'])) {
107: $view = TbArray::popValue('view', $tabOptions);
108: if ($controller->getViewFile($view) !== false) {
109: $tabOptions['content'] = $controller->renderPartial($view, $this->viewData, true);
110: }
111: }
112: }
113: }
114: }
115: return $tabs;
116: }
117:
118: /**
119: * Registers necessary client scripts.
120: */
121: public function registerClientScript()
122: {
123: $selector = '#' . $this->htmlOptions['id'];
124: Yii::app()->clientScript->registerScript(__CLASS__ . $selector, "jQuery('{$selector}').tab('show');");
125: $this->registerEvents($selector, $this->events);
126: }
127: }