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 = html_entity_decode($this->url($url), ENT_COMPAT, Configure::read('App.encoding'));
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: unset($options['dataExpression']);
258: } elseif (!empty($data)) {
259: $data = $this->object($data);
260: }
261: $options = $this->_parseOptions($options, array_keys($this->_callbackArguments['request']));
262: return "var jsRequest = new Request$type({{$options}}).send($data);";
263: }
264:
265: 266: 267: 268: 269: 270: 271: 272: 273:
274: public function sortable($options = array()) {
275: $options = $this->_processOptions('sortable', $options);
276: return 'var jsSortable = new Sortables(' . $this->selection . ', {' . $options . '});';
277: }
278:
279: 280: 281: 282: 283: 284: 285: 286: 287:
288: public function drag($options = array()) {
289: $options = $this->_processOptions('drag', $options);
290: return $this->selection . '.makeDraggable({' . $options . '});';
291: }
292:
293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306:
307: public function drop($options = array()) {
308: if (empty($options['drag'])) {
309: trigger_error(
310: __d('cake_dev', 'MootoolsEngine::drop() requires a "drag" option to properly function'), E_USER_WARNING
311: );
312: return false;
313: }
314: $options['droppables'] = $this->selection;
315:
316: $this->get($options['drag']);
317: unset($options['drag']);
318:
319: $options = $this->_mapOptions('drag', $this->_mapOptions('drop', $options));
320: $options = $this->_prepareCallbacks('drop', $options);
321: $safe = array_merge(array_keys($this->_callbackArguments['drop']), array('droppables'));
322: $optionString = $this->_parseOptions($options, $safe);
323: $out = $this->selection . '.makeDraggable({' . $optionString . '});';
324: $this->selection = $options['droppables'];
325: return $out;
326: }
327:
328: 329: 330: 331: 332: 333: 334: 335: 336:
337: public function slider($options = array()) {
338: $slider = $this->selection;
339: $this->get($options['handle']);
340: unset($options['handle']);
341:
342: if (isset($options['min']) && isset($options['max'])) {
343: $options['range'] = array($options['min'], $options['max']);
344: unset($options['min'], $options['max']);
345: }
346: $optionString = $this->_processOptions('slider', $options);
347: if (!empty($optionString)) {
348: $optionString = ', {' . $optionString . '}';
349: }
350: $out = 'var jsSlider = new Slider(' . $slider . ', ' . $this->selection . $optionString . ');';
351: $this->selection = $slider;
352: return $out;
353: }
354:
355: 356: 357: 358: 359: 360: 361:
362: public function serializeForm($options = array()) {
363: $options = array_merge(array('isForm' => false, 'inline' => false), $options);
364: $selection = $this->selection;
365: if (!$options['isForm']) {
366: $selection = '$(' . $this->selection . '.form)';
367: }
368: $method = '.toQueryString()';
369: if (!$options['inline']) {
370: $method .= ';';
371: }
372: return $selection . $method;
373: }
374:
375: }
376: