PageRenderTime 25ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/public_html/cake/libs/view/helpers/jquery_engine.php

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