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

/cake/libs/view/helpers/js.php

https://github.com/mariuz/firetube
PHP | 450 lines | 367 code | 18 blank | 65 comment | 17 complexity | 22236a30d4e0d051fd8ef772f2ca7854 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * Javascript Generator class file.
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * CakePHP : Rapid Development Framework (http://cakephp.org)
  9. * Copyright 2005-2011, Cake Software Foundation, Inc.
  10. *
  11. * Licensed under The MIT License
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright 2005-2011, Cake Software Foundation, Inc.
  15. * @link http://cakefoundation.org/projects/info/cakephp CakePHP Project
  16. * @package cake
  17. * @subpackage cake.cake.libs.view.helpers
  18. * @since CakePHP v 1.2
  19. * @version $Revision$
  20. * @modifiedby $LastChangedBy$
  21. * @lastmodified $Date$
  22. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  23. */
  24. /**
  25. * Javascript Generator helper class for easy use of JavaScript.
  26. *
  27. * JsHelper provides an abstract interface for authoring JavaScript with a
  28. * given client-side library.
  29. *
  30. * @package cake
  31. * @subpackage cake.cake.libs.view.helpers
  32. */
  33. class JsHelper extends Overloadable2 {
  34. var $base = null;
  35. var $webroot = null;
  36. var $here = null;
  37. var $params = null;
  38. var $action = null;
  39. var $data = null;
  40. var $themeWeb = null;
  41. var $plugin = null;
  42. var $helpers = array();
  43. var $hook = null;
  44. var $__objects = array();
  45. var $effectMap = array(
  46. 'Appear', 'Fade', 'Puff', 'BlindDown', 'BlindUp', 'SwitchOff', 'SlideDown', 'SlideUp',
  47. 'DropOut', 'Shake', 'Pulsate', 'Squish', 'Fold', 'Grow', 'Shrink', 'Highlight', 'toggle'
  48. );
  49. var $output = false;
  50. function __construct() {
  51. $this->effectMap = array_combine(
  52. array_map('strtolower', $this->effectMap),
  53. $this->effectMap
  54. );
  55. parent::__construct();
  56. }
  57. function call__($method, $params) {
  58. if (is_object($this->hook) && method_exists($this->hook, $method)) {
  59. $this->hook->dispatchMethod($method . '_', $params);
  60. }
  61. if (method_exists($this, $method . '_')) {
  62. return $this->dispatchMethod($method . '_', $params);
  63. }
  64. }
  65. function alert_($message) {
  66. return 'alert("' . $this->escape($message) . '");';
  67. }
  68. function if_($if, $then, $else = null, $elseIf = array()) {
  69. $len = strlen($if) - 1;
  70. if ($if{$len} == ';') {
  71. $if{$len} = null;
  72. }
  73. $out = 'if (' . $if . ') { ' . $then . ' }';
  74. foreach ($elseIf as $cond => $exec) {
  75. //$out .=
  76. }
  77. if (!empty($else)) {
  78. $out .= ' else { ' . $else . ' }';
  79. }
  80. return $out;
  81. }
  82. function confirm_($message) {
  83. return 'confirm("' . $this->escape($message) . '");';
  84. }
  85. function prompt_($message, $default = '') {
  86. return 'prompt("' . $this->escape($message) . '", "' . $this->escape($default) . '");';
  87. }
  88. /*
  89. * Tries a series of expressions, and executes after first successful completion.
  90. * (See Prototype's Try.these).
  91. *
  92. * @return string
  93. */
  94. function tryThese_($expr1, $expr2, $expr3) {
  95. }
  96. /**
  97. * Loads a remote URL
  98. *
  99. * @param string $url
  100. * @param array $options
  101. * @return string
  102. */
  103. function load_($url = null, $options = array()) {
  104. if (isset($options['update'])) {
  105. if (!is_array($options['update'])) {
  106. $func = "new Ajax.Updater('{$options['update']}',";
  107. } else {
  108. $func = "new Ajax.Updater(document.createElement('div'),";
  109. }
  110. if (!isset($options['requestHeaders'])) {
  111. $options['requestHeaders'] = array();
  112. }
  113. if (is_array($options['update'])) {
  114. $options['update'] = implode(' ', $options['update']);
  115. }
  116. $options['requestHeaders']['X-Update'] = $options['update'];
  117. } else {
  118. $func = "new Ajax.Request(";
  119. }
  120. $func .= "'" . Router::url($url) . "'";
  121. $ajax =& new AjaxHelper();
  122. $func .= ", " . $ajax->__optionsForAjax($options) . ")";
  123. if (isset($options['before'])) {
  124. $func = "{$options['before']}; $func";
  125. }
  126. if (isset($options['after'])) {
  127. $func = "$func; {$options['after']};";
  128. }
  129. if (isset($options['condition'])) {
  130. $func = "if ({$options['condition']}) { $func; }";
  131. }
  132. if (isset($options['confirm'])) {
  133. $func = "if (confirm('" . $this->Javascript->escapeString($options['confirm'])
  134. . "')) { $func; } else { return false; }";
  135. }
  136. return $func;
  137. }
  138. /**
  139. * Redirects to a URL
  140. *
  141. * @param mixed $url
  142. * @param array $options
  143. * @return string
  144. */
  145. function redirect_($url = null) {
  146. return 'window.location = "' . Router::url($url) . '";';
  147. }
  148. /**
  149. * Escape a string to be JavaScript friendly.
  150. *
  151. * List of escaped ellements:
  152. * + "\r\n" => '\n'
  153. * + "\r" => '\n'
  154. * + "\n" => '\n'
  155. * + '"' => '\"'
  156. * + "'" => "\\'"
  157. *
  158. * @param string $script String that needs to get escaped.
  159. * @return string Escaped string.
  160. */
  161. function escape($string) {
  162. $escape = array("\r\n" => '\n', "\r" => '\n', "\n" => '\n', '"' => '\"', "'" => "\\'");
  163. return str_replace(array_keys($escape), array_values($escape), $string);
  164. }
  165. function get__($name) {
  166. return $this->__object($name, 'id');
  167. }
  168. function select($pattern) {
  169. return $this->__object($pattern, 'pattern');
  170. }
  171. function real($var) {
  172. return $this->__object($var, 'real');
  173. }
  174. function __object($name, $var) {
  175. if (!isset($this->__objects[$name])) {
  176. $this->__objects[$name] = new JsHelperObject($this);
  177. $this->__objects[$name]->{$var} = $name;
  178. }
  179. return $this->__objects[$name];
  180. }
  181. /**
  182. * Generates a JavaScript object in JavaScript Object Notation (JSON)
  183. * from an array
  184. *
  185. * @param array $data Data to be converted
  186. * @param boolean $block Wraps return value in a <script/> block if true
  187. * @param string $prefix Prepends the string to the returned data
  188. * @param string $postfix Appends the string to the returned data
  189. * @param array $stringKeys A list of array keys to be treated as a string
  190. * @param boolean $quoteKeys If false, treats $stringKey as a list of keys *not* to be quoted
  191. * @param string $q The type of quote to use
  192. * @return string A JSON code block
  193. */
  194. function object($data = array(), $block = false, $prefix = '', $postfix = '', $stringKeys = array(), $quoteKeys = true, $q = "\"") {
  195. if (is_object($data)) {
  196. $data = get_object_vars($data);
  197. }
  198. $out = array();
  199. $key = array();
  200. if (is_array($data)) {
  201. $keys = array_keys($data);
  202. }
  203. $numeric = true;
  204. if (!empty($keys)) {
  205. foreach ($keys as $key) {
  206. if (!is_numeric($key)) {
  207. $numeric = false;
  208. break;
  209. }
  210. }
  211. }
  212. foreach ($data as $key => $val) {
  213. if (is_array($val) || is_object($val)) {
  214. $val = $this->object($val, false, '', '', $stringKeys, $quoteKeys, $q);
  215. } else {
  216. if ((!count($stringKeys) && !is_numeric($val) && !is_bool($val)) || ($quoteKeys && in_array($key, $stringKeys)) || (!$quoteKeys && !in_array($key, $stringKeys)) && $val !== null) {
  217. $val = $q . $this->escapeString($val) . $q;
  218. }
  219. if ($val == null) {
  220. $val = 'null';
  221. }
  222. }
  223. if (!$numeric) {
  224. $val = $q . $key . $q . ':' . $val;
  225. }
  226. $out[] = $val;
  227. }
  228. if (!$numeric) {
  229. $rt = '{' . implode(', ', $out) . '}';
  230. } else {
  231. $rt = '[' . implode(', ', $out) . ']';
  232. }
  233. $rt = $prefix . $rt . $postfix;
  234. if ($block) {
  235. $rt = $this->codeBlock($rt);
  236. }
  237. return $rt;
  238. }
  239. }
  240. class JsHelperObject {
  241. var $__parent = null;
  242. var $id = null;
  243. var $pattern = null;
  244. var $real = null;
  245. function __construct(&$parent) {
  246. if (is_object($parent)) {
  247. $this->setParent($parent);
  248. }
  249. }
  250. function toString() {
  251. return $this->__toString();
  252. }
  253. function __toString() {
  254. return $this->literal;
  255. }
  256. function ref($ref = null) {
  257. if ($ref == null) {
  258. foreach (array('id', 'pattern', 'real') as $ref) {
  259. if ($this->{$ref} !== null) {
  260. return $this->{$ref};
  261. }
  262. }
  263. } else {
  264. return ($this->{$ref} !== null);
  265. }
  266. return null;
  267. }
  268. function literal($append = null) {
  269. if (!empty($this->id)) {
  270. $data = '$("' . $this->id . '")';
  271. }
  272. if (!empty($this->pattern)) {
  273. $data = '$$("' . $this->pattern . '")';
  274. }
  275. if (!empty($this->real)) {
  276. $data = $this->real;
  277. }
  278. if (!empty($append)) {
  279. $data .= '.' . $append;
  280. }
  281. return $data;
  282. }
  283. function __call($name, $args) {
  284. $data = '';
  285. if (isset($this->__parent->effectMap[strtolower($name)])) {
  286. array_unshift($args, $this->__parent->effectMap[strtolower($name)]);
  287. $name = 'effect';
  288. }
  289. switch ($name) {
  290. case 'effect':
  291. case 'visualEffect':
  292. if (strpos($args[0], '_') || $args[0]{0} != strtoupper($args[0]{0})) {
  293. $args[0] = Inflector::camelize($args[0]);
  294. }
  295. if (strtolower($args[0]) == 'highlight') {
  296. $data .= 'new ';
  297. }
  298. if ($this->pattern == null) {
  299. $data .= 'Effect.' . $args[0] . '(' . $this->literal();
  300. } else {
  301. $data .= 'Effect.' . $args[0] . '(item';
  302. }
  303. if (isset($args[1]) && is_array($args[1])) {
  304. $data .= ', {' . $this->__options($args[1]) . '}';
  305. }
  306. $data .= ');';
  307. if ($this->pattern !== null) {
  308. $data = $this->each($data);
  309. }
  310. break;
  311. case 'remove':
  312. case 'toggle':
  313. case 'show':
  314. case 'hide':
  315. if (empty($args)) {
  316. $obj = 'Element';
  317. $params = '';
  318. } else {
  319. $obj = 'Effect';
  320. $params = ', "' . $args[0] . '"';
  321. }
  322. if ($this->pattern != null) {
  323. $data = $this->each($obj . ".{$name}(item);");
  324. } else {
  325. $data = $obj . ".{$name}(" . $this->literal() . ');';
  326. }
  327. break;
  328. case 'visible':
  329. $data = $this->literal() . '.visible();';
  330. break;
  331. case 'update':
  332. $data = $this->literal() . ".update({$args[0]});";
  333. break;
  334. case 'load':
  335. $data = 'new Ajax.Updater("' . $this->id . '", "' . $args[0] . '"';
  336. if (isset($args[1]) && is_array($args[1])) {
  337. $data .= ', {' . $this->__options($args[1]) . '}';
  338. }
  339. $data .= ');';
  340. break;
  341. case 'each':
  342. case 'all':
  343. case 'any':
  344. case 'detect':
  345. case 'findAll':
  346. if ($this->pattern != null) {
  347. $data = $this->__iterate($name, $args[0]);
  348. }
  349. break;
  350. case 'addClass':
  351. case 'removeClass':
  352. case 'hasClass':
  353. case 'toggleClass':
  354. $data = $this->literal() . ".{$name}Name(\"{$args[0]}\");";
  355. break;
  356. case 'clone':
  357. case 'inspect':
  358. case 'keys':
  359. case 'values':
  360. $data = "Object.{$name}(" . $this->literal() . ");";
  361. break;
  362. case 'extend':
  363. $data = "Object.extend(" . $this->literal() . ", {$args[0]});";
  364. break;
  365. case '...':
  366. // Handle other methods here
  367. // including interfaces to load other files on-the-fly
  368. // that add support for additional methods/replacing existing methods
  369. break;
  370. default:
  371. $data = $this->literal() . '.' . $name . '();';
  372. break;
  373. }
  374. if ($this->__parent->output) {
  375. echo $data;
  376. } else {
  377. return $data;
  378. }
  379. }
  380. function __iterate($method, $data) {
  381. return '$$("' . $this->pattern . '").' . $method . '(function(item) {' . $data . '});';
  382. }
  383. function setParent(&$parent) {
  384. $this->__parent =& $parent;
  385. }
  386. function __options($opts) {
  387. $options = array();
  388. foreach ($opts as $key => $val) {
  389. if (!is_int($val)) {
  390. $val = '"' . $val . '"';
  391. }
  392. $options[] = $key . ':' . $val;
  393. }
  394. return implode(', ', $options);
  395. }
  396. }
  397. ?>