1: <?php
 2: /**
 3:  * CJuiAccordion class file.
 4:  *
 5:  * @author Sebastian Thierer <sebathi@gmail.com>
 6:  * @author Qiang Xue <qiang.xue@gmail.com>
 7:  * @link http://www.yiiframework.com/
 8:  * @copyright 2008-2013 Yii Software LLC
 9:  * @license http://www.yiiframework.com/license/
10:  */
11: 
12: Yii::import('zii.widgets.jui.CJuiWidget');
13: 
14: /**
15:  * CJuiAccordion displays an accordion widget.
16:  *
17:  * CJuiAccordion encapsulates the {@link http://jqueryui.com/accordion/ JUI Accordion}
18:  * plugin.
19:  *
20:  * To use this widget, you may insert the following code in a view:
21:  * <pre>
22:  * $this->widget('zii.widgets.jui.CJuiAccordion',array(
23:  *     'panels'=>array(
24:  *         'panel 1'=>'content for panel 1',
25:  *         'panel 2'=>'content for panel 2',
26:  *         // panel 3 contains the content rendered by a partial view
27:  *         'panel 3'=>$this->renderPartial('_partial',null,true),
28:  *     ),
29:  *     // additional javascript options for the accordion plugin
30:  *     'options'=>array(
31:  *         'animate'=>'bounceslide',
32:  *     ),
33:  * ));
34:  * </pre>
35:  *
36:  * By configuring the {@link options} property, you may specify the options
37:  * that need to be passed to the JUI accordion plugin. Please refer to
38:  * the {@link http://api.jqueryui.com/accordion/ JUI Accordion API}
39:  * documentation for possible options (name-value pairs) and
40:  * {@link http://jqueryui.com/accordion/ JUI Accordion page} for general
41:  * description and demo.
42:  *
43:  * @author Sebastian Thierer <sebathi@gmail.com>
44:  * @author Qiang Xue <qiang.xue@gmail.com>
45:  * @package zii.widgets.jui
46:  * @since 1.1
47:  */
48: class CJuiAccordion extends CJuiWidget
49: {
50:     /**
51:      * @var array list of panels (panel title=>panel content).
52:      * Note that neither panel title nor panel content will be HTML-encoded.
53:      */
54:     public $panels=array();
55:     /**
56:      * @var string the name of the container element that contains all panels. Defaults to 'div'.
57:      */
58:     public $tagName='div';
59:     /**
60:      * @var string the template that is used to generated every panel header.
61:      * The token "{title}" in the template will be replaced with the panel title.
62:      * Note that if you make change to this template, you may also need to adjust
63:      * the 'header' setting in {@link options}.
64:      */
65:     public $headerTemplate='<h3><a href="#">{title}</a></h3>';
66:     /**
67:      * @var string the template that is used to generated every panel content.
68:      * The token "{content}" in the template will be replaced with the panel content.
69:      */
70:     public $contentTemplate='<div>{content}</div>';
71: 
72:     /**
73:      * Run this widget.
74:      * This method registers necessary javascript and renders the needed HTML code.
75:      */
76:     public function run()
77:     {
78:         $id=$this->getId();
79:         if(isset($this->htmlOptions['id']))
80:             $id=$this->htmlOptions['id'];
81:         else
82:             $this->htmlOptions['id']=$id;
83: 
84:         echo CHtml::openTag($this->tagName,$this->htmlOptions)."\n";
85:         foreach($this->panels as $title=>$content)
86:         {
87:             echo strtr($this->headerTemplate,array('{title}'=>$title))."\n";
88:             echo strtr($this->contentTemplate,array('{content}'=>$content))."\n";
89:         }
90:         echo CHtml::closeTag($this->tagName);
91: 
92:         $options=CJavaScript::encode($this->options);
93:         Yii::app()->getClientScript()->registerScript(__CLASS__.'#'.$id,"jQuery('#{$id}').accordion($options);");
94:     }
95: }