3
* MooTools Engine Helper for JsHelper
5
* Provides MooTools specific Javascript for JsHelper.
6
* Assumes that you have the following MooTools packages
8
* - Remote, Remote.HTML, Remote.JSON
9
* - Fx, Fx.Tween, Fx.Morph
10
* - Selectors, DomReady,
13
* PHP versions 4 and 5
15
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
16
* Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
18
* Licensed under The MIT License
19
* Redistributions of files must retain the above copyright notice.
21
* @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
22
* @link http://cakephp.org CakePHP(tm) Project
24
* @subpackage cake.libs.view.helpers
25
* @since CakePHP(tm) v 1.3
26
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
28
App::import('Helper', 'Js');
30
class MootoolsEngineHelper extends JsBaseEngineHelper {
32
* Option mappings for MooTools
36
var $_optionMap = array(
38
'complete' => 'onComplete',
39
'success' => 'onSuccess',
40
'before' => 'onRequest',
41
'error' => 'onFailure'
45
'containment' => 'constrain',
47
'complete' => 'onComplete',
54
'stop' => 'onComplete',
62
'complete' => 'onComplete',
63
'change' => 'onChange',
64
'direction' => 'mode',
70
* Contains a list of callback names -> default arguments.
74
var $_callbackArguments = array(
76
'onTick' => 'position',
78
'onComplete' => 'event'
84
'onSuccess' => 'responseText, responseXML',
86
'onException' => 'headerName, value',
89
'onBeforeStart' => 'element',
90
'onStart' => 'element',
91
'onSnap' => 'element',
92
'onDrag' => 'element, event',
93
'onComplete' => 'element, event',
94
'onCancel' => 'element',
97
'onBeforeStart' => 'element',
98
'onStart' => 'element',
99
'onSnap' => 'element',
100
'onDrag' => 'element, event',
101
'onComplete' => 'element, event',
102
'onCancel' => 'element',
103
'onDrop' => 'element, droppable, event',
104
'onLeave' => 'element, droppable',
105
'onEnter' => 'element, droppable',
108
'onStart' => 'element, clone',
109
'onSort' => 'element, clone',
110
'onComplete' => 'element',
115
* Create javascript selector for a CSS rule
117
* @param string $selector The selector that is targeted
118
* @return object instance of $this. Allows chained methods.
120
function get($selector) {
121
$this->_multipleSelection = false;
122
if ($selector == 'window' || $selector == 'document') {
123
$this->selection = "$(" . $selector .")";
126
if (preg_match('/^#[^\s.]+$/', $selector)) {
127
$this->selection = '$("' . substr($selector, 1) . '")';
130
$this->_multipleSelection = true;
131
$this->selection = '$$("' . $selector . '")';
136
* Add an event to the script cache. Operates on the currently selected elements.
140
* - 'wrap' - Whether you want the callback wrapped in an anonymous function. (defaults true)
141
* - 'stop' - Whether you want the event to stopped. (defaults true)
143
* @param string $type Type of event to bind to the current dom id
144
* @param string $callback The Javascript function you wish to trigger or the function literal
145
* @param array $options Options for the event.
146
* @return string completed event handler
148
function event($type, $callback, $options = array()) {
149
$defaults = array('wrap' => true, 'stop' => true);
150
$options = array_merge($defaults, $options);
152
$function = 'function (event) {%s}';
153
if ($options['wrap'] && $options['stop']) {
154
$callback = "event.stop();\n" . $callback;
156
if ($options['wrap']) {
157
$callback = sprintf($function, $callback);
159
$out = $this->selection . ".addEvent(\"{$type}\", $callback);";
164
* Create a domReady event. This is a special event in many libraries
166
* @param string $functionBody The code to run on domReady
167
* @return string completed domReady method
169
function domReady($functionBody) {
170
$this->selection = 'window';
171
return $this->event('domready', $functionBody, array('stop' => false));
175
* Create an iteration over the current selection result.
177
* @param string $method The method you want to apply to the selection
178
* @param string $callback The function body you wish to apply during the iteration.
179
* @return string completed iteration
181
function each($callback) {
182
return $this->selection . '.each(function (item, index) {' . $callback . '});';
188
* @param string $name The name of the effect to trigger.
189
* @param array $options Array of options for the effect.
190
* @return string completed string with effect.
191
* @see JsBaseEngineHelper::effect()
193
function effect($name, $options = array()) {
195
if (isset($options['speed']) && in_array($options['speed'], array('fast', 'slow'))) {
196
if ($options['speed'] == 'fast') {
198
} elseif ($options['speed'] == 'slow') {
205
$effect = 'setStyle("display", "none")';
208
$effect = 'setStyle("display", "")';
214
list($effectName, $direction) = preg_split('/([A-Z][a-z]+)/', $name, -1, PREG_SPLIT_DELIM_CAPTURE);
215
$direction = strtolower($direction);
217
$effect .= "set(\"$effectName\", {duration:$speed}).";
219
$effect .= "$effectName(\"$direction\")";
222
return $this->selection . '.' . $effect . ';';
226
* Create an new Request.
228
* Requires `Request`. If you wish to use 'update' key you must have ```Request.HTML```
229
* if you wish to do Json requests you will need ```JSON``` and ```Request.JSON```.
232
* @param array $options
233
* @return string The completed ajax call.
235
function request($url, $options = array()) {
236
$url = $this->url($url);
237
$options = $this->_mapOptions('request', $options);
238
$type = $data = null;
239
if (isset($options['type']) || isset($options['update'])) {
240
if (isset($options['type']) && strtolower($options['type']) == 'json') {
243
if (isset($options['update'])) {
244
$options['update'] = str_replace('#', '', $options['update']);
247
unset($options['type']);
249
if (!empty($options['data'])) {
250
$data = $options['data'];
251
unset($options['data']);
253
$options['url'] = $url;
254
$options = $this->_prepareCallbacks('request', $options);
255
if (isset($options['dataExpression'])) {
256
$callbacks[] = 'data';
257
unset($options['dataExpression']);
258
} elseif (!empty($data)) {
259
$data = $this->object($data);
261
$options = $this->_parseOptions($options, array_keys($this->_callbackArguments['request']));
262
return "var jsRequest = new Request$type({{$options}}).send($data);";
266
* Create a sortable element.
268
* Requires the `Sortables` plugin from MootoolsMore
270
* @param array $options Array of options for the sortable.
271
* @return string Completed sortable script.
272
* @see JsBaseEngineHelper::sortable() for options list.
274
function sortable($options = array()) {
275
$options = $this->_processOptions('sortable', $options);
276
return 'var jsSortable = new Sortables(' . $this->selection . ', {' . $options . '});';
280
* Create a Draggable element.
282
* Requires the `Drag` plugin from MootoolsMore
284
* @param array $options Array of options for the draggable.
285
* @return string Completed draggable script.
286
* @see JsHelper::drag() for options list.
288
function drag($options = array()) {
289
$options = $this->_processOptions('drag', $options);
290
return $this->selection . '.makeDraggable({' . $options . '});';
294
* Create a Droppable element.
296
* Requires the `Drag` and `Drag.Move` plugins from MootoolsMore
298
* Droppables in Mootools function differently from other libraries. Droppables
299
* are implemented as an extension of Drag. So in addtion to making a get() selection for
300
* the droppable element. You must also provide a selector rule to the draggable element. Furthermore,
301
* Mootools droppables inherit all options from Drag.
303
* @param array $options Array of options for the droppable.
304
* @return string Completed droppable script.
305
* @see JsBaseEngineHelper::drop() for options list.
307
function drop($options = array()) {
308
if (empty($options['drag'])) {
310
__('MootoolsEngine::drop() requires a "drag" option to properly function', true), E_USER_WARNING
314
$options['droppables'] = $this->selection;
316
$this->get($options['drag']);
317
unset($options['drag']);
319
$options = $this->_mapOptions('drag', $this->_mapOptions('drop', $options));
320
$options = $this->_prepareCallbacks('drop', $options);
321
$safe = array_merge(array_keys($this->_callbackArguments['drop']), array('droppables'));
322
$optionString = $this->_parseOptions($options, $safe);
323
$out = $this->selection . '.makeDraggable({' . $optionString . '});';
324
$this->selection = $options['droppables'];
329
* Create a slider control
331
* Requires `Slider` from MootoolsMore
333
* @param array $options Array of options for the slider.
334
* @return string Completed slider script.
335
* @see JsBaseEngineHelper::slider() for options list.
337
function slider($options = array()) {
338
$slider = $this->selection;
339
$this->get($options['handle']);
340
unset($options['handle']);
342
if (isset($options['min']) && isset($options['max'])) {
343
$options['range'] = array($options['min'], $options['max']);
344
unset($options['min'], $options['max']);
346
$optionString = $this->_processOptions('slider', $options);
347
if (!empty($optionString)) {
348
$optionString = ', {' . $optionString . '}';
350
$out = 'var jsSlider = new Slider(' . $slider . ', ' . $this->selection . $optionString . ');';
351
$this->selection = $slider;
356
* Serialize the form attached to $selector.
358
* @param array $options Array of options.
359
* @return string Completed serializeForm() snippet
360
* @see JsBaseEngineHelper::serializeForm()
362
function serializeForm($options = array()) {
363
$options = array_merge(array('isForm' => false, 'inline' => false), $options);
364
$selection = $this->selection;
365
if (!$options['isForm']) {
366
$selection = '$(' . $this->selection . '.form)';
368
$method = '.toQueryString()';
369
if (!$options['inline']) {
372
return $selection . $method;