[ Index ]
 

Code source de CakePHP 1.1.13.4450

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/cake/libs/view/helpers/ -> javascript.php (source)

   1  <?php
   2  /* SVN FILE: $Id: javascript.php 4409 2007-02-02 13:20:59Z phpnut $ */
   3  /**
   4   * Javascript Helper class file.
   5   *
   6   * PHP versions 4 and 5
   7   *
   8   * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
   9   * Copyright 2005-2007, Cake Software Foundation, Inc.
  10   *                                1785 E. Sahara Avenue, Suite 490-204
  11   *                                Las Vegas, Nevada 89104
  12   *
  13   * Licensed under The MIT License
  14   * Redistributions of files must retain the above copyright notice.
  15   *
  16   * @filesource
  17   * @copyright        Copyright 2005-2007, Cake Software Foundation, Inc.
  18   * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
  19   * @package            cake
  20   * @subpackage        cake.cake.libs.view.helpers
  21   * @since            CakePHP(tm) v 0.10.0.1076
  22   * @version            $Revision: 4409 $
  23   * @modifiedby        $LastChangedBy: phpnut $
  24   * @lastmodified    $Date: 2007-02-02 07:20:59 -0600 (Fri, 02 Feb 2007) $
  25   * @license            http://www.opensource.org/licenses/mit-license.php The MIT License
  26   */
  27  /**
  28   * Javascript Helper class for easy use of JavaScript.
  29   *
  30   * JavascriptHelper encloses all methods needed while working with JavaScript.
  31   *
  32   * @package        cake
  33   * @subpackage    cake.cake.libs.view.helpers
  34   */
  35  class JavascriptHelper extends Helper{
  36      var $_cachedEvents = array();
  37      var $_cacheEvents = false;
  38      var $_cacheToFile = false;
  39      var $_cacheAll = false;
  40      var $_rules = array();
  41  /**
  42   * Returns a JavaScript script tag.
  43   *
  44   * @param  string $script The JavaScript to be wrapped in SCRIPT tags.
  45   * @param  boolean $allowCache Allows the script to be cached if non-event caching is active
  46   * @return string The full SCRIPT element, with the JavaScript inside it.
  47   * @access public
  48   */
  49  	function codeBlock($script, $allowCache = true) {
  50          if ($this->_cacheEvents && $this->_cacheAll && $allowCache) {
  51              $this->_cachedEvents[] = $script;
  52          } else {
  53              return sprintf($this->tags['javascriptblock'], $script);
  54          }
  55      }
  56  /**
  57   * Returns a JavaScript include tag (SCRIPT element)
  58   *
  59   * @param  string $url URL to JavaScript file.
  60   * @return string
  61   * @access public
  62   */
  63  	function link($url) {
  64          if (strpos($url, ".") === false) {
  65              $url .= ".js";
  66          }
  67          return sprintf($this->tags['javascriptlink'], $this->webroot . $this->themeWeb . JS_URL . $url);
  68      }
  69  /**
  70   * Returns a JavaScript include tag for an externally-hosted script
  71   *
  72   * @param  string $url URL to JavaScript file.
  73   * @return string
  74   * @access public
  75   */
  76  	function linkOut($url) {
  77          if (strpos($url, ".") === false) {
  78              $url .= ".js";
  79          }
  80          return sprintf($this->tags['javascriptlink'], $url);
  81      }
  82  /**
  83   * Escape carriage returns and single and double quotes for JavaScript segments.
  84   *
  85   * @param string $script string that might have javascript elements
  86   * @return string escaped string
  87   * @access public
  88   */
  89  	function escapeScript($script) {
  90          $script = r(array("\r\n", "\n", "\r"), '\n', $script);
  91          $script = r(array('"', "'"), array('\"', "\\'"), $script);
  92          return $script;
  93      }
  94  /**
  95   * Escape a string to be JavaScript friendly.
  96   *
  97   * List of escaped ellements:
  98   *    + "\r\n" => '\n'
  99   *    + "\r" => '\n'
 100   *    + "\n" => '\n'
 101   *    + '"' => '\"'
 102   *    + "'" => "\\'"
 103   *
 104   * @param  string $script String that needs to get escaped.
 105   * @return string Escaped string.
 106   * @access public
 107   */
 108  	function escapeString($string) {
 109          $escape = array("\r\n" => '\n', "\r" => '\n', "\n" => '\n', '"' => '\"', "'" => "\\'");
 110          return r(array_keys($escape), array_values($escape), $string);
 111      }
 112  /**
 113   * Attach an event to an element. Used with the Prototype library.
 114   *
 115   * @param string $object Object to be observed
 116   * @param string $event event to observe
 117   * @param string $observer function to call
 118   * @param boolean $useCapture default true
 119   * @return boolean true on success
 120   * @access public
 121   */
 122  	function event($object, $event, $observer = null, $useCapture = false) {
 123  
 124          if ($useCapture == true) {
 125              $useCapture = "true";
 126          } else {
 127              $useCapture = "false";
 128          }
 129  
 130          if ($object == 'window' || strpos($object, '$(') !== false || strpos($object, '"') !== false || strpos($object, '\'') !== false) {
 131              $b = "Event.observe($object, '$event', function(event){ $observer }, $useCapture);";
 132          } else {
 133              $chars = array('#', ' ', ', ', '.', ':');
 134              $found = false;
 135              foreach ($chars as $char) {
 136                  if (strpos($object, $char) !== false) {
 137                      $found = true;
 138                      break;
 139                  }
 140              }
 141              if ($found) {
 142                  $this->_rules[$object] = $event;
 143              } else {
 144                  $b = "Event.observe(\$('$object'), '$event', function(event){ $observer }, $useCapture);";
 145              }
 146          }
 147  
 148          if (isset($b) && !empty($b)) {
 149              if ($this->_cacheEvents === true) {
 150                  $this->_cachedEvents[] = $b;
 151                  return;
 152              } else {
 153                  return $this->codeBlock($b);
 154              }
 155          }
 156      }
 157  /**
 158   * Cache JavaScript events created with event()
 159   *
 160   * @param boolean $file If true, code will be written to a file
 161   * @param boolean $all If true, all code written with JavascriptHelper will be sent to a file
 162   * @return void
 163   * @access public
 164   */
 165  	function cacheEvents($file = false, $all = false) {
 166          $this->_cacheEvents = true;
 167          $this->_cacheToFile = $file;
 168          $this->_cacheAll = $all;
 169      }
 170  /**
 171   * Write cached JavaScript events
 172   *
 173   * @return string
 174   * @access public
 175   */
 176  	function writeEvents() {
 177  
 178          $rules = array();
 179          if (!empty($this->_rules)) {
 180              foreach ($this->_rules as $sel => $event) {
 181                  $rules[] = "\t'{$sel}': function(element, event) {\n\t\t{$event}\n\t}";
 182              }
 183              $this->_cacheEvents = true;
 184          }
 185  
 186          if ($this->_cacheEvents) {
 187  
 188              $this->_cacheEvents = false;
 189              $events = $this->_cachedEvents;
 190              $data = implode("\n", $events);
 191              $this->_cachedEvents = array();
 192  
 193              if (!empty($rules)) {
 194                  $data .= "\n\nvar SelectorRules = {\n" . implode(",\n\n", $rules) . "\n}\n";
 195                  $data .= "\nEventSelectors.start(SelectorRules);\n";
 196              }
 197  
 198              if (!empty($events) || !empty($rules)) {
 199                  if ($this->_cacheToFile) {
 200                      $filename = md5($data);
 201                      if (!file_exists(JS . $filename . '.js')) {
 202                          cache(r(WWW_ROOT, '', JS) . $filename . '.js', $data, '+999 days', 'public');
 203                      }
 204                      return $this->link($filename);
 205                  } else {
 206                      return $this->codeBlock("\n" . $data . "\n");
 207                  }
 208              }
 209          }
 210      }
 211  /**
 212   * Includes the Prototype Javascript library (and anything else) inside a single script tag.
 213   *
 214   * Note: The recommended approach is to copy the contents of
 215   * javascripts into your application's
 216   * public/javascripts/ directory, and use @see javascriptIncludeTag() to
 217   * create remote script links.
 218   *
 219   * @param string $script name of script to include
 220   * @return string script with all javascript in/javascripts folder
 221   * @access public
 222   */
 223  	function includeScript($script = "") {
 224          if ($script == "") {
 225              $files = scandir(JS);
 226              $javascript = '';
 227  
 228              foreach($files as $file) {
 229                  if (substr($file, -3) == '.js') {
 230                      $javascript .= file_get_contents(JS . "{$file}") . "\n\n";
 231                  }
 232              }
 233          } else {
 234              $javascript = file_get_contents(JS . "$script.js") . "\n\n";
 235          }
 236          return $this->codeBlock("\n\n" . $javascript);
 237      }
 238  /**
 239   * Generates a JavaScript object in JavaScript Object Notation (JSON)
 240   * from an array
 241   *
 242   * @param array $data Data to be converted
 243   * @param boolean $block Wraps return value in a <script/> block if true
 244   * @param string $prefix Prepends the string to the returned data
 245   * @param string $postfix Appends the string to the returned data
 246   * @param array $stringKeys A list of array keys to be treated as a string
 247   * @param boolean $quoteKeys If false, treats $stringKey as a list of keys *not* to be quoted
 248   * @param string $q The type of quote to use
 249   * @return string A JSON code block
 250   * @access public
 251   */
 252  	function object($data = array(), $block = false, $prefix = '', $postfix = '', $stringKeys = array(), $quoteKeys = true, $q = "\"") {
 253          if (is_object($data)) {
 254              $data = get_object_vars($data);
 255          }
 256  
 257          $out = array();
 258          $key = array();
 259  
 260          if (is_array($data)) {
 261              $keys = array_keys($data);
 262          }
 263  
 264          $numeric = true;
 265  
 266          if (!empty($keys)) {
 267              foreach($keys as $key) {
 268                  if (!is_numeric($key)) {
 269                      $numeric = false;
 270                      break;
 271                  }
 272              }
 273          }
 274  
 275          foreach($data as $key => $val) {
 276              if (is_array($val) || is_object($val)) {
 277                  $val = $this->object($val, false, '', '', $stringKeys, $quoteKeys, $q);
 278              } else {
 279                  if ((!count($stringKeys) && !is_numeric($val) && !is_bool($val)) || ($quoteKeys && in_array($key, $stringKeys, true)) || (!$quoteKeys && !in_array($key, $stringKeys, true))) {
 280                      $val = $q . $this->escapeString($val) . $q;
 281                  }
 282                  if (trim($val) == '') {
 283                      $val = 'null';
 284                  }
 285              }
 286  
 287              if (!$numeric) {
 288                  $val = $q . $key . $q . ':' . $val;
 289              }
 290  
 291              $out[] = $val;
 292          }
 293  
 294          if (!$numeric) {
 295              $rt = '{' . join(', ', $out) . '}';
 296          } else {
 297              $rt = '[' . join(', ', $out) . ']';
 298          }
 299          $rt = $prefix . $rt . $postfix;
 300  
 301          if ($block) {
 302              $rt = $this->codeBlock($rt);
 303          }
 304  
 305          return $rt;
 306      }
 307  /**
 308   * AfterRender callback.  Writes any cached events to the view, or to a temp file.
 309   *
 310   * @return void
 311   * @access public
 312   */
 313  	function afterRender() {
 314          echo $this->writeEvents();
 315      }
 316  }
 317  ?>


Généré le : Sun Feb 25 19:27:47 2007 par Balluche grâce à PHPXref 0.7