PageRenderTime 35ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/View/Helper/JqueryEngineHelper.php

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