~tsep-dev/tsep/0.9-beta

« back to all changes in this revision

Viewing changes to branches/symfony/cake/libs/view/helpers/mootools_engine.php

  • Committer: geoffreyfishing
  • Date: 2011-01-11 23:46:12 UTC
  • Revision ID: svn-v4:ae0de26e-ed09-4cbe-9a20-e40b4c60ac6c::125
Created a symfony branch for future migration to symfony

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * MooTools Engine Helper for JsHelper
 
4
 *
 
5
 * Provides MooTools specific Javascript for JsHelper.
 
6
 * Assumes that you have the following MooTools packages
 
7
 *
 
8
 * - Remote, Remote.HTML, Remote.JSON
 
9
 * - Fx, Fx.Tween, Fx.Morph
 
10
 * - Selectors, DomReady,
 
11
 * - Drag, Drag.Move
 
12
 *
 
13
 * PHP versions 4 and 5
 
14
 *
 
15
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 
16
 * Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
 
17
 *
 
18
 * Licensed under The MIT License
 
19
 * Redistributions of files must retain the above copyright notice.
 
20
 *
 
21
 * @copyright     Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
 
22
 * @link          http://cakephp.org CakePHP(tm) Project
 
23
 * @package       cake
 
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)
 
27
 */
 
28
App::import('Helper', 'Js');
 
