Created
April 28, 2010 14:40
-
-
Save JangoSteve/382223 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module PrototipHelper | |
# Created by Steve Schwartz [1] (for Alfa Jango, LLC [2]) for the Scriptaculous Prototip tooltip library by Nick Stakenburg [3]. | |
# | |
# [1] http://github.com/jangosteve | |
# [2] http://www.alfajango.com | |
# [3] http://www.nickstakenburg.com/projects/prototip2/ | |
# Using this in your views: | |
# <%= prototip( "target_id_to_attach_prototip", | |
# "Prototip content (may be HTML)", | |
# { | |
# :title => "Prototip Title", | |
# :close_button => true, | |
# :hide_on => {:element => 'target', :event => 'click'}, | |
# :hide_after => 1, | |
# :hide_others => true, | |
# :stem => 'left_top', | |
# :hook => { :tip => 'top_left', :target => 'right_top' } }) %> | |
# creates the following code: | |
# <script type='text/javascript'> | |
# try { | |
# new Tip( 'target_id_to_attach_prototip', | |
# 'Prototip content (may be HTML)', | |
# { hideOn: {event: 'click',element: 'target'}, | |
# stem: 'leftTop', | |
# hideAfter: 1.0, | |
# hook: {tip: 'topLeft',target: 'rightTop'}, | |
# hideOthers: true, | |
# title: 'Prototip Title', | |
# style: 'protogrey', | |
# closeButton: true }); | |
# } | |
# catch(err) {} | |
# </script> | |
def prototip(target_id,body,options={}) | |
options = default_options.merge!(options) | |
option_params = "" | |
options.each do |key, value| | |
unless value.kind_of?(Hash) && value.size > 1 | |
value = key == :title ? normalize_value(value, false) : normalize_value(value) | |
option_params += "#{key.to_s.camelize(:lower)}: #{value.to_s}," | |
else | |
option_params += "#{key.to_s.camelize(:lower)}: {" | |
value.each do |k,v| | |
v = key == :title ? normalize_value(v, false) : normalize_value(v) | |
option_params += k.to_s.camelize(:lower) + ": " + v.to_s + "," | |
end | |
option_params = option_params.chop + "}," | |
end | |
end | |
option_params.chop! | |
out = "<script type='text/javascript'>" | |
out += "try {new Tip('#{target_id}', '#{nl2br(body)}', { #{option_params} });} catch(err) {}" | |
out += "</script>" | |
end | |
def default_options | |
{ :style => 'protogrey', | |
:stem => 'top_left', | |
:hook => { :tip => 'top_left', :target => 'bottom_middle' } | |
} | |
end | |
def normalize_value(value, camel=true) | |
value = case | |
when value.is_a?(String) : "'#{camel ? value.camelize(:lower) : value}'" | |
when value.is_a?(Numeric) : sprintf("%.1f",value) | |
else value | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment