| Module | ActionView::Helpers::ActionScriptHelper |
| In: |
lib/actionscript_helper.rb
|
Provides functionality for working with ActionScript in your views.
| ACTIONSCRIPT_PATH | = | File.join(File.dirname(__FILE__), 'as') |
Returns a JavaScript tag with the content inside. Example:
javascript_tag "alert('All is good')"
Returns:
<script type="text/javascript">
//<![CDATA[
alert('All is good')
//]]>
</script>
html_options may be a hash of attributes for the <script> tag. Example:
javascript_tag "alert('All is good')", :defer => 'true' # => <script defer="true" type="text/javascript">alert('All is good')</script>
# File lib/actionscript_helper.rb, line 57
57: def actionscript_tag(content, html_options = {})
58: #content_tag("script", javascript_cdata_section(content), html_options.merge(:type => "text/javascript"))
59: end
Includes the Action Pack JavaScript libraries inside a single <script> tag. The function first includes prototype.js and then its core extensions, (determined by filenames starting with "prototype"). Afterwards, any additional scripts will be included in undefined order.
Note: The recommended approach is to copy the contents of lib/action_view/helpers/javascripts/ into your application’s public/javascripts/ directory, and use javascript_include_tag to create remote <script> links.
# File lib/actionscript_helper.rb, line 23
23: def define_javascript_functions
24: javascript = '<script type="text/javascript">'
25:
26: # load prototype.js and its extensions first
27: prototype_libs = Dir.glob(File.join(JAVASCRIPT_PATH, 'prototype*')).sort.reverse
28: prototype_libs.each do |filename|
29: javascript << "\n" << IO.read(filename)
30: end
31:
32: # load other librairies
33: (Dir.glob(File.join(JAVASCRIPT_PATH, '*')) - prototype_libs).each do |filename|
34: javascript << "\n" << IO.read(filename)
35: end
36: javascript << '</script>'
37: end
Escape carrier returns and single and double quotes for JavaScript segments.
# File lib/actionscript_helper.rb, line 40
40: def escape_actionscript(javascript)
41: (javascript || '').gsub('\\','\0\0').gsub(/\r\n|\n|\r/, "\\n").gsub(/["']/) { |m| "\\#{m}" }
42: end
# File lib/actionscript_helper.rb, line 70
70: def array_or_string_for_actionscript(option)
71: as_option = if option.kind_of?(Array)
72: "['#{option.join('\',\'')}']"
73: elsif !option.nil?
74: "'#{option}'"
75: end
76: as_option
77: end