JqueryEngineHelper.php 10 KB

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