summaryrefslogtreecommitdiff
path: root/poc/poc02-compiling-cake/src/vendor/cakephp-2.2.1-0-gcc44130/lib/Cake/View/Helper/MootoolsEngineHelper.php
blob: 3919754fe90d5898150c01cdf26ba49a4c4880b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?php
/**
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       Cake.View.Helper
 * @since         CakePHP(tm) v 1.3
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

App::uses('JsBaseEngineHelper', 'View/Helper');

/**
 * MooTools Engine Helper for JsHelper
 *
 * Provides MooTools specific Javascript for JsHelper.
 * Assumes that you have the following MooTools packages
 *
 * - Remote, Remote.HTML, Remote.JSON
 * - Fx, Fx.Tween, Fx.Morph
 * - Selectors, DomReady,
 * - Drag, Drag.Move
 *
 * @package       Cake.View.Helper
 */
class MootoolsEngineHelper extends JsBaseEngineHelper {

/**
 * Option mappings for MooTools
 *
 * @var array
 */
	protected $_optionMap = array(
		'request' => array(
			'complete' => 'onComplete',
			'success' => 'onSuccess',
			'before' => 'onRequest',
			'error' => 'onFailure'
		),
		'sortable' => array(
			'distance' => 'snap',
			'containment' => 'constrain',
			'sort' => 'onSort',
			'complete' => 'onComplete',
			'start' => 'onStart',
		),
		'drag' => array(
			'snapGrid' => 'snap',
			'start' => 'onStart',
			'drag' => 'onDrag',
			'stop' => 'onComplete',
		),
		'drop' => array(
			'drop' => 'onDrop',
			'hover' => 'onEnter',
			'leave' => 'onLeave',
		),
		'slider' => array(
			'complete' => 'onComplete',
			'change' => 'onChange',
			'direction' => 'mode',
			'step' => 'steps'
		)
	);

/**
 * Contains a list of callback names -> default arguments.
 *
 * @var array
 */
	protected $_callbackArguments = array(
		'slider' => array(
			'onTick' => 'position',
			'onChange' => 'step',
			'onComplete' => 'event'
		),
		'request' => array(
			'onRequest' => '',
			'onComplete' => '',
			'onCancel' => '',
			'onSuccess' => 'responseText, responseXML',
			'onFailure' => 'xhr',
			'onException' => 'headerName, value',
		),
		'drag' => array(
			'onBeforeStart' => 'element',
			'onStart' => 'element',
			'onSnap' => 'element',
			'onDrag' => 'element, event',
			'onComplete' => 'element, event',
			'onCancel' => 'element',
		),
		'drop' => array(
			'onBeforeStart' => 'element',
			'onStart' => 'element',
			'onSnap' => 'element',
			'onDrag' => 'element, event',
			'onComplete' => 'element, event',
			'onCancel' => 'element',
			'onDrop' => 'element, droppable, event',
			'onLeave' => 'element, droppable',
			'onEnter' => 'element, droppable',
		),
		'sortable' => array(
			'onStart' => 'element, clone',
			'onSort' => 'element, clone',
			'onComplete' => 'element',
		)
	);

/**
 * Create javascript selector for a CSS rule
 *
 * @param string $selector The selector that is targeted
 * @return MootoolsEngineHelper instance of $this. Allows chained methods.
 */
	public function get($selector) {
		$this->_multipleSelection = false;
		if ($selector == 'window' || $selector == 'document') {
			$this->selection = "$(" . $selector . ")";
			return $this;
		}
		if (preg_match('/^#[^\s.]+$/', $selector)) {
			$this->selection = '$("' . substr($selector, 1) . '")';
			return $this;
		}
		$this->_multipleSelection = true;
		$this->selection = '$$("' . $selector . '")';
		return $this;
	}

/**
 * Add an event to the script cache. Operates on the currently selected elements.
 *
 * ### Options
 *
 * - 'wrap' - Whether you want the callback wrapped in an anonymous function. (defaults true)
 * - 'stop' - Whether you want the event to stopped. (defaults true)
 *
 * @param string $type Type of event to bind to the current dom id
 * @param string $callback The Javascript function you wish to trigger or the function literal
 * @param array $options Options for the event.
 * @return string completed event handler
 */
	public function event($type, $callback, $options = array()) {
		$defaults = array('wrap' => true, 'stop' => true);
		$options = array_merge($defaults, $options);

		$function = 'function (event) {%s}';
		if ($options['wrap'] && $options['stop']) {
			$callback = "event.stop();\n" . $callback;
		}
		if ($options['wrap']) {
			$callback = sprintf($function, $callback);
		}
		$out = $this->selection . ".addEvent(\"{$type}\", $callback);";
		return $out;
	}

/**
 * Create a domReady event. This is a special event in many libraries
 *
 * @param string $functionBody The code to run on domReady
 * @return string completed domReady method
 */
	public function domReady($functionBody) {
		$this->selection = 'window';
		return $this->event('domready', $functionBody, array('stop' => false));
	}

/**
 * Create an iteration over the current selection result.
 *
 * @param string $callback The function body you wish to apply during the iteration.
 * @return string completed iteration
 */
	public function each($callback) {
		return $this->selection . '.each(function (item, index) {' . $callback . '});';
	}

/**
 * Trigger an Effect.
 *
 * @param string $name The name of the effect to trigger.
 * @param array $options Array of options for the effect.
 * @return string completed string with effect.
 * @see JsBaseEngineHelper::effect()
 */
	public function effect($name, $options = array()) {
		$speed = null;
		if (isset($options['speed']) && in_array($options['speed'], array('fast', 'slow'))) {
			if ($options['speed'] == 'fast') {
				$speed = '"short"';
			} elseif ($options['speed'] == 'slow') {
				$speed = '"long"';
			}
		}
		$effect = '';
		switch ($name) {
			case 'hide':
				$effect = 'setStyle("display", "none")';
			break;
			case 'show':
				$effect = 'setStyle("display", "")';
			break;
			case 'fadeIn':
			case 'fadeOut':
			case 'slideIn':
			case 'slideOut':
				list($effectName, $direction) = preg_split('/([A-Z][a-z]+)/', $name, -1, PREG_SPLIT_DELIM_CAPTURE);
				$direction = strtolower($direction);
				if ($speed) {
					$effect .= "set(\"$effectName\", {duration:$speed}).";
				}
				$effect .= "$effectName(\"$direction\")";
			break;
		}
		return $this->selection . '.' . $effect . ';';
	}

/**
 * Create an new Request.
 *
 * Requires `Request`.  If you wish to use 'update' key you must have ```Request.HTML```
 * if you wish to do Json requests you will need ```JSON``` and ```Request.JSON```.
 *
 * @param string|array $url
 * @param array $options
 * @return string The completed ajax call.
 */
	public function request($url, $options = array()) {
		$url = $this->url($url);
		$options = $this->_mapOptions('request', $options);
		$type = $data = null;
		if (isset($options['type']) || isset($options['update'])) {
			if (isset($options['type']) && strtolower($options['type']) == 'json') {
				$type = '.JSON';
			}
			if (isset($options['update'])) {
				$options['update'] = str_replace('#', '', $options['update']);
				$type = '.HTML';
			}
			unset($options['type']);
		}
		if (!empty($options['data'])) {
			$data = $options['data'];
			unset($options['data']);
		}
		$options['url'] = $url;
		$options = $this->_prepareCallbacks('request', $options);
		if (!empty($options['dataExpression'])) {
			$callbacks[] = 'data';
			unset($options['dataExpression']);
		} elseif (!empty($data)) {
			$data = $this->object($data);
		}
		$options = $this->_parseOptions($options, array_keys($this->_callbackArguments['request']));
		return "var jsRequest = new Request$type({{$options}}).send($data);";
	}

/**
 * Create a sortable element.
 *
 * Requires the `Sortables` plugin from MootoolsMore
 *
 * @param array $options Array of options for the sortable.
 * @return string Completed sortable script.
 * @see JsBaseEngineHelper::sortable() for options list.
 */
	public function sortable($options = array()) {
		$options = $this->_processOptions('sortable', $options);
		return 'var jsSortable = new Sortables(' . $this->selection . ', {' . $options . '});';
	}

/**
 * Create a Draggable element.
 *
 * Requires the `Drag` plugin from MootoolsMore
 *
 * @param array $options Array of options for the draggable.
 * @return string Completed draggable script.
 * @see JsHelper::drag() for options list.
 */
	public function drag($options = array()) {
		$options = $this->_processOptions('drag', $options);
		return $this->selection . '.makeDraggable({' . $options . '});';
	}

/**
 * Create a Droppable element.
 *
 * Requires the `Drag` and `Drag.Move` plugins from MootoolsMore
 *
 * Droppables in Mootools function differently from other libraries.  Droppables
 * are implemented as an extension of Drag.  So in addition to making a get() selection for
 * the droppable element. You must also provide a selector rule to the draggable element. Furthermore,
 * Mootools droppables inherit all options from Drag.
 *
 * @param array $options Array of options for the droppable.
 * @return string Completed droppable script.
 * @see JsBaseEngineHelper::drop() for options list.
 */
	public function drop($options = array()) {
		if (empty($options['drag'])) {
			trigger_error(
				__d('cake_dev', 'MootoolsEngine::drop() requires a "drag" option to properly function'), E_USER_WARNING
			);
			return false;
		}
		$options['droppables'] = $this->selection;

		$this->get($options['drag']);
		unset($options['drag']);

		$options = $this->_mapOptions('drag', $this->_mapOptions('drop', $options));
		$options = $this->_prepareCallbacks('drop', $options);
		$safe = array_merge(array_keys($this->_callbackArguments['drop']), array('droppables'));
		$optionString = $this->_parseOptions($options, $safe);
		$out = $this->selection . '.makeDraggable({' . $optionString . '});';
		$this->selection = $options['droppables'];
		return $out;
	}

/**
 * Create a slider control
 *
 * Requires `Slider` from MootoolsMore
 *
 * @param array $options Array of options for the slider.
 * @return string Completed slider script.
 * @see JsBaseEngineHelper::slider() for options list.
 */
	public function slider($options = array()) {
		$slider = $this->selection;
		$this->get($options['handle']);
		unset($options['handle']);

		if (isset($options['min']) && isset($options['max'])) {
			$options['range'] = array($options['min'], $options['max']);
			unset($options['min'], $options['max']);
		}
		$optionString = $this->_processOptions('slider', $options);
		if (!empty($optionString)) {
			$optionString = ', {' . $optionString . '}';
		}
		$out = 'var jsSlider = new Slider(' . $slider . ', ' . $this->selection . $optionString . ');';
		$this->selection = $slider;
		return $out;
	}

/**
 * Serialize the form attached to $selector.
 *
 * @param array $options Array of options.
 * @return string Completed serializeForm() snippet
 * @see JsBaseEngineHelper::serializeForm()
 */
	public function serializeForm($options = array()) {
		$options = array_merge(array('isForm' => false, 'inline' => false), $options);
		$selection = $this->selection;
		if (!$options['isForm']) {
			$selection = '$(' . $this->selection . '.form)';
		}
		$method = '.toQueryString()';
		if (!$options['inline']) {
			$method .= ';';
		}
		return $selection . $method;
	}

}