JqueryEngineHelper.php 10 KB

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