JqueryEngineHelper.php 10 KB

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