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