Skip to content

Instantly share code, notes, and snippets.

@felixhageloh
Created July 13, 2011 10:28
Show Gist options
  • Save felixhageloh/1080057 to your computer and use it in GitHub Desktop.
Save felixhageloh/1080057 to your computer and use it in GitHub Desktop.
Rails tooltip application helper code
def with_tooltip(tool_tip_text, options={}, &block)
content = capture(&block)
opts = tag_options(options[:html]);
uid = "tooltipjs_#{get_uid}"
js = %{
var parent = $('#{uid}').up();
var trigger = parent.getElementsByClassName('TooltipTrigger')[0] || parent;
var tooltip = parent.getElementsByClassName('Tooltip')[0];
tooltip.trigger = trigger;
}
if options[:event].to_s == "click"
show_handler = "function(e) { ttip_skip = this.trigger; showTooltip(this);}"
hide_handler = "function(e) { (ttip_skip == this.trigger) ? ttip_skip = null : this.hide() }"
js << "Event.observe(trigger, 'click', #{show_handler}.bindAsEventListener(tooltip));"
js << "Event.observe(document, 'click', #{hide_handler}.bindAsEventListener(tooltip));"
else
js << "Event.observe(trigger, 'mouseover', function() { showTooltip(this);}.bind(tooltip));"
js << "Event.observe(trigger, 'mouseout', tooltip.hide.bind(tooltip))"
end
concat("<span style='position:relative' #{opts}>", block.binding)
concat(content, block.binding)
concat("<div style='display:none' class='Tooltip'><span>#{tool_tip_text}</span></div>", block.binding)
concat(javascript_tag(js, :id => uid), block.binding)
concat("</span>", block.binding)
end
def get_uid
session[:dom_uid] ||= 0
session[:dom_uid] += 1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment