Module: Coroutine::MichaelHintbuble::Helpers

Defined in:
lib/michael_hintbuble/helpers.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#hide_bubble(target_id) ⇒ Object

This method returns a Javascript string that will hide the bubble attached to the supplied target id.



96
97
98
# File 'lib/michael_hintbuble/helpers.rb', line 96

def hide_bubble(target_id)
  "MichaelHintbuble.Bubble.hide('#{target_id}');"
end

#render_bubble(target_id, options = {}, &block) ⇒ Object

This method returns a javascript tag containing the hint bubble initialization logic. The first argument to this method is target_id, the id of the html element to which the hint bubble is anchored.

The target_id is required and should be unique (duh). Further options may be provided; those that are specific to the hint bubble are:

  • :class - the css style to assign to the outermost div container (defaults to “michael_hintbuble_bubble”)

  • :position - css-style value that specifies the hint bubble’s relative position, e.g., top, bottom, right, or left (defaults to right)

  • :event_names - an array of strings specifying the events that should trigger the display of the hint bubble

  • :before_show - a Javascript function that will be invoked before the hint bubble is shown

  • :after_show - a Javascript function that will be invoked after the hint bubble has been shown

  • :before_hide - a Javascript function that will be invoked before the hint bubble is hidden

  • :after_hide - a Javascript function that will be invoked after the hint bubble has been hidden

  • &block - HTML markup that will be automatically converted to render’s inline option

All remaining options are the same as the options available to ActionController::Base#render. Please see the documentation for ActionController::Base#render for further details.

Events

The library manages repositioning the hint bubble in response to window resizing and scrolling events automatically. You do not need to specify resize or scroll in the optional event names array.

The library defaults to trapping mouse gestures, but it is capable of trapping focus events in addition to or in place of mouse events. When specifying events, you need only reference the positive action: the library can infer the corresponding action to toggle off the tooltip.

Valid entries for the events array are any combination of the following options:

  • "focus"

  • "mouseover"

Example

# Generates: # # <script type=“text/javascript”> # //<![CDATA[ # MichaelHintbuble.Bubble.instances = new MichaelHintbuble.Bubble(“What up, foo?”, { “event_names”: [“mouseover”], “position”: “top center” }); # //]]> # </script> <%= render_bubble :foo_target_id, :content => “What up, foo?”, :position => “top center” %>

In this case, a simple hint bubble is produced with the specified text content. The bubble responds to mouseover/mouseout events and centers itself above the target element when shown.

Example

# Generates: # # <script type=“text/javascript”> # //<![CDATA[ # MichaelHintbuble.Bubble.instances = new MichaelHintbuble.Bubble(“<ul><li>Item 1</li><li>Item 2</li></ul>”, { “event_names”: [“focus”], “position”: “center left” }); # //]]> # </script> <%= render_bubble :bar_target_id, :event_names => [“focus”], :position => “center left” %>

<ul>
  <li>Item 1</li>
  <li>Item 2</li>
</ul>

<% end %>

In this case, a slightly more complex hint bubble is produced with the specified markup. The bubble responds to focus/blur events and positions itself to the left of the target.



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/michael_hintbuble/helpers.rb', line 71

def render_bubble(target_id, options = {}, &block)
  options[:inline]    = capture(&block) if block_given?
  render_options      = extract_bubble_render_options(options)
  javascript_options  = bubble_options_to_js(extract_bubble_javascript_options(options))
  
  content = escape_javascript(render(render_options))
  
  raise "You gotta specify a target id to register a hint bubble, baby."  unless target_id
  raise "You gotta provide content to register a hint bubble, baby."      unless content
  
  javascript_tag "Event.observe(window, 'load', function() { MichaelHintbuble.Bubble.instances['#{target_id}'] = new MichaelHintbuble.Bubble('#{target_id}', '#{content}', #{javascript_options}) });"
end

#show_bubble(target_id) ⇒ Object

This method returns a Javascript string that will show the bubble attached to the supplied target id.



88
89
90
# File 'lib/michael_hintbuble/helpers.rb', line 88

def show_bubble(target_id)
  "MichaelHintbuble.Bubble.show('#{target_id}');"
end