Mercurial > hg > Members > shoshi > webvirt
comparison cake/libs/view/helpers/jquery_engine.php @ 0:261e66bd5a0c
hg init
author | Shoshi TAMAKI <shoshi@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 24 Jul 2011 21:08:31 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:261e66bd5a0c |
---|---|
1 <?php | |
2 /** | |
3 * jQuery Engine Helper for JsHelper | |
4 * | |
5 * Provides jQuery specific Javascript for JsHelper. | |
6 * | |
7 * Implements the JsHelper interface for jQuery. All $options arrays | |
8 * support all options found in the JsHelper, as well as those in the jQuery | |
9 * documentation. | |
10 * | |
11 * PHP versions 4 and 5 | |
12 * | |
13 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) | |
14 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | |
15 * | |
16 * Licensed under The MIT License | |
17 * Redistributions of files must retain the above copyright notice. | |
18 * | |
19 * @copyright Copyright 2006-2010, Cake Software Foundation, Inc. | |
20 * @link http://cakephp.org CakePHP Project | |
21 * @package cake | |
22 * @subpackage cake.view.helpers | |
23 * @license MIT License (http://www.opensource.org/licenses/mit-license.php) | |
24 */ | |
25 App::import('Helper', 'Js'); | |
26 | |
27 class JqueryEngineHelper extends JsBaseEngineHelper { | |
28 /** | |
29 * Option mappings for jQuery | |
30 * | |
31 * @var array | |
32 * @access protected | |
33 */ | |
34 var $_optionMap = array( | |
35 'request' => array( | |
36 'type' => 'dataType', | |
37 'before' => 'beforeSend', | |
38 'method' => 'type', | |
39 ), | |
40 'sortable' => array( | |
41 'complete' => 'stop', | |
42 ), | |
43 'drag' => array( | |
44 'snapGrid' => 'grid', | |
45 'container' => 'containment', | |
46 ), | |
47 'drop' => array( | |
48 'leave' => 'out', | |
49 'hover' => 'over' | |
50 ), | |
51 'slider' => array( | |
52 'complete' => 'stop', | |
53 'direction' => 'orientation' | |
54 ) | |
55 ); | |
56 | |
57 /** | |
58 * Callback arguments lists | |
59 * | |
60 * @var string | |
61 * @access protected | |
62 */ | |
63 var $_callbackArguments = array( | |
64 'slider' => array( | |
65 'start' => 'event, ui', | |
66 'slide' => 'event, ui', | |
67 'change' => 'event, ui', | |
68 'stop' => 'event, ui' | |
69 ), | |
70 'sortable' => array( | |
71 'start' => 'event, ui', | |
72 'sort' => 'event, ui', | |
73 'change' => 'event, ui', | |
74 'beforeStop' => 'event, ui', | |
75 'stop' => 'event, ui', | |
76 'update' => 'event, ui', | |
77 'receive' => 'event, ui', | |
78 'remove' => 'event, ui', | |
79 'over' => 'event, ui', | |
80 'out' => 'event, ui', | |
81 'activate' => 'event, ui', | |
82 'deactivate' => 'event, ui' | |
83 ), | |
84 'drag' => array( | |
85 'start' => 'event, ui', | |
86 'drag' => 'event, ui', | |
87 'stop' => 'event, ui', | |
88 ), | |
89 'drop' => array( | |
90 'activate' => 'event, ui', | |
91 'deactivate' => 'event, ui', | |
92 'over' => 'event, ui', | |
93 'out' => 'event, ui', | |
94 'drop' => 'event, ui' | |
95 ), | |
96 'request' => array( | |
97 'beforeSend' => 'XMLHttpRequest', | |
98 'error' => 'XMLHttpRequest, textStatus, errorThrown', | |
99 'success' => 'data, textStatus', | |
100 'complete' => 'XMLHttpRequest, textStatus', | |
101 'xhr' => '' | |
102 ) | |
103 ); | |
104 | |
105 /** | |
106 * The variable name of the jQuery Object, useful | |
107 * when jQuery is put into noConflict() mode. | |
108 * | |
109 * @var string | |
110 * @access public | |
111 */ | |
112 var $jQueryObject = '$'; | |
113 | |
114 /** | |
115 * Helper function to wrap repetitive simple method templating. | |
116 * | |
117 * @param string $method The method name being generated. | |
118 * @param string $template The method template | |
119 * @param string $selection the selection to apply | |
120 * @param string $options Array of options for method | |
121 * @param string $callbacks Array of callback / special options. | |
122 * @return string Composed method string | |
123 * @access public | |
124 */ | |
125 function _methodTemplate($method, $template, $options, $extraSafeKeys = array()) { | |
126 $options = $this->_mapOptions($method, $options); | |
127 $options = $this->_prepareCallbacks($method, $options); | |
128 $callbacks = array_keys($this->_callbackArguments[$method]); | |
129 if (!empty($extraSafeKeys)) { | |
130 $callbacks = array_merge($callbacks, $extraSafeKeys); | |
131 } | |
132 $options = $this->_parseOptions($options, $callbacks); | |
133 return sprintf($template, $this->selection, $options); | |
134 } | |
135 | |
136 /** | |
137 * Create javascript selector for a CSS rule | |
138 * | |
139 * @param string $selector The selector that is targeted | |
140 * @return object instance of $this. Allows chained methods. | |
141 * @access public | |
142 */ | |
143 function get($selector) { | |
144 if ($selector == 'window' || $selector == 'document') { | |
145 $this->selection = $this->jQueryObject . '(' . $selector .')'; | |
146 } else { | |
147 $this->selection = $this->jQueryObject . '("' . $selector . '")'; | |
148 } | |
149 return $this; | |
150 } | |
151 | |
152 /** | |
153 * Add an event to the script cache. Operates on the currently selected elements. | |
154 * | |
155 * ### Options | |
156 * | |
157 * - 'wrap' - Whether you want the callback wrapped in an anonymous function. (defaults true) | |
158 * - 'stop' - Whether you want the event to stopped. (defaults true) | |
159 * | |
160 * @param string $type Type of event to bind to the current dom id | |
161 * @param string $callback The Javascript function you wish to trigger or the function literal | |
162 * @param array $options Options for the event. | |
163 * @return string completed event handler | |
164 * @access public | |
165 */ | |
166 function event($type, $callback, $options = array()) { | |
167 $defaults = array('wrap' => true, 'stop' => true); | |
168 $options = array_merge($defaults, $options); | |
169 | |
170 $function = 'function (event) {%s}'; | |
171 if ($options['wrap'] && $options['stop']) { | |
172 $callback .= "\nreturn false;"; | |
173 } | |
174 if ($options['wrap']) { | |
175 $callback = sprintf($function, $callback); | |
176 } | |
177 return sprintf('%s.bind("%s", %s);', $this->selection, $type, $callback); | |
178 } | |
179 | |
180 /** | |
181 * Create a domReady event. For jQuery. This method does not | |
182 * bind a 'traditional event' as `$(document).bind('ready', fn)` | |
183 * Works in an entirely different fashion than `$(document).ready()` | |
184 * The first will not run the function when eval()'d as part of a response | |
185 * The second will. Because of the way that ajax pagination is done | |
186 * `$().ready()` is used. | |
187 * | |
188 * @param string $functionBody The code to run on domReady | |
189 * @return string completed domReady method | |
190 * @access public | |
191 */ | |
192 function domReady($functionBody) { | |
193 return $this->jQueryObject . '(document).ready(function () {' . $functionBody . '});'; | |
194 } | |
195 | |
196 /** | |
197 * Create an iteration over the current selection result. | |
198 * | |
199 * @param string $method The method you want to apply to the selection | |
200 * @param string $callback The function body you wish to apply during the iteration. | |
201 * @return string completed iteration | |
202 * @access public | |
203 */ | |
204 function each($callback) { | |
205 return $this->selection . '.each(function () {' . $callback . '});'; | |
206 } | |
207 | |
208 /** | |
209 * Trigger an Effect. | |
210 * | |
211 * @param string $name The name of the effect to trigger. | |
212 * @param array $options Array of options for the effect. | |
213 * @return string completed string with effect. | |
214 * @access public | |
215 * @see JsBaseEngineHelper::effect() | |
216 */ | |
217 function effect($name, $options = array()) { | |
218 $speed = null; | |
219 if (isset($options['speed']) && in_array($options['speed'], array('fast', 'slow'))) { | |
220 $speed = $this->value($options['speed']); | |
221 } | |
222 $effect = ''; | |
223 switch ($name) { | |
224 case 'slideIn': | |
225 case 'slideOut': | |
226 $name = ($name == 'slideIn') ? 'slideDown' : 'slideUp'; | |
227 case 'hide': | |
228 case 'show': | |
229 case 'fadeIn': | |
230 case 'fadeOut': | |
231 case 'slideDown': | |
232 case 'slideUp': | |
233 $effect = ".$name($speed);"; | |
234 break; | |
235 } | |
236 return $this->selection . $effect; | |
237 } | |
238 | |
239 /** | |
240 * Create an $.ajax() call. | |
241 * | |
242 * If the 'update' key is set, success callback will be overridden. | |
243 * | |
244 * @param mixed $url | |
245 * @param array $options See JsHelper::request() for options. | |
246 * @return string The completed ajax call. | |
247 * @access public | |
248 * @see JsBaseEngineHelper::request() for options list. | |
249 */ | |
250 function request($url, $options = array()) { | |
251 $url = $this->url($url); | |
252 $options = $this->_mapOptions('request', $options); | |
253 if (isset($options['data']) && is_array($options['data'])) { | |
254 $options['data'] = $this->_toQuerystring($options['data']); | |
255 } | |
256 $options['url'] = $url; | |
257 if (isset($options['update'])) { | |
258 $wrapCallbacks = isset($options['wrapCallbacks']) ? $options['wrapCallbacks'] : true; | |
259 $success = ''; | |
260 if(isset($options['success']) AND !empty($options['success'])) { | |
261 $success .= $options['success']; | |
262 } | |
263 $success .= $this->jQueryObject . '("' . $options['update'] . '").html(data);'; | |
264 if (!$wrapCallbacks) { | |
265 $success = 'function (data, textStatus) {' . $success . '}'; | |
266 } | |
267 $options['dataType'] = 'html'; | |
268 $options['success'] = $success; | |
269 unset($options['update']); | |
270 } | |
271 $callbacks = array('success', 'error', 'beforeSend', 'complete'); | |
272 if (!empty($options['dataExpression'])) { | |
273 $callbacks[] = 'data'; | |
274 unset($options['dataExpression']); | |
275 } | |
276 $options = $this->_prepareCallbacks('request', $options); | |
277 $options = $this->_parseOptions($options, $callbacks); | |
278 return $this->jQueryObject . '.ajax({' . $options .'});'; | |
279 } | |
280 | |
281 /** | |
282 * Create a sortable element. | |
283 * | |
284 * Requires both Ui.Core and Ui.Sortables to be loaded. | |
285 * | |
286 * @param array $options Array of options for the sortable. | |
287 * @return string Completed sortable script. | |
288 * @access public | |
289 * @see JsBaseEngineHelper::sortable() for options list. | |
290 */ | |
291 function sortable($options = array()) { | |
292 $template = '%s.sortable({%s});'; | |
293 return $this->_methodTemplate('sortable', $template, $options); | |
294 } | |
295 | |
296 /** | |
297 * Create a Draggable element | |
298 * | |
299 * Requires both Ui.Core and Ui.Draggable to be loaded. | |
300 * | |
301 * @param array $options Array of options for the draggable element. | |
302 * @return string Completed Draggable script. | |
303 * @access public | |
304 * @see JsBaseEngineHelper::drag() for options list. | |
305 */ | |
306 function drag($options = array()) { | |
307 $template = '%s.draggable({%s});'; | |
308 return $this->_methodTemplate('drag', $template, $options); | |
309 } | |
310 | |
311 /** | |
312 * Create a Droppable element | |
313 * | |
314 * Requires both Ui.Core and Ui.Droppable to be loaded. | |
315 * | |
316 * @param array $options Array of options for the droppable element. | |
317 * @return string Completed Droppable script. | |
318 * @access public | |
319 * @see JsBaseEngineHelper::drop() for options list. | |
320 */ | |
321 function drop($options = array()) { | |
322 $template = '%s.droppable({%s});'; | |
323 return $this->_methodTemplate('drop', $template, $options); | |
324 } | |
325 | |
326 /** | |
327 * Create a Slider element | |
328 * | |
329 * Requires both Ui.Core and Ui.Slider to be loaded. | |
330 * | |
331 * @param array $options Array of options for the droppable element. | |
332 * @return string Completed Slider script. | |
333 * @access public | |
334 * @see JsBaseEngineHelper::slider() for options list. | |
335 */ | |
336 function slider($options = array()) { | |
337 $callbacks = array('start', 'change', 'slide', 'stop'); | |
338 $template = '%s.slider({%s});'; | |
339 return $this->_methodTemplate('slider', $template, $options, $callbacks); | |
340 } | |
341 | |
342 /** | |
343 * Serialize a form attached to $selector. If the current selection is not an input or | |
344 * form, errors will be created in the Javascript. | |
345 * | |
346 * @param array $options Options for the serialization | |
347 * @return string completed form serialization script. | |
348 * @access public | |
349 * @see JsBaseEngineHelper::serializeForm() for option list. | |
350 */ | |
351 function serializeForm($options = array()) { | |
352 $options = array_merge(array('isForm' => false, 'inline' => false), $options); | |
353 $selector = $this->selection; | |
354 if (!$options['isForm']) { | |
355 $selector = $this->selection . '.closest("form")'; | |
356 } | |
357 $method = '.serialize()'; | |
358 if (!$options['inline']) { | |
359 $method .= ';'; | |
360 } | |
361 return $selector . $method; | |
362 } | |
363 } |