From cf1ed9a650e8d284333d037dd9179227016be625 Mon Sep 17 00:00:00 2001 From: gautamc Date: Sat, 23 May 2009 16:44:27 +0530 Subject: [PATCH] modification to allow us to set the type of the submit field to either image or button based on the options hash. --- .../lib/action_view/helpers/javascript_helper.rb | 13 +++++++++- .../lib/action_view/helpers/prototype_helper.rb | 14 +++++++++++ actionpack/test/template/javascript_helper_test.rb | 25 ++++++++++++++++++++ actionpack/test/template/prototype_helper_test.rb | 12 +++++++++ 4 files changed, 63 insertions(+), 1 deletions(-) diff --git a/actionpack/lib/action_view/helpers/javascript_helper.rb b/actionpack/lib/action_view/helpers/javascript_helper.rb index 8f64acf..5675448 100644 --- a/actionpack/lib/action_view/helpers/javascript_helper.rb +++ b/actionpack/lib/action_view/helpers/javascript_helper.rb @@ -107,6 +107,15 @@ module ActionView # (instead of making an Ajax request first). # # The +html_options+ will accept a hash of html attributes for the link tag. Some examples are :class => "nav_button", :id => "articles_nav_button" + # To set the input type to an image(instead of the default 'button'), you set :type and :src. + # Example: + # button_to_function( + # 'update_resource', + # 'return false', + # :onclick => "alert('hello world!')", + # :type => 'image', + # :src => "/images/button.gif" + # ) # # Note: if you choose to specify the javascript function in a block, but would like to pass html_options, set the +function+ parameter to nil # @@ -125,7 +134,9 @@ module ActionView function = block_given? ? update_page(&block) : args[0] || '' onclick = "#{"#{html_options[:onclick]}; " if html_options[:onclick]}#{function};" - tag(:input, html_options.merge(:type => 'button', :value => name, :onclick => onclick)) + html_options[:type] = 'button' if( !html_options[:type] || html_options[:type] !~ /^image|button$/i ) + + tag(:input, html_options.merge(:value => name, :onclick => onclick)) end JS_ESCAPE_MAP = { diff --git a/actionpack/lib/action_view/helpers/prototype_helper.rb b/actionpack/lib/action_view/helpers/prototype_helper.rb index c0f5df3..489c41a 100644 --- a/actionpack/lib/action_view/helpers/prototype_helper.rb +++ b/actionpack/lib/action_view/helpers/prototype_helper.rb @@ -419,6 +419,20 @@ module ActionView # :update => { :success => "succeed", :failure => "fail" } # # options argument is the same as in form_remote_tag. + # In addtion, you can choose to set the type of the submit field to either of button or image. + # Example: + # # Generates: + # submit_to_remote( + # 'update_resource', + # 'Update Resource', + # :url => { :action => 'update' }, + # :before => "$('loading').style.display = 'block'", + # :after => "return false", + # :html => { :src => '/images/button.gif', :type => 'image' } + # ) def submit_to_remote(name, value, options = {}) options[:with] ||= 'Form.serialize(this.form)' diff --git a/actionpack/test/template/javascript_helper_test.rb b/actionpack/test/template/javascript_helper_test.rb index 8caabfc..7ecb234 100644 --- a/actionpack/test/template/javascript_helper_test.rb +++ b/actionpack/test/template/javascript_helper_test.rb @@ -79,6 +79,31 @@ class JavaScriptHelperTest < ActionView::TestCase button_to_function("Greeting") end + def test_button_to_function_with_type_as_image + html = button_to_function( + 'update_resource', + 'return false', + :onclick => "alert('hello world!')", + :type => 'image', + :src => "/images/button.gif" + ) + + assert_dom_equal "", html + end + + def test_button_to_function_with_type_as_image_and_rjs_block + html = button_to_function( + 'update_resource', + :onclick => "alert('hello world!')", + :type => 'image', + :src => "/images/button.gif" + ) do |page| + page.replace_html 'header', "

Greetings

" + end + + assert_dom_equal "", html + end + def test_javascript_tag self.output_buffer = 'foo' diff --git a/actionpack/test/template/prototype_helper_test.rb b/actionpack/test/template/prototype_helper_test.rb index f9f418a..bb57c15 100644 --- a/actionpack/test/template/prototype_helper_test.rb +++ b/actionpack/test/template/prototype_helper_test.rb @@ -226,6 +226,18 @@ class PrototypeHelperTest < PrototypeHelperBaseTest submit_to_remote("More beer!", 1_000_000, :update => "empty_bottle") end + def test_submit_to_remote_with_type_as_image + html = submit_to_remote( + 'update_resource', + 'Update Resource', + :url => { :action => 'update' }, + :before => "$('loading').style.display = 'block'", + :after => "return false", + :html => { :src => '/images/button.gif', :type => 'image' } + ) + assert_dom_equal %(), html + end + def test_observe_field assert_dom_equal %(), observe_field("glass", :frequency => 5.minutes, :url => { :action => "reorder_if_empty" }) -- 1.5.6.3