CJuiAccordion.php 3.1 KB

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