* Copyright (c) 2006, Cake Software Foundation, Inc. * 1785 E. Sahara Avenue, Suite 490-204 * Las Vegas, Nevada 89104 * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * * @filesource * @copyright Copyright (c) 2006, Cake Software Foundation, Inc. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP Project * @package cake * @subpackage cake.cake.libs.view.helpers * @since CakePHP v 1.2 * @version $Revision$ * @modifiedby $LastChangedBy$ * @lastmodified $Date$ * @license http://www.opensource.org/licenses/mit-license.php The MIT License */ /** * Javascript Generator helper class for easy use of JavaScript. * * JsHelper provides an abstract interface for authoring JavaScript with a * given client-side library. * * @package cake * @subpackage cake.cake.libs.view.helpers */ class JsHelper extends Overloadable2 { var $base = null; var $webroot = null; var $here = null; var $params = null; var $action = null; var $data = null; var $themeWeb = null; var $plugin = null; var $helpers = array(); var $hook = null; var $__objects = array(); var $effectMap = array( 'Appear', 'Fade', 'Puff', 'BlindDown', 'BlindUp', 'SwitchOff', 'SlideDown', 'SlideUp', 'DropOut', 'Shake', 'Pulsate', 'Squish', 'Fold', 'Grow', 'Shrink', 'Highlight', 'toggle' ); var $output = false; function __construct() { $this->effectMap = array_combine( array_map('strtolower', $this->effectMap), $this->effectMap ); parent::__construct(); } function call__($method, $params) { if (is_object($this->hook) && method_exists($this->hook, $method)) { } if (method_exists($this, $method . '_')) { return call_user_func_array(array(&$this, $method . '_'), $params); } } function alert_($message) { return 'alert("' . $this->escape($message) . '");'; } function if_($if, $then, $else = null, $elseif = array()) { $len = strlen($if) - 1; if ($if{$len} == ';') { $if{$len} = null; } $out = 'if (' . $if . ') { ' . $then . ' }'; foreach ($elseif as $cond => $exec) { //$out .= } if (!empty($else)) { $out .= ' else { ' . $else . ' }'; } return $out; } function confirm_($message) { return 'confirm("' . $this->escape($message) . '");'; } function prompt_($message, $default = '') { return 'prompt("' . $this->escape($message) . '", "' . $this->escape($default) . '");'; } /* * Tries a series of expressions, and executes after first successful completion. * (See Prototype's Try.these). * * @return string */ function tryThese_($expr1, $expr2, $expr3) { } /** * Loads a remote URL * * @param string $url * @param array $options * @return string */ function load_($url = null, $options = array()) { if (isset($options['update'])) { if (!is_array($options['update'])) { $func = "new Ajax.Updater('{$options['update']}',"; } else { $func = "new Ajax.Updater(document.createElement('div'),"; } if (!isset($options['requestHeaders'])) { $options['requestHeaders'] = array(); } if (is_array($options['update'])) { $options['update'] = join(' ', $options['update']); } $options['requestHeaders']['X-Update'] = $options['update']; } else { $func = "new Ajax.Request("; } $func .= "'" . Router::url($url) . "'"; $ajax =& new AjaxHelper(); $func .= ", " . $ajax->__optionsForAjax($options) . ")"; if (isset($options['before'])) { $func = "{$options['before']}; $func"; } if (isset($options['after'])) { $func = "$func; {$options['after']};"; } if (isset($options['condition'])) { $func = "if ({$options['condition']}) { $func; }"; } if (isset($options['confirm'])) { $func = "if (confirm('" . $this->Javascript->escapeString($options['confirm']) . "')) { $func; } else { return false; }"; } return $func; } /** * Redirects to a URL * * @param mixed $url * @param array $options * @return string */ function redirect_($url = null) { return 'window.location = "' . Router::url($url) . '";'; } /** * Escape a string to be JavaScript friendly. * * List of escaped ellements: * + "\r\n" => '\n' * + "\r" => '\n' * + "\n" => '\n' * + '"' => '\"' * + "'" => "\\'" * * @param string $script String that needs to get escaped. * @return string Escaped string. */ function escape($string) { $escape = array("\r\n" => '\n', "\r" => '\n', "\n" => '\n', '"' => '\"', "'" => "\\'"); return r(array_keys($escape), array_values($escape), $string); } function get__($name) { return $this->__object($name, 'id'); } function select($pattern) { return $this->__object($pattern, 'pattern'); } function real($var) { return $this->__object($var, 'real'); } function __object($name, $var) { if (!isset($this->__objects[$name])) { $this->__objects[$name] = new JsHelperObject($this); $this->__objects[$name]->{$var} = $name; } return $this->__objects[$name]; } /** * Generates a JavaScript object in JavaScript Object Notation (JSON) * from an array * * @param array $data Data to be converted * @param bool $block Wraps return value in a