29
 
 
30
class MootoolsEngineHelper extends JsBaseEngineHelper {
 
31
/**
 
32
 * Option mappings for MooTools
 
33
 *
 
34
 * @var array
 
35
 */
 
36
        var $_optionMap = array(
 
37
                'request' => array(
 
38
                        'complete' => 'onComplete',
 
39
                        'success' => 'onSuccess',
 
40
                        'before' => 'onRequest',
 
41
                        'error' => 'onFailure'
 
42
                ),
 
43
                'sortable' => array(
 
44
                        'distance' => 'snap',
 
45
                        'containment' => 'constrain',
 
46
                        'sort' => 'onSort',
 
47
                        'complete' => 'onComplete',
 
48
                        'start' => 'onStart',
 
49
                ),
 
50
                'drag' => array(
 
51
                        'snapGrid' => 'snap',
 
52
                        'start' => 'onStart',
 
53
                        'drag' => 'onDrag',
 
54
                        'stop' => 'onComplete',
 
55
                ),
 
56
                'drop' => array(
 
57
                        'drop' => 'onDrop',
 
58
                        'hover' => 'onEnter',
 
59
                        'leave' => 'onLeave',
 
60
                ),
 
61
                'slider' => array(
 
62
                        'complete' => 'onComplete',
 
63
                        'change' => 'onChange',
 
64
                        'direction' => 'mode',
 
65
                        'step' => 'steps'
 
66
                )
 
67
        );
 
68
 
 
69
/**
 
70
 * Contains a list of callback names -> default arguments.
 
71
 *
 
72
 * @var array
 
73
 */
 
74
        var $_callbackArguments = array(
 
75
                'slider' => array(
 
76
                        'onTick' => 'position',
 
77
                        'onChange' => 'step',
 
78
                        'onComplete' => 'event'
 
79
                ),
 
80
                'request' => array(
 
81
                        'onRequest' => '',
 
82
                        'onComplete' => '',
 
83
                        'onCancel' => '',
 
84
                        'onSuccess' => 'responseText, responseXML',
 
85
                        'onFailure' => 'xhr',
 
86
                        'onException' => 'headerName, value',
 
87
                ),
 
88
                'drag' => array(
 
89
                        'onBeforeStart' => 'element',
 
90
                        'onStart' => 'element',
 
91
                        'onSnap' => 'element',
 
92
                        'onDrag' => 'element, event',
 
93
                        'onComplete' => 'element, event',
 
94
                        'onCancel' => 'element',
 
95
                ),
 
96
                'drop' => array(
 
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',
 
106
                ),
 
107
                'sortable' => array(
 
108
                        'onStart' => 'element, clone',
 
109
                        'onSort' => 'element, clone',
 
110
                        'onComplete' => 'element',
 
111
                )
 
112
        );
 
113
 
 
114
/**
 
115
 * Create javascript selector for a CSS rule
 
116
 *
 
117
 * @param string $selector The selector that is targeted
 
118
 * @return object instance of $this. Allows chained methods.
 
119
 */
 
120
        function get($selector) {
 
121
                $this->_multipleSelection = false;
 
122
                if ($selector == 'window' || $selector == 'document') {
 
123
                        $this->selection = "$(" . $selector .")";
 
124
                        return $this;
 
125
                }
 
126
                if (preg_match('/^#[^\s.]+$/', $selector)) {
 
127
                        $this->selection = '$("' . substr($selector, 1) . '")';
 
128
                        return $this;
 
129
                }
 
130
                $this->_multipleSelection = true;
 
131
                $this->selection = '$$("' . $selector . '")';
 
132
                return $this;
 
133
        }
 
134
 
 
135
/**
 
136
 * Add an event to the script cache. Operates on the currently selected elements.
 
137
 *
 
138
 * ### Options
 
139
 *
 
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)
 
142
 *
 
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
 
147
 */
 
148
        function event($type, $callback, $options = array()) {
 
149
                $defaults = array('wrap' => true, 'stop' => true);
 
150
                $options = array_merge($defaults, $options);
 
151
 
 
152
                $function = 'function (event) {%s}';
 
153
                if ($options['wrap'] && $options['stop']) {
 
154
                        $callback = "event.stop();\n" . $callback;
 
155
                }
 
156
                if ($options['wrap']) {
 
157
                        $callback = sprintf($function, $callback);
 
158
                }
 
159
                $out = $this->selection . ".addEvent(\"{$type}\", $callback);";
 
160
                return $out;
 
161
        }
 
162
 
 
163
/**
 
164
 * Create a domReady event. This is a special event in many libraries
 
165
 *
 
166
 * @param string $functionBody The code to run on domReady
 
167
 * @return string completed domReady method
 
168
 */
 
169
        function domReady($functionBody) {
 
170
                $this->selection = 'window';
 
171
                return $this->event('domready', $functionBody, array('stop' => false));
 
172
        }
 
173
 
 
174
/**
 
175
 * Create an iteration over the current selection result.
 
176
 *
 
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
 
180
 */
 
181
        function each($callback) {
 
182
                return $this->selection . '.each(function (item, index) {' . $callback . '});';
 
183
        }
 
184
 
 
185
/**
 
186
 * Trigger an Effect.
 
187
 *
 
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()
 
192
 */
 
193
        function effect($name, $options = array()) {
 
194
                $speed = null;
 
195
                if (isset($options['speed']) && in_array($options['speed'], array('fast', 'slow'))) {
 
196
                        if ($options['speed'] == 'fast') {
 
197
                                $speed = '"short"';
 
198
                        } elseif ($options['speed'] == 'slow') {
 
199
                                $speed = '"long"';
 
200
                        }
 
201
                }
 
202
                $effect = '';
 
203
                switch ($name) {
 
204
                        case 'hide':
 
205
                                $effect = 'setStyle("display", "none")';
 
206
                        break;
 
207
                        case 'show':
 
208
                                $effect = 'setStyle("display", "")';
 
209
                        break;
 
210
                        case 'fadeIn':
 
211
                        case 'fadeOut':
 
212
                        case 'slideIn':
 
213
                        case 'slideOut':
 
214
                                list($effectName, $direction) = preg_split('/([A-Z][a-z]+)/', $name, -1, PREG_SPLIT_DELIM_CAPTURE);
 
215
                                $direction = strtolower($direction);
 
216
                                if ($speed) {
 
217
                                        $effect .= "set(\"$effectName\", {duration:$speed}).";
 
218
                                }
 
219
                                $effect .= "$effectName(\"$direction\")";
 
220
                        break;
 
221
                }
 
222
                return $this->selection . '.' . $effect . ';';
 
223
        }
 
224
 
 
225
/**
 
226
 * Create an new Request.
 
227
 *
 
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```.
 
230
 *
 
231
 * @param mixed $url
 
232
 * @param array $options
 
233
 * @return string The completed ajax call.
 
234
 */
 
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') {
 
241
                                $type = '.JSON';
 
242
                        }
 
243
                        if (isset($options['update'])) {
 
244
                                $options['update'] = str_replace('#', '', $options['update']);
 
245
                                $type = '.HTML';
 
246
                        }
 
247
                        unset($options['type']);
 
248
                }
 
249
                if (!empty($options['data'])) {
 
250
                        $data = $options['data'];
 
251
                        unset($options['data']);
 
252
                }
 
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);
 
260
                }
 
261
                $options = $this->_parseOptions($options, array_keys($this->_callbackArguments['request']));
 
262
                return "var jsRequest = new Request$type({{$options}}).send($data);";
 
263
        }
 
264
 
 
265
/**
 
266
 * Create a sortable element.
 
267
 *
 
268
 * Requires the `Sortables` plugin from MootoolsMore
 
269
 *
 
270
 * @param array $options Array of options for the sortable.
 
271
 * @return string Completed sortable script.
 
272
 * @see JsBaseEngineHelper::sortable() for options list.
 
273
 */
 
274
        function sortable($options = array()) {
 
275
                $options = $this->_processOptions('sortable', $options);
 
276
                return 'var jsSortable = new Sortables(' . $this->selection . ', {' . $options . '});';
 
277
        }
 
278
 
 
279
/**
 
280
 * Create a Draggable element.
 
281
 *
 
282
 * Requires the `Drag` plugin from MootoolsMore
 
283
 *
 
284
 * @param array $options Array of options for the draggable.
 
285
 * @return string Completed draggable script.
 
286
 * @see JsHelper::drag() for options list.
 
287
 */
 
288
        function drag($options = array()) {
 
289
                $options = $this->_processOptions('drag', $options);
 
290
                return $this->selection . '.makeDraggable({' . $options . '});';
 
291
        }
 
292
 
 
293
/**
 
294
 * Create a Droppable element.
 
295
 *
 
296
 * Requires the `Drag` and `Drag.Move` plugins from MootoolsMore
 
297
 *
 
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.
 
302
 *
 
303
 * @param array $options Array of options for the droppable.
 
304
 * @return string Completed droppable script.
 
305
 * @see JsBaseEngineHelper::drop() for options list.
 
306
 */
 
307
        function drop($options = array()) {
 
308
                if (empty($options['drag'])) {
 
309
                        trigger_error(
 
310
                                __('MootoolsEngine::drop() requires a "drag" option to properly function', true), E_USER_WARNING
 
311
                        );
 
312
                        return false;
 
313
                }
 
314
                $options['droppables'] = $this->selection;
 
315
 
 
316
                $this->get($options['drag']);
 
317
                unset($options['drag']);
 
318
 
 
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'];
 
325
                return $out;
 
326
        }
 
327
 
 
328
/**
 
329
 * Create a slider control
 
330
 *
 
331
 * Requires `Slider` from MootoolsMore
 
332
 *
 
333
 * @param array $options Array of options for the slider.
 
334
 * @return string Completed slider script.
 
335
 * @see JsBaseEngineHelper::slider() for options list.
 
336
 */
 
337
        function slider($options = array()) {
 
338
                $slider = $this->selection;
 
339
                $this->get($options['handle']);
 
340
                unset($options['handle']);
 
341
 
 
342
                if (isset($options['min']) && isset($options['max'])) {
 
343
                        $options['range'] = array($options['min'], $options['max']);
 
344
                        unset($options['min'], $options['max']);
 
345
                }
 
346
                $optionString = $this->_processOptions('slider', $options);
 
347
                if (!empty($optionString)) {
 
348
                        $optionString = ', {' . $optionString . '}';
 
349
                }
 
350
                $out = 'var jsSlider = new Slider(' . $slider . ', ' . $this->selection . $optionString . ');';
 
351
                $this->selection = $slider;
 
352
                return $out;
 
353
        }
 
354
 
 
355
/**
 
356
 * Serialize the form attached to $selector.
 
357
 *
 
358
 * @param array $options Array of options.
 
359
 * @return string Completed serializeForm() snippet
 
360
 * @see JsBaseEngineHelper::serializeForm()
 
361
 */
 
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)';
 
367
                }
 
368
                $method = '.toQueryString()';
 
369
                if (!$options['inline']) {
 
370
                        $method .= ';';
 
371
                }
 
372
                return $selection . $method;
 
373
        }
 
374
}