1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15:
16: App::uses('JsBaseEngineHelper', 'View/Helper');
17:
18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30:
31: class MootoolsEngineHelper extends JsBaseEngineHelper {
32:
33: 34: 35: 36: 37:
38: protected $_optionMap = array(
39: 'request' => array(
40: 'complete' => 'onComplete',
41: 'success' => 'onSuccess',
42: 'before' => 'onRequest',
43: 'error' => 'onFailure'
44: ),
45: 'sortable' => array(
46: 'distance' => 'snap',
47: 'containment' => 'constrain',
48: 'sort' => 'onSort',
49: 'complete' => 'onComplete',
50: 'start' => 'onStart',
51: ),
52: 'drag' => array(
53: 'snapGrid' => 'snap',
54: 'start' => 'onStart',
55: 'drag' => 'onDrag',
56: 'stop' => 'onComplete',
57: ),
58: 'drop' => array(
59: 'drop' => 'onDrop',
60: 'hover' => 'onEnter',
61: 'leave' => 'onLeave',
62: ),
63: 'slider' => array(
64: 'complete' => 'onComplete',
65: 'change' => 'onChange',
66: 'direction' => 'mode',
67: 'step' => 'steps'
68: )
69: );
70:
71: 72: 73: 74: 75:
76: protected $_callbackArguments = array(
77: 'slider' => array(
78: 'onTick' => 'position',
79: 'onChange' => 'step',
80: 'onComplete' => 'event'
81: ),
82: 'request' => array(
83: 'onRequest' => '',
84: 'onComplete' => '',
85: 'onCancel' => '',
86: 'onSuccess' => 'responseText, responseXML',
87: 'onFailure' => 'xhr',
88: 'onException' => 'headerName, value',
89: ),
90: 'drag' => array(
91: 'onBeforeStart' => 'element',
92: 'onStart' => 'element',
93: 'onSnap' => 'element',
94: 'onDrag' => 'element, event',
95: 'onComplete' => 'element, event',
96: 'onCancel' => 'element',
97: ),
98: 'drop' => array(
99: 'onBeforeStart' => 'element',
100: 'onStart' => 'element',
101: 'onSnap' => 'element',
102: 'onDrag' => 'element, event',
103: 'onComplete' => 'element, event',
104: 'onCancel' => 'element',
105: 'onDrop' => 'element, droppable, event',
106: 'onLeave' => 'element, droppable',
107: 'onEnter' => 'element, droppable',
108: ),
109: 'sortable' => array(
110: 'onStart' => 'element, clone',
111: 'onSort' => 'element, clone',
112: 'onComplete' => 'element',
113: )
114: );
115:
116: 117: 118: 119: 120: 121:
122: public function get($selector) {
123: $this->_multipleSelection = false;
124: if ($selector == 'window' || $selector == 'document') {
125: $this->selection = "$(" . $selector . ")";
126: return $this;
127: }
128: if (preg_match('/^#[^\s.]+$/', $selector)) {
129: $this->selection = '$("' . substr($selector, 1) . '")';
130: return $this;
131: }
132: $this->_multipleSelection = true;
133: $this->selection = '$$("' . $selector . '")';
134: return $this;
135: }
136:
137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149:
150: public function event($type, $callback, $options = array()) {
151: $defaults = array('wrap' => true, 'stop' => true);
152: $options = array_merge($defaults, $options);
153:
154: $function = 'function (event) {%s}';
155: if ($options['wrap'] && $options['stop']) {
156: $callback = "event.stop();\n" . $callback;
157: }
158: if ($options['wrap']) {
159: $callback = sprintf($function, $callback);
160: }
161: $out = $this->selection . ".addEvent(\"{$type}\", $callback);";
162: return $out;
163: }
164:
165: 166: 167: 168: 169: 170:
171: public function domReady($functionBody) {
172: $this->selection = 'window';
173: return $this->event('domready', $functionBody, array('stop' => false));
174: }
175:
176: 177: 178: 179: 180: 181:
182: public function each($callback) {
183: return $this->selection . '.each(function (item, index) {' . $callback . '});';
184: }
185:
186: 187: 188: 189: 190: 191: 192: 193:
194: public function effect($name, $options = array()) {
195: $speed = null;
196: if (isset($options['speed']) && in_array($options['speed'], array('fast', 'slow'))) {
197: if ($options['speed'] == 'fast') {
198: $speed = '"short"';
199: } elseif ($options['speed'] == 'slow') {
200: $speed = '"long"';
201: }
202: }
203: $effect = '';
204: switch ($name) {
205: case 'hide':
206: $effect = 'setStyle("display", "none")';
207: break;
208: case 'show':
209: $effect = 'setStyle("display", "")';
210: break;
211: case 'fadeIn':
212: case 'fadeOut':
213: case 'slideIn':
214: case 'slideOut':
215: list($effectName, $direction) = preg_split('/([A-Z][a-z]+)/', $name, -1, PREG_SPLIT_DELIM_CAPTURE);
216: $direction = strtolower($direction);
217: if ($speed) {
218: $effect .= "set(\"$effectName\", {duration:$speed}).";
219: }
220: $effect .= "$effectName(\"$direction\")";
221: break;
222: }
223: return $this->selection . '.' . $effect . ';';
224: }
225:
226: 227: 228: 229: 230: 231: 232: 233: 234: 235:
236: public function request($url, $options = array()) {
237: $url = $this->url($url);
238: $options = $this->_mapOptions('request', $options);
239: $type = $data = null;
240: if (isset($options['type']) || isset($options['update'])) {
241: if (isset($options['type']) && strtolower($options['type']) == 'json') {
242: $type = '.JSON';
243: }
244: if (isset($options['update'])) {
245: $options['update'] = str_replace('#', '', $options['update']);
246: $type = '.HTML';
247: }
248: unset($options['type']);
249: }
250: if (!empty($options['data'])) {
251: $data = $options['data'];
252: unset($options['data']);
253: }
254: $options['url'] = $url;
255: $options = $this->_prepareCallbacks('request', $options);
256: if (!empty($options['dataExpression'])) {
257: $callbacks[] = 'data';
258: unset($options['dataExpression']);
259: } elseif (!empty($data)) {
260: $data = $this->object($data);
261: }
262: $options = $this->_parseOptions($options, array_keys($this->_callbackArguments['request']));
263: return "var jsRequest = new Request$type({{$options}}).send($data);";
264: }
265:
266: 267: 268: 269: 270: 271: 272: 273: 274:
275: public function sortable($options = array()) {
276: $options = $this->_processOptions('sortable', $options);
277: return 'var jsSortable = new Sortables(' . $this->selection . ', {' . $options . '});';
278: }
279:
280: 281: 282: 283: 284: 285: 286: 287: 288:
289: public function drag($options = array()) {
290: $options = $this->_processOptions('drag', $options);
291: return $this->selection . '.makeDraggable({' . $options . '});';
292: }
293:
294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307:
308: public function drop($options = array()) {
309: if (empty($options['drag'])) {
310: trigger_error(
311: __d('cake_dev', 'MootoolsEngine::drop() requires a "drag" option to properly function'), E_USER_WARNING
312: );
313: return false;
314: }
315: $options['droppables'] = $this->selection;
316:
317: $this->get($options['drag']);
318: unset($options['drag']);
319:
320: $options = $this->_mapOptions('drag', $this->_mapOptions('drop', $options));
321: $options = $this->_prepareCallbacks('drop', $options);
322: $safe = array_merge(array_keys($this->_callbackArguments['drop']), array('droppables'));
323: $optionString = $this->_parseOptions($options, $safe);
324: $out = $this->selection . '.makeDraggable({' . $optionString . '});';
325: $this->selection = $options['droppables'];
326: return $out;
327: }
328:
329: 330: 331: 332: 333: 334: 335: 336: 337:
338: public function slider($options = array()) {
339: $slider = $this->selection;
340: $this->get($options['handle']);
341: unset($options['handle']);
342:
343: if (isset($options['min']) && isset($options['max'])) {
344: $options['range'] = array($options['min'], $options['max']);
345: unset($options['min'], $options['max']);
346: }
347: $optionString = $this->_processOptions('slider', $options);
348: if (!empty($optionString)) {
349: $optionString = ', {' . $optionString . '}';
350: }
351: $out = 'var jsSlider = new Slider(' . $slider . ', ' . $this->selection . $optionString . ');';
352: $this->selection = $slider;
353: return $out;
354: }
355:
356: 357: 358: 359: 360: 361: 362:
363: public function serializeForm($options = array()) {
364: $options = array_merge(array('isForm' => false, 'inline' => false), $options);
365: $selection = $this->selection;
366: if (!$options['isForm']) {
367: $selection = '$(' . $this->selection . '.form)';
368: }
369: $method = '.toQueryString()';
370: if (!$options['inline']) {
371: $method .= ';';
372: }
373: return $selection . $method;
374: }
375:
376: }
377: