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: App::import('Helper', 'Js');
26:
27: 28: 29: 30: 31: 32:
33: class JqueryEngineHelper extends JsBaseEngineHelper {
34: 35: 36: 37: 38: 39:
40: var $_optionMap = array(
41: 'request' => array(
42: 'type' => 'dataType',
43: 'before' => 'beforeSend',
44: 'method' => 'type',
45: ),
46: 'sortable' => array(
47: 'complete' => 'stop',
48: ),
49: 'drag' => array(
50: 'snapGrid' => 'grid',
51: 'container' => 'containment',
52: ),
53: 'drop' => array(
54: 'leave' => 'out',
55: 'hover' => 'over'
56: ),
57: 'slider' => array(
58: 'complete' => 'stop',
59: 'direction' => 'orientation'
60: )
61: );
62:
63: 64: 65: 66: 67: 68:
69: var $_callbackArguments = array(
70: 'slider' => array(
71: 'start' => 'event, ui',
72: 'slide' => 'event, ui',
73: 'change' => 'event, ui',
74: 'stop' => 'event, ui'
75: ),
76: 'sortable' => array(
77: 'start' => 'event, ui',
78: 'sort' => 'event, ui',
79: 'change' => 'event, ui',
80: 'beforeStop' => 'event, ui',
81: 'stop' => 'event, ui',
82: 'update' => 'event, ui',
83: 'receive' => 'event, ui',
84: 'remove' => 'event, ui',
85: 'over' => 'event, ui',
86: 'out' => 'event, ui',
87: 'activate' => 'event, ui',
88: 'deactivate' => 'event, ui'
89: ),
90: 'drag' => array(
91: 'start' => 'event, ui',
92: 'drag' => 'event, ui',
93: 'stop' => 'event, ui',
94: ),
95: 'drop' => array(
96: 'activate' => 'event, ui',
97: 'deactivate' => 'event, ui',
98: 'over' => 'event, ui',
99: 'out' => 'event, ui',
100: 'drop' => 'event, ui'
101: ),
102: 'request' => array(
103: 'beforeSend' => 'XMLHttpRequest',
104: 'error' => 'XMLHttpRequest, textStatus, errorThrown',
105: 'success' => 'data, textStatus',
106: 'complete' => 'XMLHttpRequest, textStatus',
107: 'xhr' => ''
108: )
109: );
110:
111: 112: 113: 114: 115: 116: 117:
118: var $jQueryObject = '$';
119:
120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130:
131: function _methodTemplate($method, $template, $options, $extraSafeKeys = array()) {
132: $options = $this->_mapOptions($method, $options);
133: $options = $this->_prepareCallbacks($method, $options);
134: $callbacks = array_keys($this->_callbackArguments[$method]);
135: if (!empty($extraSafeKeys)) {
136: $callbacks = array_merge($callbacks, $extraSafeKeys);
137: }
138: $options = $this->_parseOptions($options, $callbacks);
139: return sprintf($template, $this->selection, $options);
140: }
141:
142: 143: 144: 145: 146: 147: 148:
149: function get($selector) {
150: if ($selector == 'window' || $selector == 'document') {
151: $this->selection = $this->jQueryObject . '(' . $selector .')';
152: } else {
153: $this->selection = $this->jQueryObject . '("' . $selector . '")';
154: }
155: return $this;
156: }
157:
158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171:
172: function event($type, $callback, $options = array()) {
173: $defaults = array('wrap' => true, 'stop' => true);
174: $options = array_merge($defaults, $options);
175:
176: $function = 'function (event) {%s}';
177: if ($options['wrap'] && $options['stop']) {
178: $callback .= "\nreturn false;";
179: }
180: if ($options['wrap']) {
181: $callback = sprintf($function, $callback);
182: }
183: return sprintf('%s.bind("%s", %s);', $this->selection, $type, $callback);
184: }
185:
186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197:
198: function domReady($functionBody) {
199: return $this->jQueryObject . '(document).ready(function () {' . $functionBody . '});';
200: }
201:
202: 203: 204: 205: 206: 207: 208: 209:
210: function each($callback) {
211: return $this->selection . '.each(function () {' . $callback . '});';
212: }
213:
214: 215: 216: 217: 218: 219: 220: 221: 222:
223: function effect($name, $options = array()) {
224: $speed = null;
225: if (isset($options['speed']) && in_array($options['speed'], array('fast', 'slow'))) {
226: $speed = $this->value($options['speed']);
227: }
228: $effect = '';
229: switch ($name) {
230: case 'slideIn':
231: case 'slideOut':
232: $name = ($name == 'slideIn') ? 'slideDown' : 'slideUp';
233: case 'hide':
234: case 'show':
235: case 'fadeIn':
236: case 'fadeOut':
237: case 'slideDown':
238: case 'slideUp':
239: $effect = ".$name($speed);";
240: break;
241: }
242: return $this->selection . $effect;
243: }
244:
245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255:
256: function request($url, $options = array()) {
257: $url = $this->url($url);
258: $options = $this->_mapOptions('request', $options);
259: if (isset($options['data']) && is_array($options['data'])) {
260: $options['data'] = $this->_toQuerystring($options['data']);
261: }
262: $options['url'] = $url;
263: if (isset($options['update'])) {
264: $wrapCallbacks = isset($options['wrapCallbacks']) ? $options['wrapCallbacks'] : true;
265: $success = '';
266: if(isset($options['success']) AND !empty($options['success'])) {
267: $success .= $options['success'];
268: }
269: $success .= $this->jQueryObject . '("' . $options['update'] . '").html(data);';
270: if (!$wrapCallbacks) {
271: $success = 'function (data, textStatus) {' . $success . '}';
272: }
273: $options['dataType'] = 'html';
274: $options['success'] = $success;
275: unset($options['update']);
276: }
277: $callbacks = array('success', 'error', 'beforeSend', 'complete');
278: if (!empty($options['dataExpression'])) {
279: $callbacks[] = 'data';
280: unset($options['dataExpression']);
281: }
282: $options = $this->_prepareCallbacks('request', $options);
283: $options = $this->_parseOptions($options, $callbacks);
284: return $this->jQueryObject . '.ajax({' . $options .'});';
285: }
286:
287: 288: 289: 290: 291: 292: 293: 294: 295: 296:
297: function sortable($options = array()) {
298: $template = '%s.sortable({%s});';
299: return $this->_methodTemplate('sortable', $template, $options);
300: }
301:
302: 303: 304: 305: 306: 307: 308: 309: 310: 311:
312: function drag($options = array()) {
313: $template = '%s.draggable({%s});';
314: return $this->_methodTemplate('drag', $template, $options);
315: }
316:
317: 318: 319: 320: 321: 322: 323: 324: 325: 326:
327: function drop($options = array()) {
328: $template = '%s.droppable({%s});';
329: return $this->_methodTemplate('drop', $template, $options);
330: }
331:
332: 333: 334: 335: 336: 337: 338: 339: 340: 341:
342: function slider($options = array()) {
343: $callbacks = array('start', 'change', 'slide', 'stop');
344: $template = '%s.slider({%s});';
345: return $this->_methodTemplate('slider', $template, $options, $callbacks);
346: }
347:
348: 349: 350: 351: 352: 353: 354: 355: 356:
357: function serializeForm($options = array()) {
358: $options = array_merge(array('isForm' => false, 'inline' => false), $options);
359: $selector = $this->selection;
360: if (!$options['isForm']) {
361: $selector = $this->selection . '.closest("form")';
362: }
363: $method = '.serialize()';
364: if (!$options['inline']) {
365: $method .= ';';
366: }
367: return $selector . $method;
368: }
369: }
370: