PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Cake/View/Helper/PrototypeEngineHelper.php

https://github.com/gustavor/lore
PHP | 360 lines | 214 code | 17 blank | 129 comment | 32 complexity | f9f18e18f4374810a430ad4ca1474703 MD5 | raw file
  1. <?php
  2. /**
  3. * Prototype Engine Helper for JsHelper
  4. *
  5. * Provides Prototype specific Javascript for JsHelper. Requires at least
  6. * Prototype 1.6
  7. *
  8. * PHP 5
  9. *
  10. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  11. * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  12. *
  13. * Licensed under The MIT License
  14. * Redistributions of files must retain the above copyright notice.
  15. *
  16. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
  17. * @link http://cakephp.org CakePHP(tm) Project
  18. * @package Cake.View.Helper
  19. * @since CakePHP(tm) v 1.3
  20. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  21. */
  22. App::uses('JsBaseEngineHelper', 'View/Helper');
  23. class PrototypeEngineHelper extends JsBaseEngineHelper {
  24. /**
  25. * Is the current selection a multiple selection? or is it just a single element.
  26. *
  27. * @var boolean
  28. */
  29. protected $_multiple = false;
  30. /**
  31. * Option mappings for Prototype
  32. *
  33. * @var array
  34. */
  35. protected $_optionMap = array(
  36. 'request' => array(
  37. 'async' => 'asynchronous',
  38. 'data' => 'parameters',
  39. 'before' => 'onCreate',
  40. 'success' => 'onSuccess',
  41. 'complete' => 'onComplete',
  42. 'error' => 'onFailure'
  43. ),
  44. 'sortable' => array(
  45. 'sort' => 'onChange',
  46. 'complete' => 'onUpdate',
  47. ),
  48. 'drag' => array(
  49. 'snapGrid' => 'snap',
  50. 'container' => 'constraint',
  51. 'stop' => 'onEnd',
  52. 'start' => 'onStart',
  53. 'drag' => 'onDrag',
  54. ),
  55. 'drop' => array(
  56. 'hover' => 'onHover',
  57. 'drop' => 'onDrop',
  58. 'hoverClass' => 'hoverclass',
  59. ),
  60. 'slider' => array(
  61. 'direction' => 'axis',
  62. 'change' => 'onSlide',
  63. 'complete' => 'onChange',
  64. 'value' => 'sliderValue',
  65. )
  66. );
  67. /**
  68. * Contains a list of callback names -> default arguments.
  69. *
  70. * @var array
  71. */
  72. protected $_callbackArguments = array(
  73. 'slider' => array(
  74. 'onSlide' => 'value',
  75. 'onChange' => 'value',
  76. ),
  77. 'drag' => array(
  78. 'onStart' => 'event',
  79. 'onDrag' => 'event',
  80. 'change' => 'draggable',
  81. 'onEnd' => 'event',
  82. ),
  83. 'drop' => array(
  84. 'onHover' => 'draggable, droppable, event',
  85. 'onDrop' => 'draggable, droppable, event',
  86. ),
  87. 'request' => array(
  88. 'onCreate' => 'transport',
  89. 'onComplete' => 'transport',
  90. 'onFailure' => 'response, jsonHeader',
  91. 'onRequest' => 'transport',
  92. 'onSuccess' => 'response, jsonHeader'
  93. ),
  94. 'sortable' => array(
  95. 'onStart' => 'element',
  96. 'onChange' => 'element',
  97. 'onUpdate' => 'element',
  98. ),
  99. );
  100. /**
  101. * Create javascript selector for a CSS rule
  102. *
  103. * @param string $selector The selector that is targeted
  104. * @return PrototypeEngineHelper instance of $this. Allows chained methods.
  105. */
  106. public function get($selector) {
  107. $this->_multiple = false;
  108. if ($selector == 'window' || $selector == 'document') {
  109. $this->selection = "$(" . $selector .")";
  110. return $this;
  111. }
  112. if (preg_match('/^#[^\s.]+$/', $selector)) {
  113. $this->selection = '$("' . substr($selector, 1) . '")';
  114. return $this;
  115. }
  116. $this->_multiple = true;
  117. $this->selection = '$$("' . $selector . '")';
  118. return $this;
  119. }
  120. /**
  121. * Add an event to the script cache. Operates on the currently selected elements.
  122. *
  123. * ### Options
  124. *
  125. * - `wrap` - Whether you want the callback wrapped in an anonymous function. (defaults true)
  126. * - `stop` - Whether you want the event to stopped. (defaults true)
  127. *
  128. * @param string $type Type of event to bind to the current 946 id
  129. * @param string $callback The Javascript function you wish to trigger or the function literal
  130. * @param array $options Options for the event.
  131. * @return string completed event handler
  132. */
  133. public function event($type, $callback, $options = array()) {
  134. $defaults = array('wrap' => true, 'stop' => true);
  135. $options = array_merge($defaults, $options);
  136. $function = 'function (event) {%s}';
  137. if ($options['wrap'] && $options['stop']) {
  138. $callback = "event.stop();\n" . $callback;
  139. }
  140. if ($options['wrap']) {
  141. $callback = sprintf($function, $callback);
  142. }
  143. $out = $this->selection . ".observe(\"{$type}\", $callback);";
  144. return $out;
  145. }
  146. /**
  147. * Create a domReady event. This is a special event in many libraries
  148. *
  149. * @param string $functionBody The code to run on domReady
  150. * @return string completed domReady method
  151. */
  152. public function domReady($functionBody) {
  153. $this->selection = 'document';
  154. return $this->event('dom:loaded', $functionBody, array('stop' => false));
  155. }
  156. /**
  157. * Create an iteration over the current selection result.
  158. *
  159. * @param string $callback The function body you wish to apply during the iteration.
  160. * @return string completed iteration
  161. */
  162. public function each($callback) {
  163. return $this->selection . '.each(function (item, index) {' . $callback . '});';
  164. }
  165. /**
  166. * Trigger an Effect.
  167. *
  168. * ### Note: Effects require Scriptaculous to be loaded.
  169. *
  170. * @param string $name The name of the effect to trigger.
  171. * @param array $options Array of options for the effect.
  172. * @return string completed string with effect.
  173. * @see JsBaseEngineHelper::effect()
  174. */
  175. public function effect($name, $options = array()) {
  176. $effect = '';
  177. $optionString = null;
  178. if (isset($options['speed'])) {
  179. if ($options['speed'] == 'fast') {
  180. $options['duration'] = 0.5;
  181. } elseif ($options['speed'] == 'slow') {
  182. $options['duration'] = 2;
  183. } else {
  184. $options['duration'] = 1;
  185. }
  186. unset($options['speed']);
  187. }
  188. if (!empty($options)) {
  189. $optionString = ', {' . $this->_parseOptions($options) . '}';
  190. }
  191. switch ($name) {
  192. case 'hide':
  193. case 'show':
  194. $effect = $this->selection . '.' . $name . '();';
  195. break;
  196. case 'slideIn':
  197. case 'slideOut':
  198. $name = ($name == 'slideIn') ? 'slideDown' : 'slideUp';
  199. $effect = 'Effect.' . $name . '(' . $this->selection . $optionString . ');';
  200. break;
  201. case 'fadeIn':
  202. case 'fadeOut':
  203. $name = ($name == 'fadeIn') ? 'appear' : 'fade';
  204. $effect = $this->selection . '.' . $name .'(' . substr($optionString, 2) . ');';
  205. break;
  206. }
  207. return $effect;
  208. }
  209. /**
  210. * Create an Ajax or Ajax.Updater call.
  211. *
  212. * @param mixed $url
  213. * @param array $options
  214. * @return string The completed ajax call.
  215. */
  216. public function request($url, $options = array()) {
  217. $url = '"'. $this->url($url) . '"';
  218. $options = $this->_mapOptions('request', $options);
  219. $type = '.Request';
  220. $data = null;
  221. if (isset($options['type']) && strtolower($options['type']) == 'json') {
  222. unset($options['type']);
  223. }
  224. if (isset($options['update'])) {
  225. $url = '"' . str_replace('#', '', $options['update']) . '", ' . $url;
  226. $type = '.Updater';
  227. unset($options['update'], $options['type']);
  228. }
  229. $safe = array_keys($this->_callbackArguments['request']);
  230. $options = $this->_prepareCallbacks('request', $options, $safe);
  231. if (!empty($options['dataExpression'])) {
  232. $safe[] = 'parameters';
  233. unset($options['dataExpression']);
  234. }
  235. $options = $this->_parseOptions($options, $safe);
  236. if (!empty($options)) {
  237. $options = ', {' . $options . '}';
  238. }
  239. return "var jsRequest = new Ajax$type($url$options);";
  240. }
  241. /**
  242. * Create a sortable element.
  243. *
  244. * #### Note: Requires scriptaculous to be loaded.
  245. *
  246. * The scriptaculous implementation of sortables does not suppot the 'start'
  247. * and 'distance' options.
  248. *
  249. * @param array $options Array of options for the sortable.
  250. * @return string Completed sortable script.
  251. * @see JsBaseEngineHelper::sortable() for options list.
  252. */
  253. public function sortable($options = array()) {
  254. $options = $this->_processOptions('sortable', $options);
  255. if (!empty($options)) {
  256. $options = ', {' . $options . '}';
  257. }
  258. return 'var jsSortable = Sortable.create(' . $this->selection . $options . ');';
  259. }
  260. /**
  261. * Create a Draggable element.
  262. *
  263. * #### Note: Requires scriptaculous to be loaded.
  264. *
  265. * @param array $options Array of options for the draggable.
  266. * @return string Completed draggable script.
  267. * @see JsBaseEngineHelper::draggable() for options list.
  268. */
  269. public function drag($options = array()) {
  270. $options = $this->_processOptions('drag', $options);
  271. if (!empty($options)) {
  272. $options = ', {' . $options . '}';
  273. }
  274. if ($this->_multiple) {
  275. return $this->each('new Draggable(item' . $options . ');');
  276. }
  277. return 'var jsDrag = new Draggable(' . $this->selection . $options . ');';
  278. }
  279. /**
  280. * Create a Droppable element.
  281. *
  282. * #### Note: Requires scriptaculous to be loaded.
  283. *
  284. * @param array $options Array of options for the droppable.
  285. * @return string Completed droppable script.
  286. * @see JsBaseEngineHelper::droppable() for options list.
  287. */
  288. public function drop($options = array()) {
  289. $options = $this->_processOptions('drop', $options);
  290. if (!empty($options)) {
  291. $options = ', {' . $options . '}';
  292. }
  293. return 'Droppables.add(' . $this->selection . $options . ');';
  294. }
  295. /**
  296. * Creates a slider control widget.
  297. *
  298. * ### Note: Requires scriptaculous to be loaded.
  299. *
  300. * @param array $options Array of options for the slider.
  301. * @return string Completed slider script.
  302. * @see JsBaseEngineHelper::slider() for options list.
  303. */
  304. public function slider($options = array()) {
  305. $slider = $this->selection;
  306. $this->get($options['handle']);
  307. unset($options['handle']);
  308. if (isset($options['min']) && isset($options['max'])) {
  309. $options['range'] = sprintf('$R(%s,%s)', $options['min'], $options['max']);
  310. unset($options['min'], $options['max']);
  311. }
  312. $options = $this->_mapOptions('slider', $options);
  313. $options = $this->_prepareCallbacks('slider', $options);
  314. $optionString = $this->_parseOptions(
  315. $options, array_merge(array_keys($this->_callbackArguments['slider']), array('range'))
  316. );
  317. if (!empty($optionString)) {
  318. $optionString = ', {' . $optionString . '}';
  319. }
  320. $out = 'var jsSlider = new Control.Slider(' . $this->selection . ', ' . $slider . $optionString . ');';
  321. $this->selection = $slider;
  322. return $out;
  323. }
  324. /**
  325. * Serialize the form attached to $selector.
  326. *
  327. * @param array $options Array of options.
  328. * @return string Completed serializeForm() snippet
  329. * @see JsBaseEngineHelper::serializeForm()
  330. */
  331. public function serializeForm($options = array()) {
  332. $options = array_merge(array('isForm' => false, 'inline' => false), $options);
  333. $selection = $this->selection;
  334. if (!$options['isForm']) {
  335. $selection = '$(' . $this->selection . '.form)';
  336. }
  337. $method = '.serialize()';
  338. if (!$options['inline']) {
  339. $method .= ';';
  340. }
  341. return $selection . $method;
  342. }
  343. }