1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35:
36:
37: 38: 39:
40:
41: class X2WidgetBehavior extends CBehavior {
42:
43: const NAMESPACE_KEY = '_x2widget_namespace';
44:
45: 46: 47:
48: public $element;
49:
50: 51: 52:
53: public $JSClass = 'Widget';
54:
55: 56: 57:
58: public $instantiateJSClassOnInit = false;
59:
60: public $checkIfJSClassIsDefined = false;
61:
62: 63: 64:
65: public $namespace = '';
66:
67: public function __construct () {
68: }
69:
70: public function attach ($owner) {
71: if (!($owner instanceof CWidget)) {
72: throw new CException ('owner must be an instance of CWidget');
73: }
74: return parent::attach ($owner);
75: }
76:
77: public function initNamespace () {
78: if ($this->owner->namespace === '' && isset ($_POST[self::NAMESPACE_KEY])) {
79: $this->owner->namespace = $_POST[self::NAMESPACE_KEY];
80: }
81: }
82:
83: public function resolveIds ($selector) {
84: return preg_replace ('/#/', '#'.$this->owner->namespace, $selector);
85: }
86:
87: public function resolveId ($id) {
88: return $this->owner->namespace.$id;
89: }
90:
91: public function getJSObjectName () {
92: return "x2.".$this->owner->namespace.lcfirst ($this->owner->JSClass);
93: }
94:
95: public function registerPackages () {
96: Yii::app()->clientScript->registerPackages ($this->owner->getPackages (), true);
97: }
98:
99: protected $_packages;
100: public function getPackages () {
101: if (!isset ($this->_packages)) {
102: $this->_packages = array (
103: 'X2Widget' => array(
104: 'baseUrl' => Yii::app()->request->baseUrl,
105: 'js' => array(
106: 'js/X2Widget.js',
107: ),
108: ),
109: );
110: }
111: return $this->_packages;
112: }
113:
114: public function prepareJSParams ($args, array $functions=array ()) {
115: foreach ($args as $key => &$val) {
116: if (is_array ($val)) {
117: } else if (is_string ($val) && preg_match ('/^js:/', $val)) {
118: $val = preg_replace ('/^js:/', '', $val);
119: $functions[$key] = $val;
120: unset ($args[$key]);
121: }
122: }
123: unset ($val);
124:
125: $paramStr = '';
126: if (count ($functions)) {
127: $fnStr = '';
128: foreach ($functions as $key => $val) {
129: $fnStr .= $key . ': ' . $val . ',';
130: }
131: $fnStr = preg_replace ('/,$/', '', $fnStr);
132: $paramStr = '$.extend ('.CJSON::encode ($args).', {'.
133: $fnStr.
134: '})';
135: } else {
136: $paramStr = CJSON::encode ($args);
137: }
138:
139: return $paramStr;
140: }
141:
142: 143: 144:
145: public function instantiateJSClass ($onReady=true) {
146: $jsObjName = $this->owner->getJSObjectName ();
147:
148: $args = $this->prepareJSParams ($this->owner->getJSClassParams ());
149:
150: $js = ($this->checkIfJSClassIsDefined ? "if (typeof $jsObjName === 'undefined') {" : '').
151: "$jsObjName = new x2.{$this->owner->JSClass} (".
152: $args.
153: ");".
154: ($this->checkIfJSClassIsDefined ? "}" : '');
155: $scriptName = $this->owner->getId ().get_class ($this->owner).'JSClassInstantiation';
156:
157: if (Yii::app()->params->isMobileApp) {
158: Yii::app()->controller->onPageLoad ($js, $scriptName);
159: } else {
160: Yii::app()->clientScript->registerScript (
161: $scriptName,
162: ($onReady ? "$(function () {" : "").
163: $js.
164: ($onReady ? "});" : ""), CClientScript::POS_END);
165: }
166:
167: Yii::app()->clientScript->registerScript('X2WidgetSetup',"
168: x2.Widget.NAMESPACE_KEY = '".self::NAMESPACE_KEY."';
169: ", CClientScript::POS_READY);
170: }
171:
172: protected $_translations;
173: protected function getTranslations () {
174: if (!isset ($this->_translations)) {
175: $this->_translations = array ();
176: }
177: return $this->_translations;
178: }
179:
180: protected $_JSClassParams;
181: public function getJSClassParams () {
182: if (!isset ($this->_JSClassParams)) {
183: $this->_JSClassParams = array (
184: 'element' => isset ($this->owner->element) ?
185: $this->owner->element : '#'.$this->owner->id,
186: 'translations' => $this->owner->getTranslations (),
187: 'namespace' => $this->owner->namespace,
188: );
189: }
190: return $this->_JSClassParams;
191: }
192:
193: public function setJSClassParams ($jSClassParams) {
194: $this->_JSClassParams = array_merge ($this->getJSClassParams (), $jSClassParams);
195: }
196:
197: }
198:
199: ?>
200: