~tsep-dev/tsep/0.9-beta

« back to all changes in this revision

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