Class: JavaScriptGenerator
- Inherits:
-
Object
- Object
- JavaScriptGenerator
- Defined in:
- lib/bivouac/helpers/view/goh/javascript.rb
Instance Method Summary collapse
-
#<<(javascript) ⇒ Object
Writes raw JavaScript to the page.
-
#[](id) ⇒ Object
Returns a element reference by finding it through
id
in the DOM. -
#alert(message) ⇒ Object
Displays an alert dialog with the given
message
. -
#assign(variable, value) ⇒ Object
Assigns the JavaScript
variable
the givenvalue
. -
#call(function, *arguments) ⇒ Object
Calls the JavaScript
function
, optionally with the givenarguments
. -
#delay(seconds = 1) ⇒ Object
Executes the content of the block after a delay of
seconds
. -
#draggable(id, options = {}) ⇒ Object
Creates a script.aculo.us draggable element.
-
#drop_receiving(id, options = {}) ⇒ Object
Creates a script.aculo.us drop receiving element.
-
#hide(*ids) ⇒ Object
Hides the visible DOM elements with the given
ids
. -
#initialize(context) {|_self| ... } ⇒ JavaScriptGenerator
constructor
:nodoc:.
-
#insert_html(position, id, data) ⇒ Object
Inserts HTML at the specified
position
relative to the DOM element identified by the givenid
. -
#redirect_to(location) ⇒ Object
Redirects the browser to the given
location
. -
#remove(*ids) ⇒ Object
Removes the DOM elements with the given ids from the page.
-
#replace(id, data) ⇒ Object
Replaces the “outer HTML” (i.e., the entire element, not just its contents) of the DOM element with the given
id
. -
#replace_html(id, data) ⇒ Object
Replaces the inner HTML of the DOM element with the given
id
. -
#select(pattern) ⇒ Object
Returns a collection reference by finding it through a CSS
pattern
in the DOM. -
#show(*ids) ⇒ Object
Shows hidden DOM elements with the given
ids
. -
#sortable(id, options = {}) ⇒ Object
Creates a script.aculo.us sortable element.
-
#to_s ⇒ Object
:nodoc:.
-
#toggle(*ids) ⇒ Object
Toggles the visibility of the DOM elements with the given
ids
. -
#unsortable(id) ⇒ Object
Creates a script.aculo.us unsortable element.
-
#visual_effect(name, element_id = false, js_options = {}) ⇒ Object
Starts a script.aculo.us visual effect.
Constructor Details
#initialize(context) {|_self| ... } ⇒ JavaScriptGenerator
:nodoc:
5 6 7 8 9 |
# File 'lib/bivouac/helpers/view/goh/javascript.rb', line 5 def initialize( context, &block ) #:nodoc: @context = context @source = "" yield( self ) end |
Instance Method Details
#<<(javascript) ⇒ Object
Writes raw JavaScript to the page.
54 55 56 |
# File 'lib/bivouac/helpers/view/goh/javascript.rb', line 54 def <<(javascript) @source << javascript end |
#[](id) ⇒ Object
Returns a element reference by finding it through id
in the DOM. This element can then be used for further method calls. Examples:
page['blank_slate'] # => $('blank_slate');
page['blank_slate'].show # => $('blank_slate').show();
page['blank_slate'].show('first').up # => $('blank_slate').show('first').up();
24 25 26 |
# File 'lib/bivouac/helpers/view/goh/javascript.rb', line 24 def []( id ) # JavaScriptElementProxy.new(self, id) end |
#alert(message) ⇒ Object
Displays an alert dialog with the given message
.
59 60 61 |
# File 'lib/bivouac/helpers/view/goh/javascript.rb', line 59 def alert() call( "alert", ) end |
#assign(variable, value) ⇒ Object
Assigns the JavaScript variable
the given value
.
64 65 66 |
# File 'lib/bivouac/helpers/view/goh/javascript.rb', line 64 def assign( variable, value ) record "#{variable} = #{value.inspect}" end |
#call(function, *arguments) ⇒ Object
Calls the JavaScript function
, optionally with the given arguments
.
104 105 106 |
# File 'lib/bivouac/helpers/view/goh/javascript.rb', line 104 def call(function, *arguments) record "#{function}(#{arguments_for_call(arguments)});\n" end |
#delay(seconds = 1) ⇒ Object
Executes the content of the block after a delay of seconds
. Example:
page.delay(20) do
page.visual_effect :fade, 'notice'
end
175 176 177 178 179 |
# File 'lib/bivouac/helpers/view/goh/javascript.rb', line 175 def delay(seconds = 1) record "setTimeout(function() {\n\n" yield record "}, #{(seconds * 1000).to_i})" end |
#draggable(id, options = {}) ⇒ Object
Creates a script.aculo.us draggable element. See ActionView::Helpers::ScriptaculousHelper for more information.
70 71 72 |
# File 'lib/bivouac/helpers/view/goh/javascript.rb', line 70 def draggable( id, = {} ) record @context.draggable_element_js( id, ) + ";\n" end |
#drop_receiving(id, options = {}) ⇒ Object
Creates a script.aculo.us drop receiving element. See ActionView::Helpers::ScriptaculousHelper for more information.
76 77 78 |
# File 'lib/bivouac/helpers/view/goh/javascript.rb', line 76 def drop_receiving( id, = {} ) record @context.drop_receiving_element_js( id, ) + ";\n" end |
#hide(*ids) ⇒ Object
Hides the visible DOM elements with the given ids
.
81 82 83 |
# File 'lib/bivouac/helpers/view/goh/javascript.rb', line 81 def hide( *ids ) loop_on_multiple_args 'Element.hide', ids end |
#insert_html(position, id, data) ⇒ Object
Inserts HTML at the specified position
relative to the DOM element identified by the given id
.
position
may be one of:
:top
-
HTML is inserted inside the element, before the element’s existing content.
:bottom
-
HTML is inserted inside the element, after the element’s existing content.
:before
-
HTML is inserted immediately preceeding the element.
:after
-
HTML is inserted immediately following the element.
data
may be a string of HTML to insert.
98 99 100 101 |
# File 'lib/bivouac/helpers/view/goh/javascript.rb', line 98 def insert_html(position, id, data) insertion = position.to_s.camelize call "new Insertion.#{insertion}", id, data end |
#redirect_to(location) ⇒ Object
Redirects the browser to the given location
.
109 110 111 |
# File 'lib/bivouac/helpers/view/goh/javascript.rb', line 109 def redirect_to(location) assign 'window.location.href', location end |
#remove(*ids) ⇒ Object
Removes the DOM elements with the given ids from the page.
114 115 116 |
# File 'lib/bivouac/helpers/view/goh/javascript.rb', line 114 def remove(*ids) loop_on_multiple_args 'Element.remove', ids end |
#replace(id, data) ⇒ Object
Replaces the “outer HTML” (i.e., the entire element, not just its contents) of the DOM element with the given id
.
data
may be a string of HTML to insert. For example:
# Replace the DOM element having ID 'person-45' with the
# 'person' partial for the appropriate object.
replace 'person-45', render( 'person', :object => @person )
139 140 141 |
# File 'lib/bivouac/helpers/view/goh/javascript.rb', line 139 def replace(id, data) call 'Element.replace', id, data end |
#replace_html(id, data) ⇒ Object
Replaces the inner HTML of the DOM element with the given id
.
data
may be a string of HTML to insert
# Replace the HTML of the DOM element having ID 'person-45' with the
# 'person' partial for the appropriate object.
replace_html 'person-45', render( 'person', :object => @person )
126 127 128 |
# File 'lib/bivouac/helpers/view/goh/javascript.rb', line 126 def replace_html(id, data) call 'Element.update', id, data end |
#select(pattern) ⇒ Object
Returns a collection reference by finding it through a CSS pattern
in the DOM. This collection can then be used for further method calls. Examples:
page.select('p') # => $$('p');
page.select('p.welcome b').first # => $$('p.welcome b').first();
page.select('p.welcome b').first.hide # => $$('p.welcome b').first().hide();
You can also use prototype enumerations with the collection. Observe:
page.select('#items li').each do |value|
value.hide
end
# => $$('#items li').each(function(value) { value.hide(); });
Though you can call the block param anything you want, they are always rendered in the javascript as ‘value, index.’ Other enumerations, like collect() return the last statement:
page.select('#items li').collect('hidden') do |item|
item.hide
end
# => var hidden = $$('#items li').collect(function(value, index) { return value.hide(); });
49 50 51 |
# File 'lib/bivouac/helpers/view/goh/javascript.rb', line 49 def select(pattern) # JavaScriptElementCollectionProxy.new(self, pattern) end |
#show(*ids) ⇒ Object
Shows hidden DOM elements with the given ids
.
144 145 146 |
# File 'lib/bivouac/helpers/view/goh/javascript.rb', line 144 def show(*ids) loop_on_multiple_args 'Element.show', ids end |
#sortable(id, options = {}) ⇒ Object
Creates a script.aculo.us sortable element. Useful to recreate sortable elements after items get added or deleted.
150 151 152 |
# File 'lib/bivouac/helpers/view/goh/javascript.rb', line 150 def sortable(id, = {}) record @context.sortable_element_js( id, ) + ";\n" end |
#to_s ⇒ Object
:nodoc:
11 12 13 14 15 16 |
# File 'lib/bivouac/helpers/view/goh/javascript.rb', line 11 def to_s #:nodoc: javascript = "try {\n#{@source}\n} catch (e) " javascript << "{ alert('RJS error:\\n\\n' + e.toString()); alert('#{@source.gsub('\\','\0\0').gsub(/\r\n|\n|\r/, "\\n").gsub(/["']/) { |m| "\\#{m}" }}'); throw e }" return javascript end |
#toggle(*ids) ⇒ Object
Toggles the visibility of the DOM elements with the given ids
.
161 162 163 |
# File 'lib/bivouac/helpers/view/goh/javascript.rb', line 161 def toggle(*ids) loop_on_multiple_args 'Element.toggle', ids end |
#unsortable(id) ⇒ Object
Creates a script.aculo.us unsortable element. Useful to recreate unsortable elements after items get added or deleted.
156 157 158 |
# File 'lib/bivouac/helpers/view/goh/javascript.rb', line 156 def unsortable( id ) record @context.unsortable_element_js( id ) + ";\n" end |
#visual_effect(name, element_id = false, js_options = {}) ⇒ Object
Starts a script.aculo.us visual effect.
166 167 168 |
# File 'lib/bivouac/helpers/view/goh/javascript.rb', line 166 def visual_effect(name, element_id = false, = {}) record( @context.visual_effect( name, element_id, ) + ";\n" ) end |