From 6b018e3d08c52417034cd648952668192327a9b7 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Wed, 7 Nov 2007 15:37:06 +0000 Subject: Added :mouseover short-cut to AssetTagHelper#image_tag for doing easy image swaps (closes #6893) [joost] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8110 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../lib/action_view/helpers/asset_tag_helper.rb | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'actionpack/lib/action_view/helpers') diff --git a/actionpack/lib/action_view/helpers/asset_tag_helper.rb b/actionpack/lib/action_view/helpers/asset_tag_helper.rb index f0229980b0..47bbe2f995 100644 --- a/actionpack/lib/action_view/helpers/asset_tag_helper.rb +++ b/actionpack/lib/action_view/helpers/asset_tag_helper.rb @@ -335,13 +335,16 @@ module ActionView # # ==== Options # You can add HTML attributes using the +options+. The +options+ supports - # two additional keys for convienence and conformance: + # three additional keys for convienence and conformance: # # * :alt - If no alt text is given, the file name part of the # +source+ is used (capitalized and without the extension) # * :size - Supplied as "{Width}x{Height}", so "30x45" becomes # width="30" and height="45". :size will be ignored if the # value is not in the correct format. + # * :mouseover - Set an alternate image to be used when the onmouseover + # event is fired, and sets the original image to be replaced onmouseout. + # This can be used to implement an easy image toggle that fires on onmouseover. # # ==== Examples # image_tag("icon") # => @@ -356,15 +359,23 @@ module ActionView # Icon # image_tag("/icons/icon.gif", :class => "menu_icon") # => # Icon + # image_tag("mouse.png", :mouseover => "/images/mouse_over.png") # => + # Mouse + # image_tag("mouse.png", :mouseover => image_path("mouse_over.png")) # => + # Mouse def image_tag(source, options = {}) options.symbolize_keys! options[:src] = path_to_image(source) options[:alt] ||= File.basename(options[:src], '.*').split('.').first.capitalize - if options[:size] - options[:width], options[:height] = options[:size].split("x") if options[:size] =~ %r{^\d+x\d+$} - options.delete(:size) + if size = options.delete(:size) + options[:width], options[:height] = size.split("x") if size =~ %r{^\d+x\d+$} + end + + if mouseover = options.delete(:mouseover) + options[:onmouseover] = "this.src='#{image_path(mouseover)}'" + options[:onmouseout] = "this.src='#{image_path(options[:src])}'" end tag("img", options) -- cgit v1.2.3