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