CJuiWidget.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * CJuiWidget 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. * This is the base class for all JUI widget classes.
  13. *
  14. * @author Sebastian Thierer <sebathi@gmail.com>
  15. * @author Qiang Xue <qiang.xue@gmail.com>
  16. * @package zii.widgets.jui
  17. * @since 1.1
  18. */
  19. abstract class CJuiWidget extends CWidget
  20. {
  21. /**
  22. * @var string the root URL that contains all JUI JavaScript files.
  23. * If this property is not set (default), Yii will publish the JUI package included in the zii release and use
  24. * that to infer the root script URL. You should set this property if you intend to use
  25. * a JUI package whose version is different from the one included in zii.
  26. * Note that under this URL, there must be a file whose name is specified by {@link scriptFile}.
  27. * Do not append any slash character to the URL.
  28. */
  29. public $scriptUrl;
  30. /**
  31. * @var string the root URL that contains all JUI theme folders.
  32. * If this property is not set (default), Yii will publish the JUI package included in the zii release and use
  33. * that to infer the root theme URL. You should set this property if you intend to use
  34. * a theme that is not found in the JUI package included in zii.
  35. * Note that under this URL, there must be a directory whose name is specified by {@link theme}.
  36. * Do not append any slash character to the URL.
  37. */
  38. public $themeUrl;
  39. /**
  40. * @var string the JUI theme name. Defaults to 'base'. Make sure that under {@link themeUrl} there
  41. * is a directory whose name is the same as this property value (case-sensitive).
  42. */
  43. public $theme='base';
  44. /**
  45. * @var mixed the main JUI JavaScript file. Defaults to 'jquery-ui.min.js'.
  46. * Note the file must exist under the URL specified by {@link scriptUrl}.
  47. * If you need to include multiple script files (e.g. during development, you want to include individual
  48. * plugin script files rather than the minized JUI script file), you may set this property
  49. * as an array of the script file names.
  50. * This property can also be set as false, which means the widget will not include any script file,
  51. * and it is your responsibility to explicitly include it somewhere else.
  52. */
  53. public $scriptFile='jquery-ui.min.js';
  54. /**
  55. * @var mixed the theme CSS file name. Defaults to 'jquery-ui.css'.
  56. * Note the file must exist under the URL specified by {@link themeUrl}/{@link theme}.
  57. * If you need to include multiple theme CSS files (e.g. during development, you want to include individual
  58. * plugin CSS files), you may set this property as an array of the CSS file names.
  59. * This property can also be set as false, which means the widget will not include any theme CSS file,
  60. * and it is your responsibility to explicitly include it somewhere else.
  61. */
  62. public $cssFile='jquery-ui.css';
  63. /**
  64. * @var array the initial JavaScript options that should be passed to the JUI plugin.
  65. */
  66. public $options=array();
  67. /**
  68. * @var array the HTML attributes that should be rendered in the HTML tag representing the JUI widget.
  69. */
  70. public $htmlOptions=array();
  71. /**
  72. * Initializes the widget.
  73. * This method will publish JUI assets if necessary.
  74. * It will also register jquery and JUI JavaScript files and the theme CSS file.
  75. * If you override this method, make sure you call the parent implementation first.
  76. */
  77. public function init()
  78. {
  79. $this->resolvePackagePath();
  80. $this->registerCoreScripts();
  81. parent::init();
  82. }
  83. /**
  84. * Determine the JUI package installation path.
  85. * This method will identify the JavaScript root URL and theme root URL.
  86. * If they are not explicitly specified, it will publish the included JUI package
  87. * and use that to resolve the needed paths.
  88. */
  89. protected function resolvePackagePath()
  90. {
  91. if($this->scriptUrl===null || $this->themeUrl===null)
  92. {
  93. $cs=Yii::app()->getClientScript();
  94. if($this->scriptUrl===null)
  95. $this->scriptUrl=$cs->getCoreScriptUrl().'/jui/js';
  96. if($this->themeUrl===null)
  97. $this->themeUrl=$cs->getCoreScriptUrl().'/jui/css';
  98. }
  99. }
  100. /**
  101. * Registers the core script files.
  102. * This method registers jquery and JUI JavaScript files and the theme CSS file.
  103. */
  104. protected function registerCoreScripts()
  105. {
  106. $cs=Yii::app()->getClientScript();
  107. if(is_string($this->cssFile))
  108. $cs->registerCssFile($this->themeUrl.'/'.$this->theme.'/'.$this->cssFile);
  109. elseif(is_array($this->cssFile))
  110. {
  111. foreach($this->cssFile as $cssFile)
  112. $cs->registerCssFile($this->themeUrl.'/'.$this->theme.'/'.$cssFile);
  113. }
  114. $cs->registerCoreScript('jquery');
  115. if(is_string($this->scriptFile))
  116. $this->registerScriptFile($this->scriptFile);
  117. elseif(is_array($this->scriptFile))
  118. {
  119. foreach($this->scriptFile as $scriptFile)
  120. $this->registerScriptFile($scriptFile);
  121. }
  122. }
  123. /**
  124. * Registers a JavaScript file under {@link scriptUrl}.
  125. * Note that by default, the script file will be rendered at the end of a page to improve page loading speed.
  126. * @param string $fileName JavaScript file name
  127. * @param integer $position the position of the JavaScript file. Valid values include the following:
  128. * <ul>
  129. * <li>CClientScript::POS_HEAD : the script is inserted in the head section right before the title element.</li>
  130. * <li>CClientScript::POS_BEGIN : the script is inserted at the beginning of the body section.</li>
  131. * <li>CClientScript::POS_END : the script is inserted at the end of the body section.</li>
  132. * </ul>
  133. */
  134. protected function registerScriptFile($fileName,$position=CClientScript::POS_END)
  135. {
  136. Yii::app()->getClientScript()->registerScriptFile($this->scriptUrl.'/'.$fileName,$position);
  137. }
  138. }