1: <?php
2: /**
3: * CJuiTabs class file.
4: *
5: * @author Sebastian Thierer <sebathi@gmail.com>
6: * @link http://www.yiiframework.com/
7: * @copyright 2008-2013 Yii Software LLC
8: * @license http://www.yiiframework.com/license/
9: */
10:
11: Yii::import('zii.widgets.jui.CJuiWidget');
12:
13: /**
14: * CJuiTabs displays a tabs widget.
15: *
16: * CJuiTabs encapsulates the {@link http://jqueryui.com/tabs/ JUI tabs}
17: * plugin.
18: *
19: * To use this widget, you may insert the following code in a view:
20: * <pre>
21: * $this->widget('zii.widgets.jui.CJuiTabs',array(
22: * 'tabs'=>array(
23: * 'StaticTab 1'=>'Content for tab 1',
24: * 'StaticTab 2'=>array('content'=>'Content for tab 2', 'id'=>'tab2'),
25: * // panel 3 contains the content rendered by a partial view
26: * 'AjaxTab'=>array('ajax'=>$ajaxUrl),
27: * ),
28: * // additional javascript options for the tabs plugin
29: * 'options'=>array(
30: * 'collapsible'=>true,
31: * ),
32: * ));
33: * </pre>
34: *
35: * By configuring the {@link options} property, you may specify the options
36: * that need to be passed to the JUI tabs plugin. Please refer to
37: * the {@link http://api.jqueryui.com/tabs/ JUI Tabs API} documentation
38: * for possible options (name-value pairs) and
39: * {@link http://jqueryui.com/tabs/ JUI Tabs page} for general
40: * description and demo.
41: *
42: * Note, in case you're using <base/> HTML tag you may run into the
43: * issue when jQuery UI uses altered base URL to load content, but not
44: * the base URL content was loaded from. (Developer may expect both behavior
45: * in different cases.) For this occasion consider using absolute URL
46: * generation as follows:
47: *
48: * <pre>
49: * $this->widget('zii.widgets.jui.CJuiTabs',array(
50: * 'tabs'=>array(
51: * 'Dynamic Tab'=>array('ajax'=>$this->createAbsoluteUrl('tab/content/route')),
52: * ),
53: * ));
54: * </pre>
55: *
56: * @author Sebastian Thierer <sebathi@gmail.com>
57: * @package zii.widgets.jui
58: * @since 1.1
59: */
60: class CJuiTabs extends CJuiWidget
61: {
62: /**
63: * @var array list of tabs (tab title=>tab content).
64: * Note that the tab title will not be HTML-encoded.
65: * The tab content can be either a string or an array. When it is an array, it can
66: * be in one of the following two formats:
67: * <pre>
68: * array('id'=>'myTabID', 'content'=>'tab content')
69: * array('id'=>'myTabID', 'ajax'=>URL)
70: * </pre>
71: * where the 'id' element is optional. The second format allows the tab content
72: * to be dynamically fetched from the specified URL via AJAX. The URL can be either
73: * a string or an array. If an array, it will be normalized into a URL using {@link CHtml::normalizeUrl}.
74: */
75: public $tabs=array();
76: /**
77: * @var string the name of the container element that contains all panels. Defaults to 'div'.
78: */
79: public $tagName='div';
80: /**
81: * @var string the template that is used to generated every panel title.
82: * The token "{title}" in the template will be replaced with the panel title and
83: * the token "{url}" will be replaced with "#TabID" or with the url of the ajax request.
84: */
85: public $headerTemplate='<li><a href="{url}" title="{title}">{title}</a></li>';
86: /**
87: * @var string the template that is used to generated every tab content.
88: * The token "{content}" in the template will be replaced with the panel content
89: * and the token "{id}" with the tab ID.
90: */
91: public $contentTemplate='<div id="{id}">{content}</div>';
92:
93: /**
94: * Run this widget.
95: * This method registers necessary javascript and renders the needed HTML code.
96: */
97: public function run()
98: {
99: $id=$this->getId();
100: if(isset($this->htmlOptions['id']))
101: $id=$this->htmlOptions['id'];
102: else
103: $this->htmlOptions['id']=$id;
104:
105: echo CHtml::openTag($this->tagName,$this->htmlOptions)."\n";
106:
107: $tabsOut="";
108: $contentOut="";
109: $tabCount=0;
110:
111: foreach($this->tabs as $title=>$content)
112: {
113: $tabId=(is_array($content) && isset($content['id']))?$content['id']:$id.'_tab_'.$tabCount++;
114:
115: if(!is_array($content))
116: {
117: $tabsOut.=strtr($this->headerTemplate,array('{title}'=>$title,'{url}'=>'#'.$tabId,'{id}'=>'#'.$tabId))."\n";
118: $contentOut.=strtr($this->contentTemplate,array('{content}'=>$content,'{id}'=>$tabId))."\n";
119: }
120: elseif(isset($content['ajax']))
121: {
122: $tabsOut.=strtr($this->headerTemplate,array('{title}'=>$title,'{url}'=>CHtml::normalizeUrl($content['ajax']),'{id}'=>'#'.$tabId))."\n";
123: }
124: else
125: {
126: $tabsOut.=strtr($this->headerTemplate,array('{title}'=>$title,'{url}'=>'#'.$tabId,'{id}'=>$tabId))."\n";
127: if(isset($content['content']))
128: $contentOut.=strtr($this->contentTemplate,array('{content}'=>$content['content'],'{id}'=>$tabId))."\n";
129: }
130: }
131: echo "<ul>\n".$tabsOut."</ul>\n";
132: echo $contentOut;
133: echo CHtml::closeTag($this->tagName)."\n";
134:
135: $options=CJavaScript::encode($this->options);
136: Yii::app()->getClientScript()->registerScript(__CLASS__.'#'.$id,"jQuery('#{$id}').tabs($options);");
137: }
138: }