PageRenderTime 51ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/dinhtrung/yiicorecms/
PHP | 135 lines | 51 code | 10 blank | 74 comment | 5 complexity | be7468d1e44ce14299b53b63e780bb8a MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause, CC0-1.0, BSD-2-Clause, GPL-2.0, LGPL-2.1, LGPL-3.0
  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. 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 3222 2011-05-13 15:04:13Z 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="{id}">{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. if (isset($this->htmlOptions['id']))
  83. $id = $this->htmlOptions['id'];
  84. else
  85. $this->htmlOptions['id']=$id;
  86. echo CHtml::openTag($this->tagName,$this->htmlOptions)."\n";
  87. $tabsOut = "";
  88. $contentOut = "";
  89. $tabCount = 0;
  90. foreach($this->tabs as $title=>$content)
  91. {
  92. $tabId = (is_array($content) && isset($content['id']))?$content['id']:$id.'_tab_'.$tabCount++;
  93. if (!is_array($content))
  94. {
  95. $tabsOut .= strtr($this->headerTemplate, array('{title}'=>$title, '{url}'=>'#'.$tabId, '{id}'=>'#' . $tabId))."\n";
  96. $contentOut .= strtr($this->contentTemplate, array('{content}'=>$content,'{id}'=>$tabId))."\n";
  97. }
  98. elseif (isset($content['ajax']))
  99. {
  100. $tabsOut .= strtr($this->headerTemplate, array('{title}'=>$title, '{url}'=>CHtml::normalizeUrl($content['ajax']), '{id}'=>'#' . $tabId))."\n";
  101. }
  102. else
  103. {
  104. $tabsOut .= strtr($this->headerTemplate, array('{title}'=>$title, '{url}'=>'#'.$tabId))."\n";
  105. if(isset($content['content']))
  106. $contentOut .= strtr($this->contentTemplate, array('{content}'=>$content['content'],'{id}'=>$tabId))."\n";
  107. }
  108. }
  109. echo "<ul>\n" . $tabsOut . "</ul>\n";
  110. echo $contentOut;
  111. echo CHtml::closeTag($this->tagName)."\n";
  112. $options=empty($this->options) ? '' : CJavaScript::encode($this->options);
  113. Yii::app()->getClientScript()->registerScript(__CLASS__.'#'.$id,"jQuery('#{$id}').tabs($options);");
  114. }
  115. /**
  116. * Registers the core script files.
  117. * This method overrides the parent implementation by registering the cookie plugin when cookie option is used.
  118. */
  119. protected function registerCoreScripts()
  120. {
  121. parent::registerCoreScripts();
  122. if(isset($this->options['cookie']))
  123. Yii::app()->getClientScript()->registerCoreScript('cookie');
  124. }
  125. }