diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2005-03-12 02:59:12 +0000 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2005-03-12 02:59:12 +0000 |
commit | ebb070e0786994ff96ec08507b169014110740d3 (patch) | |
tree | efca6c9c61d293eb009cca80c2b313c059f7aec1 /actionpack/lib/action_view | |
parent | 7d801ae1c88dc2c29302de9a5feeb86ac5391dae (diff) | |
download | rails-ebb070e0786994ff96ec08507b169014110740d3.tar.gz rails-ebb070e0786994ff96ec08507b169014110740d3.tar.bz2 rails-ebb070e0786994ff96ec08507b169014110740d3.zip |
Put it in the right module
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@890 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'actionpack/lib/action_view')
-rw-r--r-- | actionpack/lib/action_view/helpers/javascript_helper.rb | 264 |
1 files changed, 134 insertions, 130 deletions
diff --git a/actionpack/lib/action_view/helpers/javascript_helper.rb b/actionpack/lib/action_view/helpers/javascript_helper.rb index 078935af3f..7e85519345 100644 --- a/actionpack/lib/action_view/helpers/javascript_helper.rb +++ b/actionpack/lib/action_view/helpers/javascript_helper.rb @@ -1,160 +1,164 @@ require File.dirname(__FILE__) + '/tag_helper' -# You must call <%= define_javascript_functions %> in your application before using these helpers. -module JavascriptHelper - def link_to_display_toggle(name, tags, html_options = {}) - toggle_functions = [ tags ].flatten.collect { |tag| "toggle_display_by_id('#{tag}'); " }.join - content_tag( - "a", name, - html_options.symbolize_keys.merge(:href => "#", :onclick => "#{toggle_functions}; #{html_options['onclick']}; return false;") - ) - end +module ActionView + module Helpers + # You must call <%= define_javascript_functions %> in your application before using these helpers. + module JavascriptHelper + def link_to_toggle_display(name, tags, html_options = {}) + toggle_functions = [ tags ].flatten.collect { |tag| "toggle_display_by_id('#{tag}'); " }.join + content_tag( + "a", name, + html_options.symbolize_keys.merge(:href => "#", :onclick => "#{toggle_functions}; #{html_options['onclick']}; return false;") + ) + end - def link_to_function(name, function, html_options = {}) - content_tag( - "a", name, - html_options.symbolize_keys.merge(:href => "#", :onclick => "#{function}; return false;") - ) - end + def link_to_function(name, function, html_options = {}) + content_tag( + "a", name, + html_options.symbolize_keys.merge(:href => "#", :onclick => "#{function}; return false;") + ) + end - def link_to_remote(name, options = {}) - link_to_function(name, remote_function(options)) - end + def link_to_remote(name, options = {}) + link_to_function(name, remote_function(options)) + end - def form_remote_tag(options = {}) - options[:form] = true + def form_remote_tag(options = {}) + options[:form] = true - options[:html] ||= { } - options[:html][:onsubmit] = "#{remote_function(options)}; return false;" + options[:html] ||= { } + options[:html][:onsubmit] = "#{remote_function(options)}; return false;" - tag("form", options[:html], true) - end + tag("form", options[:html], true) + end + + def define_javascript_functions + <<-EOF + <script language="JavaScript"> + /* XMLHttpRequest Methods */ - def define_javascript_functions -<<-EOF -<script language="JavaScript"> -/* XMLHttpRequest Methods */ - -function update_with_response() { - o(arguments[0]).innerHTML = xml_request(arguments[1], arguments[2]); -} - -function xml_request() { - var url = arguments[0]; - var parameters = arguments[1]; - var async = arguments[2]; - var type = parameters ? "POST" : "GET"; - - req = xml_http_request_object(); - req.open(type, url, async); - req.send(parameters); - - return req.responseText; -} - -function xml_http_request_object() { - var req = false; - try { - req = new ActiveXObject("Msxml2.XMLHTTP"); - } catch (e) { - try { - req = new ActiveXObject("Microsoft.XMLHTTP"); - } catch (E) { - req = false; + function update_with_response() { + o(arguments[0]).innerHTML = xml_request(arguments[1], arguments[2]); } - } - if (!req && typeof XMLHttpRequest!='undefined') { - req = new XMLHttpRequest(); - } + function xml_request() { + var url = arguments[0]; + var parameters = arguments[1]; + var async = arguments[2]; + var type = parameters ? "POST" : "GET"; + + req = xml_http_request_object(); + req.open(type, url, async); + req.send(parameters); - return req; -} + return req.responseText; + } + function xml_http_request_object() { + var req = false; + try { + req = new ActiveXObject("Msxml2.XMLHTTP"); + } catch (e) { + try { + req = new ActiveXObject("Microsoft.XMLHTTP"); + } catch (E) { + req = false; + } + } -/* Common methods ------------------------------ */ + if (!req && typeof XMLHttpRequest!='undefined') { + req = new XMLHttpRequest(); + } -function toggle_display_by_id(id) { - o(id).style.display = (o(id).style.display == "none") ? "" : "none"; -} + return req; + } -function o(id) { - return document.getElementById(id); -} + /* Common methods ------------------------------ */ -/* Serialize a form by Sam Stephenson ------------------------------ */ + function toggle_display_by_id(id) { + o(id).style.display = (o(id).style.display == "none") ? "" : "none"; + } -Form = { - Serializers: { - input: function(element) { - switch (element.type.toLowerCase()) { - case 'hidden': - case 'text': - return Form.Serializers.textarea(element); - case 'checkbox': - case 'radio': - return Form.Serializers.inputSelector(element); - } - }, + function o(id) { + return document.getElementById(id); + } + + + /* Serialize a form by Sam Stephenson ------------------------------ */ + + Form = { + Serializers: { + input: function(element) { + switch (element.type.toLowerCase()) { + case 'hidden': + case 'text': + return Form.Serializers.textarea(element); + case 'checkbox': + case 'radio': + return Form.Serializers.inputSelector(element); + } + }, - inputSelector: function(element) { - if (element.checked) - return [element.name, element.value]; - }, + inputSelector: function(element) { + if (element.checked) + return [element.name, element.value]; + }, - textarea: function(element) { - return [element.name, element.value]; - }, + textarea: function(element) { + return [element.name, element.value]; + }, - select: function(element) { - var index = element.selectedIndex; - return [element.name, element.options[index].value]; - } - }, + select: function(element) { + var index = element.selectedIndex; + return [element.name, element.options[index].value]; + } + }, - serialize: function(form) { - var elements = Form.getFormElements(form); - var queryComponents = new Array(); + serialize: function(form) { + var elements = Form.getFormElements(form); + var queryComponents = new Array(); - for (var i = 0; i < elements.length; i++) { - var element = elements[i]; - var method = element.tagName.toLowerCase(); + for (var i = 0; i < elements.length; i++) { + var element = elements[i]; + var method = element.tagName.toLowerCase(); - var parameter = Form.Serializers[method](element); - if (parameter) { - var queryComponent = encodeURIComponent(parameter[0]) + '=' + - encodeURIComponent(parameter[1]); - queryComponents.push(queryComponent); - } - } + var parameter = Form.Serializers[method](element); + if (parameter) { + var queryComponent = encodeURIComponent(parameter[0]) + '=' + + encodeURIComponent(parameter[1]); + queryComponents.push(queryComponent); + } + } - return queryComponents.join('&'); - }, + return queryComponents.join('&'); + }, - getFormElements: function(form) { - var elements = new Array(); - for (tagName in Form.Serializers) { - var tagElements = form.getElementsByTagName(tagName); - for (var j = 0; j < tagElements.length; j++) - elements.push(tagElements[j]); + getFormElements: function(form) { + var elements = new Array(); + for (tagName in Form.Serializers) { + var tagElements = form.getElementsByTagName(tagName); + for (var j = 0; j < tagElements.length; j++) + elements.push(tagElements[j]); + } + return elements; + } } - return elements; - } -} -</script> -EOF - end - - private - def remote_function(options) - function = options[:update] ? - "update_with_response('#{options[:update]}', '#{url_for(options[:url])}'#{', Form.serialize(this)' if options[:form]})" : - "xml_request('#{url_for(options[:url])}'#{', Form.serialize(this)' if options[:form]})" - - function = "#{options[:before]};#{function}" if options[:before] - function = "#{function};#{options[:after]}" if options[:after] + </script> + EOF + end + + private + def remote_function(options) + function = options[:update] ? + "update_with_response('#{options[:update]}', '#{url_for(options[:url])}'#{', Form.serialize(this)' if options[:form]})" : + "xml_request('#{url_for(options[:url])}'#{', Form.serialize(this)' if options[:form]})" + + function = "#{options[:before]};#{function}" if options[:before] + function = "#{function};#{options[:after]}" if options[:after] - return function + return function + end end + end end
\ No newline at end of file |