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