2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
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 // +----------------------------------------------------------------------+
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>
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');
24 * Provides functionality for working with JavaScript in your views.
26 * == Ajax, controls and visual effects
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
33 * == Including the JavaScript libraries into your pages
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
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.
52 * For documentation on +javascript_include_tag+ see
53 * AkActionView/helpers/asset_tag_helpers.php
56 defined('AK_JAVASCRIPT_PATH') ?
null : define('AK_JAVASCRIPT_PATH', AK_PUBLIC_DIR
.DS
.'javascripts');
58 class JavascriptHelper
extends AkActionViewHelper
62 * Returns a link that'll trigger a JavaScript +function+ using the
63 * onclick handler and return false after the fact.
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(); }");
69 function link_to_function($name, $function, $html_options = array())
71 $default_html_options = array(
73 'onclick' => (!empty($html_options['onclick']) ?
"{$html_options['onclick']}; " : ""). "{$function}; return false;"
76 $html_options = array_merge($default_html_options, $html_options);
78 return TagHelper
::content_tag('a',$name, $html_options);
82 * Returns a link that'll trigger a JavaScript +function+ using the
86 * $javascript_helper->button_to_function("Greeting", "alert('Hello world!')");
87 * $javascript_helper->button_to_function("Delete", "if confirm('Really?'){ do_delete(); }"));
89 function button_to_function($name, $function, $html_options = array())
91 $default_html_options = array(
94 'onclick' => (!empty($html_options['onclick']) ?
"{$html_options['onclick']}; " : ""). "{$function};"
97 $html_options = array_merge($default_html_options, $html_options);
99 return TagHelper
::tag('input', $html_options);
103 * Escape carrier returns and single and double quotes for JavaScript segments.
105 function escape_javascript($javascript)
107 return preg_replace(array('/\r\n|\n|\r/',"/[\"']/"), array('\\n','\\\${0}'), $javascript);
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>
114 function javascript_tag($content)
116 return TagHelper
::content_tag("script", JavascriptHelper
::javascript_cdata_section($content), array('type' => 'text/javascript'));
119 function javascript_cdata_section($content)
121 return "\n//<![CDATA[\n".$content."\n//]]>\n";
124 function _options_for_javascript($options)
126 $_js_options = array();
127 foreach ($options as $k=>$v){
128 $_js_options[] = "$k:$v";
131 return '{'.join(', ',$_js_options).'}';
135 function _array_or_string_for_javascript($option)
137 return is_array($option) ?
"['".join("', '",$option)."']" : "'".$option."'";
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.
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
151 * remote <script> links.
153 function define_javascript_functions()
155 die('This function is not recomended. Please use $asset->javascript_include_tag() instead');