JqueryEngineHelper.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. <?php
  2. /**
  3. * jQuery Engine Helper for JsHelper
  4. *
  5. * Provides jQuery specific Javascript for JsHelper.
  6. *
  7. * Implements the JsHelper interface for jQuery. All $options arrays
  8. * support all options found in the JsHelper, as well as those in the jQuery
  9. * documentation.
  10. *
  11. * PHP 5
  12. *
  13. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  14. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. *
  16. * Licensed under The MIT License
  17. * Redistributions of files must retain the above copyright notice.
  18. *
  19. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  20. * @link http://cakephp.org CakePHP(tm) Project
  21. * @package Cake.View.Helper
  22. * @since CakePHP(tm) v 1.3
  23. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  24. */
  25. App::uses('AppHelper', 'View/Helper');
  26. App::uses('JsBaseEngineHelper', 'View/Helper');
  27. class JqueryEngineHelper extends JsBaseEngineHelper {
  28. /**
  29. * Option mappings for jQuery
  30. *
  31. * @var array
  32. */
  33. protected $_optionMap = array(
  34. 'request' => array(
  35. 'type' => 'dataType',
  36. 'before' => 'beforeSend',
  37. 'method' => 'type',
  38. ),
  39. 'sortable' => array(
  40. 'complete' => 'stop',
  41. ),
  42. 'drag' => array(
  43. 'snapGrid' => 'grid',
  44. 'container' => 'containment',
  45. ),
  46. 'drop' => array(
  47. 'leave' => 'out',
  48. 'hover' => 'over'
  49. ),
  50. 'slider' => array(
  51. 'complete' => 'stop',
  52. 'direction' => 'orientation'
  53. )
  54. );
  55. /**
  56. * Callback arguments lists
  57. *
  58. * @var string
  59. */
  60. protected $_callbackArguments = array(
  61. 'slider' => array(
  62. 'start' => 'event, ui',
  63. 'slide' => 'event, ui',
  64. 'change' => 'event, ui',
  65. 'stop' => 'event, ui'
  66. ),
  67. 'sortable' => array(
  68. 'start' => 'event, ui',
  69. 'sort' => 'event, ui',
  70. 'change' => 'event, ui',
  71. 'beforeStop' => 'event, ui',
  72. 'stop' => 'event, ui',
  73. 'update' => 'event, ui',
  74. 'receive' => 'event, ui',
  75. 'remove' => 'event, ui',
  76. 'over' => 'event, ui',
  77. 'out' => 'event, ui',
  78. 'activate' => 'event, ui',
  79. 'deactivate' => 'event, ui'
  80. ),
  81. 'drag' => array(
  82. 'start' => 'event, ui',
  83. 'drag' => 'event, ui',
  84. 'stop' => 'event, ui',
  85. ),
  86. 'drop' => array(
  87. 'activate' => 'event, ui',
  88. 'deactivate' => 'event, ui',
  89. 'over' => 'event, ui',
  90. 'out' => 'event, ui',
  91. 'drop' => 'event, ui'
  92. ),
  93. 'request' => array(
  94. 'beforeSend' => 'XMLHttpRequest',
  95. 'error' => 'XMLHttpRequest, textStatus, errorThrown',
  96. 'success' => 'data, textStatus',
  97. 'complete' => 'XMLHttpRequest, textStatus',
  98. 'xhr' => ''
  99. )
  100. );
  101. /**
  102. * The variable name of the jQuery Object, useful
  103. * when jQuery is put into noConflict() mode.
  104. *
  105. * @var string
  106. */
  107. public $jQueryObject = '$';
  108. /**
  109. * Helper function to wrap repetitive simple method templating.
  110. *
  111. * @param string $method The method name being generated.
  112. * @param string $template The method template
  113. * @param array $options Array of options for method
  114. * @param array $extraSafeKeys Extra safe keys
  115. * @return string Composed method string
  116. */
  117. protected function _methodTemplate($method, $template, $options, $extraSafeKeys = array()) {
  118. $options = $this->_mapOptions($method, $options);
  119. $options = $this->_prepareCallbacks($method, $options);
  120. $callbacks = array_keys($this->_callbackArguments[$method]);
  121. if (!empty($extraSafeKeys)) {
  122. $callbacks = array_merge($callbacks, $extraSafeKeys);
  123. }
  124. $options = $this->_parseOptions($options, $callbacks);
  125. return sprintf($template, $this->selection, $options);
  126. }
  127. /**
  128. * Create javascript selector for a CSS rule
  129. *
  130. * @param string $selector The selector that is targeted
  131. * @return JqueryEngineHelper instance of $this. Allows chained methods.
  132. */
  133. public function get($selector) {
  134. if ($selector == 'window' || $selector == 'document') {
  135. $this->selection = $this->jQueryObject . '(' . $selector .')';
  136. } else {
  137. $this->selection = $this->jQueryObject . '("' . $selector . '")';
  138. }
  139. return $this;
  140. }
  141. /**
  142. * Add an event to the script cache. Operates on the currently selected elements.
  143. *
  144. * ### Options
  145. *
  146. * - 'wrap' - Whether you want the callback wrapped in an anonymous function. (defaults true)
  147. * - 'stop' - Whether you want the event to stopped. (defaults true)
  148. *
  149. * @param string $type Type of event to bind to the current dom id
  150. * @param string $callback The Javascript function you wish to trigger or the function literal
  151. * @param array $options Options for the event.
  152. * @return string completed event handler
  153. */
  154. public function event($type, $callback, $options = array()) {
  155. $defaults = array('wrap' => true, 'stop' => true);
  156. $options = array_merge($defaults, $options);
  157. $function = 'function (event) {%s}';
  158. if ($options['wrap'] && $options['stop']) {
  159. $callback .= "\nreturn false;";
  160. }
  161. if ($options['wrap']) {
  162. $callback = sprintf($function, $callback);
  163. }
  164. return sprintf('%s.bind("%s", %s);', $this->selection, $type, $callback);
  165. }
  166. /**
  167. * Create a domReady event. For jQuery. This method does not
  168. * bind a 'traditional event' as `$(document).bind('ready', fn)`
  169. * Works in an entirely different fashion than `$(document).ready()`
  170. * The first will not run the function when eval()'d as part of a response
  171. * The second will. Because of the way that ajax pagination is done
  172. * `$().ready()` is used.
  173. *
  174. * @param string $functionBody The code to run on domReady
  175. * @return string completed domReady method
  176. */
  177. public function domReady($functionBody) {
  178. return $this->jQueryObject . '(document).ready(function () {' . $functionBody . '});';
  179. }
  180. /**
  181. * Create an iteration over the current selection result.
  182. *
  183. * @param string $callback The function body you wish to apply during the iteration.
  184. * @return string completed iteration
  185. */
  186. public function each($callback) {
  187. return $this->selection . '.each(function () {' . $callback . '});';
  188. }
  189. /**
  190. * Trigger an Effect.
  191. *
  192. * @param string $name The name of the effect to trigger.
  193. * @param array $options Array of options for the effect.
  194. * @return string completed string with effect.
  195. * @see JsBaseEngineHelper::effect()
  196. */
  197. public function effect($name, $options = array()) {
  198. $speed = null;
  199. if (isset($options['speed']) && in_array($options['speed'], array('fast', 'slow'))) {
  200. $speed = $this->value($options['speed']);
  201. }
  202. $effect = '';
  203. switch ($name) {
  204. case 'slideIn':
  205. case 'slideOut':
  206. $name = ($name == 'slideIn') ? 'slideDown' : 'slideUp';
  207. case 'hide':
  208. case 'show':
  209. case 'fadeIn':
  210. case 'fadeOut':
  211. case 'slideDown':
  212. case 'slideUp':
  213. $effect = ".$name($speed);";
  214. break;
  215. }
  216. return $this->selection . $effect;
  217. }
  218. /**
  219. * Create an $.ajax() call.
  220. *
  221. * If the 'update' key is set, success callback will be overridden.
  222. *
  223. * @param mixed $url
  224. * @param array $options See JsHelper::request() for options.
  225. * @return string The completed ajax call.
  226. * @see JsBaseEngineHelper::request() for options list.
  227. */
  228. public function request($url, $options = array()) {
  229. $url = $this->url($url);
  230. $options = $this->_mapOptions('request', $options);
  231. if (isset($options['data']) && is_array($options['data'])) {
  232. $options['data'] = $this->_toQuerystring($options['data']);
  233. }
  234. $options['url'] = $url;
  235. if (isset($options['update'])) {
  236. $wrapCallbacks = isset($options['wrapCallbacks']) ? $options['wrapCallbacks'] : true;
  237. $success = '';
  238. if (isset($options['success']) AND !empty($options['success'])) {
  239. $success .= $options['success'];
  240. }
  241. $success .= $this->jQueryObject . '("' . $options['update'] . '").html(data);';
  242. if (!$wrapCallbacks) {
  243. $success = 'function (data, textStatus) {' . $success . '}';
  244. }
  245. $options['dataType'] = 'html';
  246. $options['success'] = $success;
  247. unset($options['update']);
  248. }
  249. $callbacks = array('success', 'error', 'beforeSend', 'complete');
  250. if (!empty($options['dataExpression'])) {
  251. $callbacks[] = 'data';
  252. unset($options['dataExpression']);
  253. }
  254. $options = $this->_prepareCallbacks('request', $options);
  255. $options = $this->_parseOptions($options, $callbacks);
  256. return $this->jQueryObject . '.ajax({' . $options .'});';
  257. }
  258. /**
  259. * Create a sortable element.
  260. *
  261. * Requires both Ui.Core and Ui.Sortables to be loaded.
  262. *
  263. * @param array $options Array of options for the sortable.
  264. * @return string Completed sortable script.
  265. * @see JsBaseEngineHelper::sortable() for options list.
  266. */
  267. public function sortable($options = array()) {
  268. $template = '%s.sortable({%s});';
  269. return $this->_methodTemplate('sortable', $template, $options);
  270. }
  271. /**
  272. * Create a Draggable element
  273. *
  274. * Requires both Ui.Core and Ui.Draggable to be loaded.
  275. *
  276. * @param array $options Array of options for the draggable element.
  277. * @return string Completed Draggable script.
  278. * @see JsBaseEngineHelper::drag() for options list.
  279. */
  280. public function drag($options = array()) {
  281. $template = '%s.draggable({%s});';
  282. return $this->_methodTemplate('drag', $template, $options);
  283. }
  284. /**
  285. * Create a Droppable element
  286. *
  287. * Requires both Ui.Core and Ui.Droppable to be loaded.
  288. *
  289. * @param array $options Array of options for the droppable element.
  290. * @return string Completed Droppable script.
  291. * @see JsBaseEngineHelper::drop() for options list.
  292. */
  293. public function drop($options = array()) {
  294. $template = '%s.droppable({%s});';
  295. return $this->_methodTemplate('drop', $template, $options);
  296. }
  297. /**
  298. * Create a Slider element
  299. *
  300. * Requires both Ui.Core and Ui.Slider to be loaded.
  301. *
  302. * @param array $options Array of options for the droppable element.
  303. * @return string Completed Slider script.
  304. * @see JsBaseEngineHelper::slider() for options list.
  305. */
  306. public function slider($options = array()) {
  307. $callbacks = array('start', 'change', 'slide', 'stop');
  308. $template = '%s.slider({%s});';
  309. return $this->_methodTemplate('slider', $template, $options, $callbacks);
  310. }
  311. /**
  312. * Serialize a form attached to $selector. If the current selection is not an input or
  313. * form, errors will be created in the Javascript.
  314. *
  315. * @param array $options Options for the serialization
  316. * @return string completed form serialization script.
  317. * @see JsBaseEngineHelper::serializeForm() for option list.
  318. */
  319. public function serializeForm($options = array()) {
  320. $options = array_merge(array('isForm' => false, 'inline' => false), $options);
  321. $selector = $this->selection;
  322. if (!$options['isForm']) {
  323. $selector = $this->selection . '.closest("form")';
  324. }
  325. $method = '.serialize()';
  326. if (!$options['inline']) {
  327. $method .= ';';
  328. }
  329. return $selector . $method;
  330. }
  331. }