CJuiTabs.php 4.7 KB

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