PageRenderTime 51ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/Cake/View/Helper/MootoolsEngineHelper.php

https://bitbucket.org/udeshika/fake_twitter
PHP | 386 lines | 223 code | 18 blank | 145 comment | 29 complexity | 05d09ba4466063d4ba13f8a81894f9e9 MD5 | raw file
  1. <?php
  2. /**
  3. * MooTools Engine Helper for JsHelper
  4. *
  5. * Provides MooTools specific Javascript for JsHelper.
  6. * Assumes that you have the following MooTools packages
  7. *
  8. * - Remote, Remote.HTML, Remote.JSON
  9. * - Fx, Fx.Tween, Fx.Morph
  10. * - Selectors, DomReady,
  11. * - Drag, Drag.Move
  12. *
  13. * PHP 5
  14. *
  15. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  16. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  17. *
  18. * Licensed under The MIT License
  19. * Redistributions of files must retain the above copyright notice.
  20. *
  21. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  22. * @link http://cakephp.org CakePHP(tm) Project
  23. * @package Cake.View.Helper
  24. * @since CakePHP(tm) v 1.3
  25. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  26. */
  27. App::uses('JsBaseEngineHelper', 'View/Helper');
  28. /**
  29. * MooTools Engine Helper for JsHelper
  30. *
  31. * Provides MooTools specific Javascript for JsHelper.
  32. * Assumes that you have the following MooTools packages
  33. *
  34. * - Remote, Remote.HTML, Remote.JSON
  35. * - Fx, Fx.Tween, Fx.Morph
  36. * - Selectors, DomReady,
  37. * - Drag, Drag.Move
  38. *
  39. * @package Cake.View.Helper
  40. */
  41. class MootoolsEngineHelper extends JsBaseEngineHelper {
  42. /**
  43. * Option mappings for MooTools
  44. *
  45. * @var array
  46. */
  47. protected $_optionMap = array(
  48. 'request' => array(
  49. 'complete' => 'onComplete',
  50. 'success' => 'onSuccess',
  51. 'before' => 'onRequest',
  52. 'error' => 'onFailure'
  53. ),
  54. 'sortable' => array(
  55. 'distance' => 'snap',
  56. 'containment' => 'constrain',
  57. 'sort' => 'onSort',
  58. 'complete' => 'onComplete',
  59. 'start' => 'onStart',
  60. ),
  61. 'drag' => array(
  62. 'snapGrid' => 'snap',
  63. 'start' => 'onStart',
  64. 'drag' => 'onDrag',
  65. 'stop' => 'onComplete',
  66. ),
  67. 'drop' => array(
  68. 'drop' => 'onDrop',
  69. 'hover' => 'onEnter',
  70. 'leave' => 'onLeave',
  71. ),
  72. 'slider' => array(
  73. 'complete' => 'onComplete',
  74. 'change' => 'onChange',
  75. 'direction' => 'mode',
  76. 'step' => 'steps'
  77. )
  78. );
  79. /**
  80. * Contains a list of callback names -> default arguments.
  81. *
  82. * @var array
  83. */
  84. protected $_callbackArguments = array(
  85. 'slider' => array(
  86. 'onTick' => 'position',
  87. 'onChange' => 'step',
  88. 'onComplete' => 'event'
  89. ),
  90. 'request' => array(
  91. 'onRequest' => '',
  92. 'onComplete' => '',
  93. 'onCancel' => '',
  94. 'onSuccess' => 'responseText, responseXML',
  95. 'onFailure' => 'xhr',
  96. 'onException' => 'headerName, value',
  97. ),
  98. 'drag' => array(
  99. 'onBeforeStart' => 'element',
  100. 'onStart' => 'element',
  101. 'onSnap' => 'element',
  102. 'onDrag' => 'element, event',
  103. 'onComplete' => 'element, event',
  104. 'onCancel' => 'element',
  105. ),
  106. 'drop' => array(
  107. 'onBeforeStart' => 'element',
  108. 'onStart' => 'element',
  109. 'onSnap' => 'element',
  110. 'onDrag' => 'element, event',
  111. 'onComplete' => 'element, event',
  112. 'onCancel' => 'element',
  113. 'onDrop' => 'element, droppable, event',
  114. 'onLeave' => 'element, droppable',
  115. 'onEnter' => 'element, droppable',
  116. ),
  117. 'sortable' => array(
  118. 'onStart' => 'element, clone',
  119. 'onSort' => 'element, clone',
  120. 'onComplete' => 'element',
  121. )
  122. );
  123. /**
  124. * Create javascript selector for a CSS rule
  125. *
  126. * @param string $selector The selector that is targeted
  127. * @return MootoolsEngineHelper instance of $this. Allows chained methods.
  128. */
  129. public function get($selector) {
  130. $this->_multipleSelection = false;
  131. if ($selector == 'window' || $selector == 'document') {
  132. $this->selection = "$(" . $selector .")";
  133. return $this;
  134. }
  135. if (preg_match('/^#[^\s.]+$/', $selector)) {
  136. $this->selection = '$("' . substr($selector, 1) . '")';
  137. return $this;
  138. }
  139. $this->_multipleSelection = true;
  140. $this->selection = '$$("' . $selector . '")';
  141. return $this;
  142. }
  143. /**
  144. * Add an event to the script cache. Operates on the currently selected elements.
  145. *
  146. * ### Options
  147. *
  148. * - 'wrap' - Whether you want the callback wrapped in an anonymous function. (defaults true)
  149. * - 'stop' - Whether you want the event to stopped. (defaults true)
  150. *
  151. * @param string $type Type of event to bind to the current dom id
  152. * @param string $callback The Javascript function you wish to trigger or the function literal
  153. * @param array $options Options for the event.
  154. * @return string completed event handler
  155. */
  156. public function event($type, $callback, $options = array()) {
  157. $defaults = array('wrap' => true, 'stop' => true);
  158. $options = array_merge($defaults, $options);
  159. $function = 'function (event) {%s}';
  160. if ($options['wrap'] && $options['stop']) {
  161. $callback = "event.stop();\n" . $callback;
  162. }
  163. if ($options['wrap']) {
  164. $callback = sprintf($function, $callback);
  165. }
  166. $out = $this->selection . ".addEvent(\"{$type}\", $callback);";
  167. return $out;
  168. }
  169. /**
  170. * Create a domReady event. This is a special event in many libraries
  171. *
  172. * @param string $functionBody The code to run on domReady
  173. * @return string completed domReady method
  174. */
  175. public function domReady($functionBody) {
  176. $this->selection = 'window';
  177. return $this->event('domready', $functionBody, array('stop' => false));
  178. }
  179. /**
  180. * Create an iteration over the current selection result.
  181. *
  182. * @param string $callback The function body you wish to apply during the iteration.
  183. * @return string completed iteration
  184. */
  185. public function each($callback) {
  186. return $this->selection . '.each(function (item, index) {' . $callback . '});';
  187. }
  188. /**
  189. * Trigger an Effect.
  190. *
  191. * @param string $name The name of the effect to trigger.
  192. * @param array $options Array of options for the effect.
  193. * @return string completed string with effect.
  194. * @see JsBaseEngineHelper::effect()
  195. */
  196. public function effect($name, $options = array()) {
  197. $speed = null;
  198. if (isset($options['speed']) && in_array($options['speed'], array('fast', 'slow'))) {
  199. if ($options['speed'] == 'fast') {
  200. $speed = '"short"';
  201. } elseif ($options['speed'] == 'slow') {
  202. $speed = '"long"';
  203. }
  204. }
  205. $effect = '';
  206. switch ($name) {
  207. case 'hide':
  208. $effect = 'setStyle("display", "none")';
  209. break;
  210. case 'show':
  211. $effect = 'setStyle("display", "")';
  212. break;
  213. case 'fadeIn':
  214. case 'fadeOut':
  215. case 'slideIn':
  216. case 'slideOut':
  217. list($effectName, $direction) = preg_split('/([A-Z][a-z]+)/', $name, -1, PREG_SPLIT_DELIM_CAPTURE);
  218. $direction = strtolower($direction);
  219. if ($speed) {
  220. $effect .= "set(\"$effectName\", {duration:$speed}).";
  221. }
  222. $effect .= "$effectName(\"$direction\")";
  223. break;
  224. }
  225. return $this->selection . '.' . $effect . ';';
  226. }
  227. /**
  228. * Create an new Request.
  229. *
  230. * Requires `Request`. If you wish to use 'update' key you must have ```Request.HTML```
  231. * if you wish to do Json requests you will need ```JSON``` and ```Request.JSON```.
  232. *
  233. * @param mixed $url
  234. * @param array $options
  235. * @return string The completed ajax call.
  236. */
  237. public function request($url, $options = array()) {
  238. $url = $this->url($url);
  239. $options = $this->_mapOptions('request', $options);
  240. $type = $data = null;
  241. if (isset($options['type']) || isset($options['update'])) {
  242. if (isset($options['type']) && strtolower($options['type']) == 'json') {
  243. $type = '.JSON';
  244. }
  245. if (isset($options['update'])) {
  246. $options['update'] = str_replace('#', '', $options['update']);
  247. $type = '.HTML';
  248. }
  249. unset($options['type']);
  250. }
  251. if (!empty($options['data'])) {
  252. $data = $options['data'];
  253. unset($options['data']);
  254. }
  255. $options['url'] = $url;
  256. $options = $this->_prepareCallbacks('request', $options);
  257. if (!empty($options['dataExpression'])) {
  258. $callbacks[] = 'data';
  259. unset($options['dataExpression']);
  260. } elseif (!empty($data)) {
  261. $data = $this->object($data);
  262. }
  263. $options = $this->_parseOptions($options, array_keys($this->_callbackArguments['request']));
  264. return "var jsRequest = new Request$type({{$options}}).send($data);";
  265. }
  266. /**
  267. * Create a sortable element.
  268. *
  269. * Requires the `Sortables` plugin from MootoolsMore
  270. *
  271. * @param array $options Array of options for the sortable.
  272. * @return string Completed sortable script.
  273. * @see JsBaseEngineHelper::sortable() for options list.
  274. */
  275. public function sortable($options = array()) {
  276. $options = $this->_processOptions('sortable', $options);
  277. return 'var jsSortable = new Sortables(' . $this->selection . ', {' . $options . '});';
  278. }
  279. /**
  280. * Create a Draggable element.
  281. *
  282. * Requires the `Drag` plugin from MootoolsMore
  283. *
  284. * @param array $options Array of options for the draggable.
  285. * @return string Completed draggable script.
  286. * @see JsHelper::drag() for options list.
  287. */
  288. public function drag($options = array()) {
  289. $options = $this->_processOptions('drag', $options);
  290. return $this->selection . '.makeDraggable({' . $options . '});';
  291. }
  292. /**
  293. * Create a Droppable element.
  294. *
  295. * Requires the `Drag` and `Drag.Move` plugins from MootoolsMore
  296. *
  297. * Droppables in Mootools function differently from other libraries. Droppables
  298. * are implemented as an extension of Drag. So in addition to making a get() selection for
  299. * the droppable element. You must also provide a selector rule to the draggable element. Furthermore,
  300. * Mootools droppables inherit all options from Drag.
  301. *
  302. * @param array $options Array of options for the droppable.
  303. * @return string Completed droppable script.
  304. * @see JsBaseEngineHelper::drop() for options list.
  305. */
  306. public function drop($options = array()) {
  307. if (empty($options['drag'])) {
  308. trigger_error(
  309. __d('cake_dev', 'MootoolsEngine::drop() requires a "drag" option to properly function'), E_USER_WARNING
  310. );
  311. return false;
  312. }
  313. $options['droppables'] = $this->selection;
  314. $this->get($options['drag']);
  315. unset($options['drag']);
  316. $options = $this->_mapOptions('drag', $this->_mapOptions('drop', $options));
  317. $options = $this->_prepareCallbacks('drop', $options);
  318. $safe = array_merge(array_keys($this->_callbackArguments['drop']), array('droppables'));
  319. $optionString = $this->_parseOptions($options, $safe);
  320. $out = $this->selection . '.makeDraggable({' . $optionString . '});';
  321. $this->selection = $options['droppables'];
  322. return $out;
  323. }
  324. /**
  325. * Create a slider control
  326. *
  327. * Requires `Slider` from MootoolsMore
  328. *
  329. * @param array $options Array of options for the slider.
  330. * @return string Completed slider script.
  331. * @see JsBaseEngineHelper::slider() for options list.
  332. */
  333. public function slider($options = array()) {
  334. $slider = $this->selection;
  335. $this->get($options['handle']);
  336. unset($options['handle']);
  337. if (isset($options['min']) && isset($options['max'])) {
  338. $options['range'] = array($options['min'], $options['max']);
  339. unset($options['min'], $options['max']);
  340. }
  341. $optionString = $this->_processOptions('slider', $options);
  342. if (!empty($optionString)) {
  343. $optionString = ', {' . $optionString . '}';
  344. }
  345. $out = 'var jsSlider = new Slider(' . $slider . ', ' . $this->selection . $optionString . ');';
  346. $this->selection = $slider;
  347. return $out;
  348. }
  349. /**
  350. * Serialize the form attached to $selector.
  351. *
  352. * @param array $options Array of options.
  353. * @return string Completed serializeForm() snippet
  354. * @see JsBaseEngineHelper::serializeForm()
  355. */
  356. public function serializeForm($options = array()) {
  357. $options = array_merge(array('isForm' => false, 'inline' => false), $options);
  358. $selection = $this->selection;
  359. if (!$options['isForm']) {
  360. $selection = '$(' . $this->selection . '.form)';
  361. }
  362. $method = '.toQueryString()';
  363. if (!$options['inline']) {
  364. $method .= ';';
  365. }
  366. return $selection . $method;
  367. }
  368. }