[ Index ]

PHP Cross Reference of Akelos Framework

title

Body

[close]

/AkActionView/helpers/ -> javascript_helper.php (source)

   1  <?php
   2  /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
   3  
   4  // +----------------------------------------------------------------------+
   5  // | Akelos Framework - http://www.akelos.org                             |
   6  // +----------------------------------------------------------------------+
   7  // | Copyright (c) 2002-2006, Akelos Media, S.L.  & Bermi Ferrer Martinez |
   8  // | Released under the GNU Lesser General Public License, see LICENSE.txt|
   9  // +----------------------------------------------------------------------+
  10  
  11  /**
  12   * @package ActionView
  13   * @subpackage Helpers
  14   * @author Bermi Ferrer <bermi a.t akelos c.om>
  15   * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
  16   * @license GNU Lesser General Public License <http://www.gnu.org/copyleft/lesser.html>
  17   */
  18  
  19  
  20  require_once (AK_LIB_DIR.DS.'AkActionView'.DS.'AkActionViewHelper.php');
  21  require_once (AK_LIB_DIR.DS.'AkActionView'.DS.'helpers'.DS.'tag_helper.php');
  22  
  23  /**
  24  * Provides functionality for working with JavaScript in your views.
  25  * 
  26  * == Ajax, controls and visual effects
  27  * 
  28  * * For information on using Ajax, see 
  29  *   AkActionView/helpers/prototype_helper.php
  30  * * For information on using controls and visual effects, see
  31  *   AkActionView/helpers/scriptaculous_helper.php
  32  *
  33  * == Including the JavaScript libraries into your pages
  34  *
  35  * Akelos Framework includes the Prototype JavaScript framework and the Scriptaculous
  36  * JavaScript controls and visual effects library.  If you wish to use
  37  * these libraries and their helpers (ActionView::Helpers::PrototypeHelper
  38  * and ActionView::Helpers::ScriptaculousHelper), you must do one of the
  39  * following:
  40  *
  41  * * Use <tt><?= $asset->javascript_include_tag('defaults') ?></tt> in the HEAD 
  42  *   section of your page (recommended): This function will return 
  43  *   references to the JavaScript files in your <tt>public/javascripts</tt> directory. 
  44  *   Using it is recommended as the browser can then cache the libraries instead of 
  45  *   fetching all the functions anew on every request.
  46  * * Use <tt><?= $asset->javascript_include_tag('prototype') ?></tt>: As above, but 
  47  *   will only include the Prototype core library, which means you are able
  48  *   to use all basic AJAX functionality. For the Scriptaculous-based 
  49  *   JavaScript helpers, like visual effects, autocompletion, drag and drop 
  50  *   and so on, you should use the method described above.
  51  *
  52  * For documentation on +javascript_include_tag+ see 
  53  * AkActionView/helpers/asset_tag_helpers.php
  54  */
  55  
  56  defined('AK_JAVASCRIPT_PATH') ? null : define('AK_JAVASCRIPT_PATH', AK_PUBLIC_DIR.DS.'javascripts');
  57  
  58  class JavascriptHelper extends AkActionViewHelper 
  59  {
  60  
  61      /**
  62      * Returns a link that'll trigger a JavaScript +function+ using the 
  63      * onclick handler and return false after the fact.
  64      *
  65      * Examples:
  66      *   $javascript_helper->link_to_function("Greeting", "alert('Hello world!')");
  67      *   $javascript_helper->link_to_function($tag->image_tag("delete"), "if confirm('Really?'){ do_delete(); }");
  68      */
  69      function link_to_function($name, $function, $html_options = array())
  70      {
  71          $default_html_options = array(
  72          'href'    => '#',
  73          'onclick' => (!empty($html_options['onclick']) ? "{$html_options['onclick']}; " : ""). "{$function}; return false;"
  74          );
  75  
  76          $html_options = array_merge($default_html_options, $html_options);
  77          
  78          return TagHelper::content_tag('a',$name, $html_options);
  79      }
  80  
  81      /**
  82      * Returns a link that'll trigger a JavaScript +function+ using the 
  83      * onclick handler.
  84      *
  85      * Examples:
  86      *   $javascript_helper->button_to_function("Greeting", "alert('Hello world!')");
  87      *   $javascript_helper->button_to_function("Delete", "if confirm('Really?'){ do_delete(); }"));
  88      */
  89      function button_to_function($name, $function, $html_options = array())
  90      {
  91          $default_html_options = array(
  92          'type'  => 'button',
  93          'value' => $name,
  94          'onclick' => (!empty($html_options['onclick']) ?  "{$html_options['onclick']}; " : ""). "{$function};"
  95          );
  96  
  97          $html_options = array_merge($default_html_options, $html_options);
  98  
  99          return TagHelper::tag('input', $html_options);
 100      }
 101  
 102      /**
 103      * Escape carrier returns and single and double quotes for JavaScript segments.
 104      */
 105      function escape_javascript($javascript)
 106      {
 107          return preg_replace(array('/\r\n|\n|\r/',"/[\"']/"), array('\\n','\\\$0}'), $javascript);
 108      }
 109  
 110      /**
 111      * Returns a JavaScript tag with the +content+ inside. Example:
 112      *   javascript_tag("alert('All is good')") => <script type="text/javascript">alert('All is good')</script>
 113      */
 114      function javascript_tag($content)
 115      {
 116          return TagHelper::content_tag("script", JavascriptHelper::javascript_cdata_section($content), array('type' => 'text/javascript'));
 117      }
 118  
 119      function javascript_cdata_section($content)
 120      {
 121          return "\n//<![CDATA[\n".$content."\n//]]>\n";
 122      }
 123  
 124      function _options_for_javascript($options)
 125      {
 126          $_js_options = array();
 127          foreach ($options as $k=>$v){
 128              $_js_options[] = "$k:$v";
 129          }
 130          sort($_js_options);
 131          return '{'.join(', ',$_js_options).'}';
 132  
 133      }
 134  
 135      function _array_or_string_for_javascript($option)
 136      {
 137          return is_array($option) ? "['".join("', '",$option)."']" : "'".$option."'";
 138      }
 139  
 140  
 141      /**
 142      * Includes the Action Pack JavaScript libraries inside a single <script> 
 143      * tag. The function first includes prototype.js and then its core extensions,
 144      * (determined by filenames starting with "prototype").
 145      * Afterwards, any additional scripts will be included in undefined order.
 146      *
 147      * Note: The recommended approach is to copy the contents of
 148      * lib/action_view/helpers/javascripts/ into your application's
 149      * public/javascripts/ directory, and use +javascript_include_tag+ to 
 150      * create 
 151      * remote <script> links.
 152      */
 153      function define_javascript_functions()
 154      {
 155          die('This function is not recomended. Please use $asset->javascript_include_tag() instead');
 156      }
 157  
 158  
 159  
 160  }
 161  ?>
', [['AkActionView/helpers','javascript_helper.php',110]], 9], '_array_or_string_for_javascript': ['_array_or_string_for_javascript', '', [['AkActionView/helpers','javascript_helper.php',135]], 4], 'define_javascript_functions': ['define_javascript_functions', 'Includes the Action Pack JavaScript libraries inside a single


Generated: Mon Oct 27 12:43:49 2008 Cross-referenced by PHPXref 0.6