~horux-dev/horux-webcli/thfo

« back to all changes in this revision

Viewing changes to yii/framework/zii/widgets/jui/CJuiTabs.php

  • Committer: Thierry Forchelet
  • Date: 2011-02-25 13:30:15 UTC
  • Revision ID: thierry.forchelet@letux.ch-20110225133015-zxyj9w7sqv8ly971
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * CJuiTabs class file.
 
4
 *
 
5
 * @author Sebastian Thierer <sebathi@gmail.com>
 
6
 * @link http://www.yiiframework.com/
 
7
 * @copyright Copyright &copy; 2008-2011 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/demos/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://jqueryui.com/demos/tabs/ JUI tabs} documentation
 
38
 * for possible options (name-value pairs).
 
39
 *
 
40
 * @author Sebastian Thierer <sebathi@gmail.com>
 
41
 * @version $Id: CJuiTabs.php 2799 2011-01-01 19:31:13Z qiang.xue $
 
42
 * @package zii.widgets.jui
 
43
 * @since 1.1
 
44
 */
 
45
class CJuiTabs extends CJuiWidget 
 
46
{
 
47
        /**
 
48
         * @var array list of tabs (tab title=>tab content).
 
49
         * Note that the tab title will not be HTML-encoded.
 
50
         * The tab content can be either a string or an array. When it is an array, it can
 
51
         * be in one of the following two formats:
 
52
         * <pre>
 
53
         * array('id'=>'myTabID', 'content'=>'tab content')
 
54
         * array('id'=>'myTabID', 'ajax'=>URL)
 
55
         * </pre>
 
56
         * where the 'id' element is optional. The second format allows the tab content
 
57
         * to be dynamically fetched from the specified URL via AJAX. The URL can be either
 
58
         * a string or an array. If an array, it will be normalized into a URL using {@link CHtml::normalizeUrl}.
 
59
         */
 
60
        public $tabs=array();
 
61
        /**
 
62
         * @var string the name of the container element that contains all panels. Defaults to 'div'.
 
63
         */
 
64
        public $tagName='div';
 
65
        /**
 
66
         * @var string the template that is used to generated every panel title.
 
67
         * The token "{title}" in the template will be replaced with the panel title and
 
68
         * the token "{url}" will be replaced with "#TabID" or with the url of the ajax request.
 
69
         */
 
70
        public $headerTemplate='<li><a href="{url}">{title}</a></li>';
 
71
        /**
 
72
         * @var string the template that is used to generated every tab content.
 
73
         * The token "{content}" in the template will be replaced with the panel content
 
74
         * and the token "{id}" with the tab ID.
 
75
         */
 
76
        public $contentTemplate='<div id="{id}">{content}</div>';
 
77
 
 
78
        /**
 
79
         * Run this widget.
 
80
         * This method registers necessary javascript and renders the needed HTML code.
 
81
         */
 
82
        public function run()
 
83
        {
 
84
                $id=$this->getId();
 
85
                if (isset($this->htmlOptions['id']))
 
86
                        $id = $this->htmlOptions['id'];
 
87
                else
 
88
                        $this->htmlOptions['id']=$id;
 
89
 
 
90
                echo CHtml::openTag($this->tagName,$this->htmlOptions)."\n";
 
91
 
 
92
                $tabsOut = "";
 
93
                $contentOut = "";
 
94
                $tabCount = 0;
 
95
 
 
96
                foreach($this->tabs as $title=>$content)
 
97
                {
 
98
                        $tabId = (is_array($content) && isset($content['id']))?$content['id']:$id.'_tab_'.$tabCount++;
 
99
 
 
100
                        if (!is_array($content))
 
101
                        {
 
102
                                $tabsOut .= strtr($this->headerTemplate, array('{title}'=>$title, '{url}'=>'#'.$tabId))."\n";
 
103
                                $contentOut .= strtr($this->contentTemplate, array('{content}'=>$content,'{id}'=>$tabId))."\n";
 
104
                        }
 
105
                        elseif (isset($content['content']))
 
106
                        {
 
107
                                $tabsOut .= strtr($this->headerTemplate, array('{title}'=>$title, '{url}'=>'#'.$tabId))."\n";
 
108
                                $contentOut .= strtr($this->contentTemplate, array('{content}'=>$content['content'],'{id}'=>$tabId))."\n";
 
109
 
 
110
                        }
 
111
                        elseif (isset($content['ajax']))
 
112
                        {
 
113
                                $tabsOut .= strtr($this->headerTemplate,array('{title}'=>$title, '{url}'=>CHtml::normalizeUrl($content['ajax'])))."\n";
 
114
                        }
 
115
                }
 
116
                echo "<ul>\n" . $tabsOut . "</ul>\n";
 
117
                echo $contentOut;
 
118
 
 
119
                echo CHtml::closeTag($this->tagName)."\n";
 
120
 
 
121
                $options=empty($this->options) ? '' : CJavaScript::encode($this->options);
 
122
                Yii::app()->getClientScript()->registerScript(__CLASS__.'#'.$id,"jQuery('#{$id}').tabs($options);");
 
123
        }
 
124
 
 
125
        /**
 
126
         * Registers the core script files.
 
127
         * This method overrides the parent implementation by registering the cookie plugin when cookie option is used.
 
128
         */
 
129
        protected function registerCoreScripts()
 
130
        {
 
131
                parent::registerCoreScripts();
 
132
                if(isset($this->options['cookie']))
 
133
                        Yii::app()->getClientScript()->registerCoreScript('cookie');
 
134
        }
 
135
}