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