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 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