From 7ae83e5ff0a1083aecc7e39e3ca0236401f35918 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sat, 12 Mar 2005 02:42:48 +0000 Subject: Added first stab at Javascript/Ajax helpers git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@888 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../lib/action_view/helpers/javascript_helper.rb | 160 +++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 actionpack/lib/action_view/helpers/javascript_helper.rb (limited to 'actionpack/lib/action_view') diff --git a/actionpack/lib/action_view/helpers/javascript_helper.rb b/actionpack/lib/action_view/helpers/javascript_helper.rb new file mode 100644 index 0000000000..20c2a9cf63 --- /dev/null +++ b/actionpack/lib/action_view/helpers/javascript_helper.rb @@ -0,0 +1,160 @@ +require File.dirname(__FILE__) + '/tag_helper' + +# You must call <%= define_javascript_functions %> in your application before using these helpers. +module JavascriptTagHelper + 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 + + 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 form_remote_tag(options = {}) + options[:form] = true + + options[:html] ||= { } + options[:html][:onsubmit] = "#{remote_function(options)}; return false;" + + tag("form", options[:html], true) + end + + def define_javascript_functions +<<-EOF + +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 + end +end \ No newline at end of file -- cgit v1.2.3