Module: Coroutine::KennyDialoggins::Helpers

Defined in:
lib/kenny_dialoggins/helpers.rb

Instance Method Summary collapse

Instance Method Details

#hide_dialog(dialog_id) ⇒ Object

Returns a string of Javascript that will hide the dialog identified by the supplied dialog_id. As an example of useage, this method might be called as the second argument to ActionView::Helpers::JavaScriptHelper#link_to_function.



73
74
75
# File 'lib/kenny_dialoggins/helpers.rb', line 73

def hide_dialog(dialog_id)
  "KennyDialoggins.Dialog.hide('#{dialog_id.to_s}')"
end

#render_dialog(id, options = {}, &block) ⇒ Object

Returns a javascript tag containing the dialog initialization logic. The first argument to this method is the dialog’s id. The id is required and should be unique. Further options may be provided; those that are specific to the dialog are:

  • :class - a css class name that will be appended to the outermost div to facilitate multiple styles

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

  • :after_show - a Javascript function that will be invoked after the dialog has become visible

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

  • :after_hide - a Javascript function that will be invoked after the dialog 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.

Example

# Generates: # # <script type=“text/javascript”> # //<![CDATA[ # KennyDialoggins.Dialog.instances = new KennyDialoggins.Dialog(‘Hello, Foo!’, {}); # //]]> # </script> <%= render_dialog :foo_dialog, :partial => “foo” %>

In this case, a partial named “_foo.html.erb”–containing the string, “Hello, Foo!”– is rendered into the dialog.

Example

# Generates: # <script type=“text/javascript”> # //<![CDATA[ # KennyDialoggins.Dialog.instances = new KennyDialoggins.Dialog(‘Hello, Foo!’, { alert(‘bar!’) }); # //]]> # </script> <%= render_dialog :foo_dialog, :partial => “foo”, :before_show => “function() { alert(‘bar!’) }” %>

This case is similar to the previous case, except that an alert containing the string, “bar!” will appear before the dialog is shown.



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/kenny_dialoggins/helpers.rb', line 46

def render_dialog(id, options = {}, &block)
  options[:inline]    = capture(&block) if block_given?
  render_options      = extract_dialog_render_options(options)
  javascript_options  = dialog_options_to_js(extract_dialog_javascript_options(options))
  
  content = escape_javascript(render(render_options))
  
  raise "You must specify an id to register a dialog."    unless id
  raise "You must provide content to register a dialog."  unless content
  
  javascript_tag "KennyDialoggins.Dialog.instances['#{id.to_s}'] = new KennyDialoggins.Dialog('#{content}', #{javascript_options});"
end

#show_dialog(dialog_id) ⇒ Object

Returns a string of Javascript that will show the dialog identified by the supplied dialog_id. As an example of useage, this method might be called as the second argument to ActionView::Helpers::JavaScriptHelper#link_to_function.



64
65
66
# File 'lib/kenny_dialoggins/helpers.rb', line 64

def show_dialog(dialog_id)
  "KennyDialoggins.Dialog.show('#{dialog_id.to_s}')"
end