PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/libs/view/helpers/prototype_engine.php

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