Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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