PageRenderTime 952ms CodeModel.GetById 34ms RepoModel.GetById 8ms app.codeStats 0ms

/framework/zii/widgets/jui/CJuiTabs.php

https://github.com/balor/yiicms
PHP | 117 lines | 36 code | 11 blank | 70 comment | 2 complexity | 2749dfe524f98bf35935c754a2aa3eee MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * CJuiTabs class file.
  4. *
  5. * @author Sebastian Thierer <sebas@artfos.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright Copyright &copy; 2008-2010 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/demos/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://jqueryui.com/demos/tabs/ JUI tabs} documentation
  36. * for possible options (name-value pairs).
  37. *
  38. * @author Sebastian Thierer <sebathi@gmail.com>
  39. * @version $Id: CJuiTabs.php 127 2010-02-18 14:03:04Z sebathi $
  40. * @package zii.widgets.jui
  41. * @since 1.1
  42. */
  43. class CJuiTabs extends CJuiWidget
  44. {
  45. /**
  46. * @var array list of tabs (tab title=>tab content).
  47. * Note that the tab title will not be HTML-encoded.
  48. * The tab content can be either a string or an array. When it is an array, it can
  49. * be in one of the following two formats:
  50. * <pre>
  51. * array('id'=>'myTabID', 'content'=>'tab content')
  52. * array('id'=>'myTabID', 'ajax'=>URL)
  53. * </pre>
  54. * where the 'id' element is optional. The second format allows the tab content
  55. * to be dynamically fetched from the specified URL via AJAX. The URL can be either
  56. * a string or an array. If an array, it will be normalized into a URL using {@link CHtml::normalizeUrl}.
  57. */
  58. public $tabs=array();
  59. /**
  60. * @var string the name of the container element that contains all panels. Defaults to 'div'.
  61. */
  62. public $tagName='div';
  63. /**
  64. * @var string the template that is used to generated every panel title.
  65. * The token "{title}" in the template will be replaced with the panel title and
  66. * the token "{url}" will be replaced with "#TabID" or with the url of the ajax request.
  67. */
  68. public $headerTemplate='<li><a href="{url}">{title}</a></li>';
  69. /**
  70. * @var string the template that is used to generated every tab content.
  71. * The token "{content}" in the template will be replaced with the panel content
  72. * and the token "{id}" with the tab ID.
  73. */
  74. public $contentTemplate='<div id="{id}">{content}</div>';
  75. /**
  76. * Run this widget.
  77. * This method registers necessary javascript and renders the needed HTML code.
  78. */
  79. public function run()
  80. {
  81. $id=$this->getId();
  82. $this->htmlOptions['id']=$id;
  83. echo CHtml::openTag($this->tagName,$this->htmlOptions)."\n";
  84. $tabsOut = "";
  85. $contentOut = "";
  86. $tabCount = 0;
  87. foreach($this->tabs as $title=>$content)
  88. {
  89. $tabId = (is_array($content) && isset($content['id']))?$content['id']:$id.'_tab_'.$tabCount++;
  90. if (!is_array($content)){
  91. $tabsOut .= strtr($this->headerTemplate, array('{title}'=>$title, '{url}'=>'#'.$tabId))."\n";
  92. $contentOut .= strtr($this->contentTemplate, array('{content}'=>$content,'{id}'=>$tabId))."\n";
  93. }elseif (isset($content['content'])){
  94. $tabsOut .= strtr($this->headerTemplate, array('{title}'=>$title, '{url}'=>'#'.$tabId))."\n";
  95. $contentOut .= strtr($this->contentTemplate, array('{content}'=>$content['content'],'{id}'=>$tabId))."\n";
  96. }elseif (isset($content['ajax'])){
  97. $tabsOut .= strtr($this->headerTemplate,array('{title}'=>$title, '{url}'=>CHtml::normalizeUrl($content['ajax'])))."\n";
  98. }
  99. }
  100. echo "<ul>\n" . $tabsOut . "</ul>\n";
  101. echo $contentOut;
  102. echo CHtml::closeTag($this->tagName)."\n";
  103. $options=empty($this->options) ? '' : CJavaScript::encode($this->options);
  104. Yii::app()->getClientScript()->registerScript(__CLASS__.'#'.$id,"jQuery('#{$id}').tabs($options);");
  105. }
  106. }