javascript.php

Go to the documentation of this file.
00001 <?php
00002 /* SVN FILE: $Id: javascript_8php-source.html 675 2008-12-26 00:27:14Z gwoo $ */
00003 /**
00004  * Javascript Helper class file.
00005  *
00006  * PHP versions 4 and 5
00007  *
00008  * CakePHP(tm) :  Rapid Development Framework <http://www.cakephp.org/>
00009  * Copyright 2005-2008, Cake Software Foundation, Inc.
00010  *                              1785 E. Sahara Avenue, Suite 490-204
00011  *                              Las Vegas, Nevada 89104
00012  *
00013  * Licensed under The MIT License
00014  * Redistributions of files must retain the above copyright notice.
00015  *
00016  * @filesource
00017  * @copyright       Copyright 2005-2008, Cake Software Foundation, Inc.
00018  * @link                http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
00019  * @package         cake
00020  * @subpackage      cake.cake.libs.view.helpers
00021  * @since           CakePHP(tm) v 0.10.0.1076
00022  * @version         $Revision: 675 $
00023  * @modifiedby      $LastChangedBy: gwoo $
00024  * @lastmodified    $Date: 2008-12-25 16:27:14 -0800 (Thu, 25 Dec 2008) $
00025  * @license         http://www.opensource.org/licenses/mit-license.php The MIT License
00026  */
00027 /**
00028  * Javascript Helper class for easy use of JavaScript.
00029  *
00030  * JavascriptHelper encloses all methods needed while working with JavaScript.
00031  *
00032  * @package     cake
00033  * @subpackage  cake.cake.libs.view.helpers
00034  */
00035 class JavascriptHelper extends Helper{
00036     var $_cachedEvents = array();
00037     var $_cacheEvents = false;
00038     var $_cacheToFile = false;
00039     var $_cacheAll = false;
00040     var $_rules = array();
00041 /**
00042  * Returns a JavaScript script tag.
00043  *
00044  * @param  string $script The JavaScript to be wrapped in SCRIPT tags.
00045  * @param  boolean $allowCache Allows the script to be cached if non-event caching is active
00046  * @return string The full SCRIPT element, with the JavaScript inside it.
00047  * @access public
00048  */
00049     function codeBlock($script, $allowCache = true) {
00050         if ($this->_cacheEvents && $this->_cacheAll && $allowCache) {
00051             $this->_cachedEvents[] = $script;
00052         } else {
00053             return sprintf($this->tags['javascriptblock'], $script);
00054         }
00055     }
00056 /**
00057  * Returns a JavaScript include tag (SCRIPT element)
00058  *
00059  * @param  string $url URL to JavaScript file.
00060  * @return string
00061  * @access public
00062  */
00063     function link($url) {
00064         if (strpos($url, '.js') === false) {
00065             $url .= ".js";
00066         }
00067         return sprintf($this->tags['javascriptlink'], $this->webroot . $this->themeWeb . JS_URL . $url);
00068     }
00069 /**
00070  * Returns a JavaScript include tag for an externally-hosted script
00071  *
00072  * @param  string $url URL to JavaScript file.
00073  * @return string
00074  * @access public
00075  */
00076     function linkOut($url) {
00077         if (strpos($url, '.js') === false && strpos($url, '?') === false) {
00078             $url .= '.js';
00079         }
00080         return sprintf($this->tags['javascriptlink'], $url);
00081     }
00082 /**
00083  * Escape carriage returns and single and double quotes for JavaScript segments.
00084  *
00085  * @param string $script string that might have javascript elements
00086  * @return string escaped string
00087  * @access public
00088  */
00089     function escapeScript($script) {
00090         $script = r(array("\r\n", "\n", "\r"), '\n', $script);
00091         $script = r(array('"', "'"), array('\"', "\\'"), $script);
00092         return $script;
00093     }
00094 /**
00095  * Escape a string to be JavaScript friendly.
00096  *
00097  * List of escaped ellements:
00098  *  + "\r\n" => '\n'
00099  *  + "\r" => '\n'
00100  *  + "\n" => '\n'
00101  *  + '"' => '\"'
00102  *  + "'" => "\\'"
00103  *
00104  * @param  string $script String that needs to get escaped.
00105  * @return string Escaped string.
00106  * @access public
00107  */
00108     function escapeString($string) {
00109         $escape = array("\r\n" => '\n', "\r" => '\n', "\n" => '\n', '"' => '\"', "'" => "\\'");
00110         return r(array_keys($escape), array_values($escape), $string);
00111     }
00112 /**
00113  * Attach an event to an element. Used with the Prototype library.
00114  *
00115  * @param string $object Object to be observed
00116  * @param string $event event to observe
00117  * @param string $observer function to call
00118  * @param boolean $useCapture default true
00119  * @return boolean true on success
00120  * @access public
00121  */
00122     function event($object, $event, $observer = null, $useCapture = false) {
00123 
00124         if ($useCapture == true) {
00125             $useCapture = "true";
00126         } else {
00127             $useCapture = "false";
00128         }
00129 
00130         if ($object == 'window' || strpos($object, '$(') !== false || strpos($object, '"') !== false || strpos($object, '\'') !== false) {
00131             $b = "Event.observe($object, '$event', function(event) { $observer }, $useCapture);";
00132         } else {
00133             $chars = array('#', ' ', ', ', '.', ':');
00134             $found = false;
00135             foreach ($chars as $char) {
00136                 if (strpos($object, $char) !== false) {
00137                     $found = true;
00138                     break;
00139                 }
00140             }
00141             if ($found) {
00142                 $this->_rules[$object] = $event;
00143             } else {
00144                 $b = "Event.observe(\$('$object'), '$event', function(event) { $observer }, $useCapture);";
00145             }
00146         }
00147 
00148         if (isset($b) && !empty($b)) {
00149             if ($this->_cacheEvents === true) {
00150                 $this->_cachedEvents[] = $b;
00151                 return;
00152             } else {
00153                 return $this->codeBlock($b);
00154             }
00155         }
00156     }
00157 /**
00158  * Cache JavaScript events created with event()
00159  *
00160  * @param boolean $file If true, code will be written to a file
00161  * @param boolean $all If true, all code written with JavascriptHelper will be sent to a file
00162  * @return void
00163  * @access public
00164  */
00165     function cacheEvents($file = false, $all = false) {
00166         $this->_cacheEvents = true;
00167         $this->_cacheToFile = $file;
00168         $this->_cacheAll = $all;
00169     }
00170 /**
00171  * Write cached JavaScript events
00172  *
00173  * @return string
00174  * @access public
00175  */
00176     function writeEvents() {
00177 
00178         $rules = array();
00179         if (!empty($this->_rules)) {
00180             foreach ($this->_rules as $sel => $event) {
00181                 $rules[] = "\t'{$sel}': function(element, event) {\n\t\t{$event}\n\t}";
00182             }
00183             $this->_cacheEvents = true;
00184         }
00185 
00186         if ($this->_cacheEvents) {
00187 
00188             $this->_cacheEvents = false;
00189             $events = $this->_cachedEvents;
00190             $data = implode("\n", $events);
00191             $this->_cachedEvents = array();
00192 
00193             if (!empty($rules)) {
00194                 $data .= "\n\nvar SelectorRules = {\n" . implode(",\n\n", $rules) . "\n}\n";
00195                 $data .= "\nEventSelectors.start(SelectorRules);\n";
00196             }
00197 
00198             if (!empty($events) || !empty($rules)) {
00199                 if ($this->_cacheToFile) {
00200                     $filename = md5($data);
00201                     if (!file_exists(JS . $filename . '.js')) {
00202                         cache(r(WWW_ROOT, '', JS) . $filename . '.js', $data, '+999 days', 'public');
00203                     }
00204                     return $this->link($filename);
00205                 } else {
00206                     return $this->codeBlock("\n" . $data . "\n");
00207                 }
00208             }
00209         }
00210     }
00211 /**
00212  * Includes the Prototype Javascript library (and anything else) inside a single script tag.
00213  *
00214  * Note: The recommended approach is to copy the contents of
00215  * javascripts into your application's
00216  * public/javascripts/ directory, and use @see javascriptIncludeTag() to
00217  * create remote script links.
00218  *
00219  * @param string $script name of script to include
00220  * @return string script with all javascript in/javascripts folder
00221  * @access public
00222  */
00223     function includeScript($script = "") {
00224         if ($script == "") {
00225             $dh = opendir(JS);
00226             while (false !== ($filename = readdir($dh))) {
00227                 $files[] = $filename;
00228             }
00229             sort($files);
00230             $javascript = '';
00231             foreach ($files as $file) {
00232                 if (substr($file, -3) == '.js') {
00233                     $javascript .= file_get_contents(JS . "{$file}") . "\n\n";
00234                 }
00235             }
00236         } else {
00237             $javascript = file_get_contents(JS . "$script.js") . "\n\n";
00238         }
00239         return $this->codeBlock("\n\n" . $javascript);
00240     }
00241 /**
00242  * Generates a JavaScript object in JavaScript Object Notation (JSON)
00243  * from an array
00244  *
00245  * @param array $data Data to be converted
00246  * @param boolean $block Wraps return value in a <script/> block if true
00247  * @param string $prefix Prepends the string to the returned data
00248  * @param string $postfix Appends the string to the returned data
00249  * @param array $stringKeys A list of array keys to be treated as a string
00250  * @param boolean $quoteKeys If false, treats $stringKey as a list of keys *not* to be quoted
00251  * @param string $q The type of quote to use
00252  * @return string A JSON code block
00253  * @access public
00254  */
00255     function object($data = array(), $block = false, $prefix = '', $postfix = '', $stringKeys = array(), $quoteKeys = true, $q = "\"") {
00256         if (is_object($data)) {
00257             $data = get_object_vars($data);
00258         }
00259 
00260         $out = array();
00261         $key = array();
00262 
00263         if (is_array($data)) {
00264             $keys = array_keys($data);
00265         }
00266 
00267         $numeric = true;
00268         if (!empty($keys)) {
00269             $numeric = (array_values($keys) === array_keys(array_values($keys)));
00270         }
00271 
00272         foreach ($data as $key => $val) {
00273             if (is_array($val) || is_object($val)) {
00274                 $val = $this->object($val, false, '', '', $stringKeys, $quoteKeys, $q);
00275             } else {
00276                 if ((!count($stringKeys) && !is_numeric($val) && !is_bool($val)) || ($quoteKeys && in_array($key, $stringKeys, true)) || (!$quoteKeys && !in_array($key, $stringKeys, true))) {
00277                     $val = $q . $this->escapeString($val) . $q;
00278                 }
00279                 if ($val === null) {
00280                     $val = 'null';
00281                 }
00282                 if (is_bool($val)) {
00283                     $val = ife($val, 'true', 'false');
00284                 }
00285             }
00286 
00287             if (!$numeric) {
00288                 $val = $q . $key . $q . ':' . $val;
00289             }
00290 
00291             $out[] = $val;
00292         }
00293 
00294         if (!$numeric) {
00295             $rt = '{' . join(', ', $out) . '}';
00296         } else {
00297             $rt = '[' . join(', ', $out) . ']';
00298         }
00299         $rt = $prefix . $rt . $postfix;
00300 
00301         if ($block) {
00302             $rt = $this->codeBlock($rt);
00303         }
00304 
00305         return $rt;
00306     }
00307 /**
00308  * AfterRender callback.  Writes any cached events to the view, or to a temp file.
00309  *
00310  * @return void
00311  * @access public
00312  */
00313     function afterRender() {
00314         echo $this->writeEvents();
00315     }
00316 }
00317 ?>