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