Module: Coroutine::KennyDialoggins::Helpers
- Defined in:
- lib/kenny_dialoggins/helpers.rb
Instance Method Summary collapse
-
#hide_dialog(dialog_id) ⇒ Object
Returns a string of Javascript that will hide the dialog identified by the supplied dialog_id.
-
#render_dialog(id, options = {}, &block) ⇒ Object
Returns a javascript tag containing the dialog initialization logic.
-
#show_dialog(dialog_id) ⇒ Object
Returns a string of Javascript that will show the dialog identified by the supplied dialog_id.
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.
79 80 81 |
# File 'lib/kenny_dialoggins/helpers.rb', line 79 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:
-
:id
- an element id for the generated dialog. If not provided, the value provided as the first argument to render_dialog will be used. -
: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!’, ‘id’:‘foo_dialog’); # //]]> # </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!’, ‘beforeShow’:function() { 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.
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/kenny_dialoggins/helpers.rb', line 47 def render_dialog(id, = {}, &block) [:inline] = capture(&block) if block_given? = () = () # In the event an id is not provided in the options, we'll use the # dialog id. [:id] ||= id = () content = escape_javascript(render()) 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 "Event.observe(window, 'load', function() { KennyDialoggins.Dialog.instances['#{id.to_s}'] = new KennyDialoggins.Dialog('#{content}', #{}) });" 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.
70 71 72 |
# File 'lib/kenny_dialoggins/helpers.rb', line 70 def show_dialog(dialog_id) "KennyDialoggins.Dialog.show('#{dialog_id.to_s}')" end |