3
* Prototype Engine Helper for JsHelper
5
* Provides Prototype specific Javascript for JsHelper. Requires at least
10
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
11
* Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
13
* Licensed under The MIT License
14
* Redistributions of files must retain the above copyright notice.
16
* @copyright Copyright 2005-2009, Cake Software Foundation, Inc. (http://cakefoundation.org)
17
* @link http://cakephp.org CakePHP(tm) Project
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)
23
App::import('Helper', 'Js');
25
class PrototypeEngineHelper extends JsBaseEngineHelper {
27
* Is the current selection a multiple selection? or is it just a single element.
31
var $_multiple = false;
34
* Option mappings for Prototype
38
var $_optionMap = array(
40
'async' => 'asynchronous',
41
'data' => 'parameters',
42
'before' => 'onCreate',
43
'success' => 'onSuccess',
44
'complete' => 'onComplete',
45
'error' => 'onFailure'
50
'complete' => 'onUpdate',
55
'container' => 'constraint',
63
'hoverClass' => 'hoverclass',
66
'direction' => 'axis',
67
'change' => 'onSlide',
68
'complete' => 'onChange',
69
'value' => 'sliderValue',
74
* Contains a list of callback names -> default arguments.
78
var $_callbackArguments = array(
81
'onChange' => 'value',
86
'change' => 'draggable',
90
'onHover' => 'draggable, droppable, event',
91
'onDrop' => 'draggable, droppable, event',
94
'onCreate' => 'transport',
95
'onComplete' => 'transport',
96
'onFailure' => 'response, jsonHeader',
97
'onRequest' => 'transport',
98
'onSuccess' => 'response, jsonHeader'
101
'onStart' => 'element',
102
'onChange' => 'element',
103
'onUpdate' => 'element',
108
* Create javascript selector for a CSS rule
110
* @param string $selector The selector that is targeted
111
* @return object instance of $this. Allows chained methods.
113
function get($selector) {
114
$this->_multiple = false;
115
if ($selector == 'window' || $selector == 'document') {
116
$this->selection = "$(" . $selector .")";
119
if (preg_match('/^#[^\s.]+$/', $selector)) {
120
$this->selection = '$("' . substr($selector, 1) . '")';
123
$this->_multiple = true;
124
$this->selection = '$$("' . $selector . '")';
129
* Add an event to the script cache. Operates on the currently selected elements.
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)
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
141
function event($type, $callback, $options = array()) {
142
$defaults = array('wrap' => true, 'stop' => true);
143
$options = array_merge($defaults, $options);
145
$function = 'function (event) {%s}';
146
if ($options['wrap'] && $options['stop']) {
147
$callback = "event.stop();\n" . $callback;
149
if ($options['wrap']) {
150
$callback = sprintf($function, $callback);
152
$out = $this->selection . ".observe(\"{$type}\", $callback);";
157
* Create a domReady event. This is a special event in many libraries
159
* @param string $functionBody The code to run on domReady
160
* @return string completed domReady method
163
function domReady($functionBody) {
164
$this->selection = 'document';
165
return $this->event('dom:loaded', $functionBody, array('stop' => false));
169
* Create an iteration over the current selection result.
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
176
function each($callback) {
177
return $this->selection . '.each(function (item, index) {' . $callback . '});';
183
* ### Note: Effects require Scriptaculous to be loaded.
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.
189
* @see JsBaseEngineHelper::effect()
191
function effect($name, $options = array()) {
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;
200
$options['duration'] = 1;
202
unset($options['speed']);
204
if (!empty($options)) {
205
$optionString = ', {' . $this->_parseOptions($options) . '}';
210
$effect = $this->selection . '.' . $name . '();';
214
$name = ($name == 'slideIn') ? 'SlideDown' : 'SlideUp';
215
$effect = 'Effect.' . $name . '(' . $this->selection . $optionString . ');';
219
$name = ($name == 'fadeIn') ? 'appear' : 'fade';
220
$effect = $this->selection . '.' . $name .'(' . substr($optionString, 2) . ');';
227
* Create an Ajax or Ajax.Updater call.
230
* @param array $options
231
* @return string The completed ajax call.
234
function request($url, $options = array()) {
235
$url = '"'. $this->url($url) . '"';
236
$options = $this->_mapOptions('request', $options);
239
if (isset($options['type']) && strtolower($options['type']) == 'json') {
240
unset($options['type']);
242
if (isset($options['update'])) {
243
$url = '"' . str_replace('#', '', $options['update']) . '", ' . $url;
245
unset($options['update'], $options['type']);
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']);
253
$options = $this->_parseOptions($options, $safe);
254
if (!empty($options)) {
255
$options = ', {' . $options . '}';
257
return "var jsRequest = new Ajax$type($url$options);";
261
* Create a sortable element.
263
* #### Note: Requires scriptaculous to be loaded.
265
* @param array $options Array of options for the sortable.
266
* @return string Completed sortable script.
268
* @see JsBaseEngineHelper::sortable() for options list.
270
function sortable($options = array()) {
271
$options = $this->_processOptions('sortable', $options);
272
if (!empty($options)) {
273
$options = ', {' . $options . '}';
275
return 'var jsSortable = Sortable.create(' . $this->selection . $options . ');';
279
* Create a Draggable element.
281
* #### Note: Requires scriptaculous to be loaded.
283
* @param array $options Array of options for the draggable.
284
* @return string Completed draggable script.
286
* @see JsBaseEngineHelper::draggable() for options list.
288
function drag($options = array()) {
289
$options = $this->_processOptions('drag', $options);
290
if (!empty($options)) {
291
$options = ', {' . $options . '}';
293
if ($this->_multiple) {
294
return $this->each('new Draggable(item' . $options . ');');
296
return 'var jsDrag = new Draggable(' . $this->selection . $options . ');';
300
* Create a Droppable element.
302
* #### Note: Requires scriptaculous to be loaded.
304
* @param array $options Array of options for the droppable.
305
* @return string Completed droppable script.
307
* @see JsBaseEngineHelper::droppable() for options list.
309
function drop($options = array()) {
310
$options = $this->_processOptions('drop', $options);
311
if (!empty($options)) {
312
$options = ', {' . $options . '}';
314
return 'Droppables.add(' . $this->selection . $options . ');';
318
* Creates a slider control widget.
320
* ### Note: Requires scriptaculous to be loaded.
322
* @param array $options Array of options for the slider.
323
* @return string Completed slider script.
325
* @see JsBaseEngineHelper::slider() for options list.
327
function slider($options = array()) {
328
$slider = $this->selection;
329
$this->get($options['handle']);
330
unset($options['handle']);
332
if (isset($options['min']) && isset($options['max'])) {
333
$options['range'] = array($options['min'], $options['max']);
334
unset($options['min'], $options['max']);
336
$optionString = $this->_processOptions('slider', $options);
337
if (!empty($optionString)) {
338
$optionString = ', {' . $optionString . '}';
340
$out = 'var jsSlider = new Control.Slider(' . $this->selection . ', ' . $slider . $optionString . ');';
341
$this->selection = $slider;
346
* Serialize the form attached to $selector.
348
* @param array $options Array of options.
349
* @return string Completed serializeForm() snippet
351
* @see JsBaseEngineHelper::serializeForm()
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)';
359
$method = '.serialize()';
360
if (!$options['inline']) {
363
return $selection . $method;