Mercurial > hg > Members > shoshi > webvirt
comparison cake/libs/view/helpers/prototype_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 * Prototype Engine Helper for JsHelper | |
4 * | |
5 * Provides Prototype specific Javascript for JsHelper. Requires at least | |
6 * Prototype 1.6 | |
7 * | |
8 * PHP versions 4 and 5 | |
9 * | |
10 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) | |
11 * Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | |
12 * | |
13 * Licensed under The MIT License | |
14 * Redistributions of files must retain the above copyright notice. | |
15 * | |
16 * @copyright Copyright 2005-2010, Cake Software Foundation, Inc. (http://cakefoundation.org) | |
17 * @link http://cakephp.org CakePHP(tm) Project | |
18 * @package cake | |
19 * @subpackage cake.libs.view.helpers | |
20 * @since CakePHP(tm) v 1.3 | |
21 * @license MIT License (http://www.opensource.org/licenses/mit-license.php) | |
22 */ | |
23 App::import('Helper', 'Js'); | |
24 | |
25 class PrototypeEngineHelper extends JsBaseEngineHelper { | |
26 /** | |
27 * Is the current selection a multiple selection? or is it just a single element. | |
28 * | |
29 * @var boolean | |
30 */ | |
31 var $_multiple = false; | |
32 | |
33 /** | |
34 * Option mappings for Prototype | |
35 * | |
36 * @var array | |
37 */ | |
38 var $_optionMap = array( | |
39 'request' => array( | |
40 'async' => 'asynchronous', | |
41 'data' => 'parameters', | |
42 'before' => 'onCreate', | |
43 'success' => 'onSuccess', | |
44 'complete' => 'onComplete', | |
45 'error' => 'onFailure' | |
46 ), | |
47 'sortable' => array( | |
48 'sort' => 'onChange', | |
49 'complete' => 'onUpdate', | |
50 ), | |
51 'drag' => array( | |
52 'snapGrid' => 'snap', | |
53 'container' => 'constraint', | |
54 'stop' => 'onEnd', | |
55 'start' => 'onStart', | |
56 'drag' => 'onDrag', | |
57 ), | |
58 'drop' => array( | |
59 'hover' => 'onHover', | |
60 'drop' => 'onDrop', | |
61 'hoverClass' => 'hoverclass', | |
62 ), | |
63 'slider' => array( | |
64 'direction' => 'axis', | |
65 'change' => 'onSlide', | |
66 'complete' => 'onChange', | |
67 'value' => 'sliderValue', | |
68 ) | |
69 ); | |
70 | |
71 /** | |
72 * Contains a list of callback names -> default arguments. | |
73 * | |
74 * @var array | |
75 */ | |
76 var $_callbackArguments = array( | |
77 'slider' => array( | |
78 'onSlide' => 'value', | |
79 'onChange' => 'value', | |
80 ), | |
81 'drag' => array( | |
82 'onStart' => 'event', | |
83 'onDrag' => 'event', | |
84 'change' => 'draggable', | |
85 'onEnd' => 'event', | |
86 ), | |
87 'drop' => array( | |
88 'onHover' => 'draggable, droppable, event', | |
89 'onDrop' => 'draggable, droppable, event', | |
90 ), | |
91 'request' => array( | |
92 'onCreate' => 'transport', | |
93 'onComplete' => 'transport', | |
94 'onFailure' => 'response, jsonHeader', | |
95 'onRequest' => 'transport', | |
96 'onSuccess' => 'response, jsonHeader' | |
97 ), | |
98 'sortable' => array( | |
99 'onStart' => 'element', | |
100 'onChange' => 'element', | |
101 'onUpdate' => 'element', | |
102 ), | |
103 ); | |
104 | |
105 /** | |
106 * Create javascript selector for a CSS rule | |
107 * | |
108 * @param string $selector The selector that is targeted | |
109 * @return object instance of $this. Allows chained methods. | |
110 */ | |
111 function get($selector) { | |
112 $this->_multiple = false; | |
113 if ($selector == 'window' || $selector == 'document') { | |
114 $this->selection = "$(" . $selector .")"; | |
115 return $this; | |
116 } | |
117 if (preg_match('/^#[^\s.]+$/', $selector)) { | |
118 $this->selection = '$("' . substr($selector, 1) . '")'; | |
119 return $this; | |
120 } | |
121 $this->_multiple = true; | |
122 $this->selection = '$$("' . $selector . '")'; | |
123 return $this; | |
124 } | |
125 | |
126 /** | |
127 * Add an event to the script cache. Operates on the currently selected elements. | |
128 * | |
129 * ### Options | |
130 * | |
131 * - `wrap` - Whether you want the callback wrapped in an anonymous function. (defaults true) | |
132 * - `stop` - Whether you want the event to stopped. (defaults true) | |
133 * | |
134 * @param string $type Type of event to bind to the current 946 id | |
135 * @param string $callback The Javascript function you wish to trigger or the function literal | |
136 * @param array $options Options for the event. | |
137 * @return string completed event handler | |
138 */ | |
139 function event($type, $callback, $options = array()) { | |
140 $defaults = array('wrap' => true, 'stop' => true); | |
141 $options = array_merge($defaults, $options); | |
142 | |
143 $function = 'function (event) {%s}'; | |
144 if ($options['wrap'] && $options['stop']) { | |
145 $callback = "event.stop();\n" . $callback; | |
146 } | |
147 if ($options['wrap']) { | |
148 $callback = sprintf($function, $callback); | |
149 } | |
150 $out = $this->selection . ".observe(\"{$type}\", $callback);"; | |
151 return $out; | |
152 } | |
153 | |
154 /** | |
155 * Create a domReady event. This is a special event in many libraries | |
156 * | |
157 * @param string $functionBody The code to run on domReady | |
158 * @return string completed domReady method | |
159 * @access public | |
160 */ | |
161 function domReady($functionBody) { | |
162 $this->selection = 'document'; | |
163 return $this->event('dom:loaded', $functionBody, array('stop' => false)); | |
164 } | |
165 | |
166 /** | |
167 * Create an iteration over the current selection result. | |
168 * | |
169 * @param string $method The method you want to apply to the selection | |
170 * @param string $callback The function body you wish to apply during the iteration. | |
171 * @return string completed iteration | |
172 * @access public | |
173 */ | |
174 function each($callback) { | |
175 return $this->selection . '.each(function (item, index) {' . $callback . '});'; | |
176 } | |
177 | |
178 /** | |
179 * Trigger an Effect. | |
180 * | |
181 * ### Note: Effects require Scriptaculous to be loaded. | |
182 * | |
183 * @param string $name The name of the effect to trigger. | |
184 * @param array $options Array of options for the effect. | |
185 * @return string completed string with effect. | |
186 * @access public | |
187 * @see JsBaseEngineHelper::effect() | |
188 */ | |
189 function effect($name, $options = array()) { | |
190 $effect = ''; | |
191 $optionString = null; | |
192 if (isset($options['speed'])) { | |
193 if ($options['speed'] == 'fast') { | |
194 $options['duration'] = 0.5; | |
195 } elseif ($options['speed'] == 'slow') { | |
196 $options['duration'] = 2; | |
197 } else { | |
198 $options['duration'] = 1; | |
199 } | |
200 unset($options['speed']); | |
201 } | |
202 if (!empty($options)) { | |
203 $optionString = ', {' . $this->_parseOptions($options) . '}'; | |
204 } | |
205 switch ($name) { | |
206 case 'hide': | |
207 case 'show': | |
208 $effect = $this->selection . '.' . $name . '();'; | |
209 break; | |
210 case 'slideIn': | |
211 case 'slideOut': | |
212 $name = ($name == 'slideIn') ? 'slideDown' : 'slideUp'; | |
213 $effect = 'Effect.' . $name . '(' . $this->selection . $optionString . ');'; | |
214 break; | |
215 case 'fadeIn': | |
216 case 'fadeOut': | |
217 $name = ($name == 'fadeIn') ? 'appear' : 'fade'; | |
218 $effect = $this->selection . '.' . $name .'(' . substr($optionString, 2) . ');'; | |
219 break; | |
220 } | |
221 return $effect; | |
222 } | |
223 | |
224 /** | |
225 * Create an Ajax or Ajax.Updater call. | |
226 * | |
227 * @param mixed $url | |
228 * @param array $options | |
229 * @return string The completed ajax call. | |
230 * @access public | |
231 */ | |
232 function request($url, $options = array()) { | |
233 $url = '"'. $this->url($url) . '"'; | |
234 $options = $this->_mapOptions('request', $options); | |
235 $type = '.Request'; | |
236 $data = null; | |
237 if (isset($options['type']) && strtolower($options['type']) == 'json') { | |
238 unset($options['type']); | |
239 } | |
240 if (isset($options['update'])) { | |
241 $url = '"' . str_replace('#', '', $options['update']) . '", ' . $url; | |
242 $type = '.Updater'; | |
243 unset($options['update'], $options['type']); | |
244 } | |
245 $safe = array_keys($this->_callbackArguments['request']); | |
246 $options = $this->_prepareCallbacks('request', $options, $safe); | |
247 if (!empty($options['dataExpression'])) { | |
248 $safe[] = 'parameters'; | |
249 unset($options['dataExpression']); | |
250 } | |
251 $options = $this->_parseOptions($options, $safe); | |
252 if (!empty($options)) { | |
253 $options = ', {' . $options . '}'; | |
254 } | |
255 return "var jsRequest = new Ajax$type($url$options);"; | |
256 } | |
257 | |
258 /** | |
259 * Create a sortable element. | |
260 * | |
261 * #### Note: Requires scriptaculous to be loaded. | |
262 * | |
263 * The scriptaculous implementation of sortables does not suppot the 'start' | |
264 * and 'distance' options. | |
265 * | |
266 * @param array $options Array of options for the sortable. | |
267 * @return string Completed sortable script. | |
268 * @access public | |
269 * @see JsBaseEngineHelper::sortable() for options list. | |
270 */ | |
271 function sortable($options = array()) { | |
272 $options = $this->_processOptions('sortable', $options); | |
273 if (!empty($options)) { | |
274 $options = ', {' . $options . '}'; | |
275 } | |
276 return 'var jsSortable = Sortable.create(' . $this->selection . $options . ');'; | |
277 } | |
278 | |
279 /** | |
280 * Create a Draggable element. | |
281 * | |
282 * #### Note: Requires scriptaculous to be loaded. | |
283 * | |
284 * @param array $options Array of options for the draggable. | |
285 * @return string Completed draggable script. | |
286 * @access public | |
287 * @see JsBaseEngineHelper::draggable() for options list. | |
288 */ | |
289 function drag($options = array()) { | |
290 $options = $this->_processOptions('drag', $options); | |
291 if (!empty($options)) { | |
292 $options = ', {' . $options . '}'; | |
293 } | |
294 if ($this->_multiple) { | |
295 return $this->each('new Draggable(item' . $options . ');'); | |
296 } | |
297 return 'var jsDrag = new Draggable(' . $this->selection . $options . ');'; | |
298 } | |
299 | |
300 /** | |
301 * Create a Droppable element. | |
302 * | |
303 * #### Note: Requires scriptaculous to be loaded. | |
304 * | |
305 * @param array $options Array of options for the droppable. | |
306 * @return string Completed droppable script. | |
307 * @access public | |
308 * @see JsBaseEngineHelper::droppable() for options list. | |
309 */ | |
310 function drop($options = array()) { | |
311 $options = $this->_processOptions('drop', $options); | |
312 if (!empty($options)) { | |
313 $options = ', {' . $options . '}'; | |
314 } | |
315 return 'Droppables.add(' . $this->selection . $options . ');'; | |
316 } | |
317 | |
318 /** | |
319 * Creates a slider control widget. | |
320 * | |
321 * ### Note: Requires scriptaculous to be loaded. | |
322 * | |
323 * @param array $options Array of options for the slider. | |
324 * @return string Completed slider script. | |
325 * @access public | |
326 * @see JsBaseEngineHelper::slider() for options list. | |
327 */ | |
328 function slider($options = array()) { | |
329 $slider = $this->selection; | |
330 $this->get($options['handle']); | |
331 unset($options['handle']); | |
332 | |
333 if (isset($options['min']) && isset($options['max'])) { | |
334 $options['range'] = sprintf('$R(%s,%s)', $options['min'], $options['max']); | |
335 unset($options['min'], $options['max']); | |
336 } | |
337 $options = $this->_mapOptions('slider', $options); | |
338 $options = $this->_prepareCallbacks('slider', $options); | |
339 $optionString = $this->_parseOptions( | |
340 $options, array_merge(array_keys($this->_callbackArguments['slider']), array('range')) | |
341 ); | |
342 if (!empty($optionString)) { | |
343 $optionString = ', {' . $optionString . '}'; | |
344 } | |
345 $out = 'var jsSlider = new Control.Slider(' . $this->selection . ', ' . $slider . $optionString . ');'; | |
346 $this->selection = $slider; | |
347 return $out; | |
348 } | |
349 | |
350 /** | |
351 * Serialize the form attached to $selector. | |
352 * | |
353 * @param array $options Array of options. | |
354 * @return string Completed serializeForm() snippet | |
355 * @access public | |
356 * @see JsBaseEngineHelper::serializeForm() | |
357 */ | |
358 function serializeForm($options = array()) { | |
359 $options = array_merge(array('isForm' => false, 'inline' => false), $options); | |
360 $selection = $this->selection; | |
361 if (!$options['isForm']) { | |
362 $selection = '$(' . $this->selection . '.form)'; | |
363 } | |
364 $method = '.serialize()'; | |
365 if (!$options['inline']) { | |
366 $method .= ';'; | |
367 } | |
368 return $selection . $method; | |
369 } | |
370 } |