Class: Mjs::JavaScriptContext
- Inherits:
-
Object
- Object
- Mjs::JavaScriptContext
- Defined in:
- lib/mjs/java_script_context.rb
Overview
:nodoc:
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, &block) ⇒ 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.
-
#each(&callback) ⇒ Object
define :each method for Rack::Response because Merb::Rack::StreamWrapper can’t create response body correctly.
-
#hide(*ids) ⇒ Object
Hides the visible DOM elements with the given
ids
. -
#initialize ⇒ JavaScriptContext
constructor
A new instance of JavaScriptContext.
-
#insert_html(position, id, *options_for_render) ⇒ Object
Inserts HTML at the specified
position
relative to the DOM element identified by the givenid
. -
#literal(code) ⇒ Object
Returns an object whose
to_json
evaluates tocode
. -
#redirect_to(location) ⇒ Object
Redirects the browser to the given
location
using JavaScript, in the same form asurl_for
. -
#reload ⇒ Object
Reloads the browser’s current
location
using JavaScript. -
#remove(*ids) ⇒ Object
Removes the DOM elements with the given
ids
from the page. -
#replace(id, *options_for_render) ⇒ 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, *options_for_render) ⇒ 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
-
#toggle(*ids) ⇒ Object
Toggles the visibility of the DOM elements with the given
ids
. -
#visual_effect(name, id = nil, options = {}) ⇒ Object
Starts a script.aculo.us visual effect.
Constructor Details
#initialize ⇒ JavaScriptContext
Returns a new instance of JavaScriptContext.
17 18 19 |
# File 'lib/mjs/java_script_context.rb', line 17 def initialize @lines = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *arguments) ⇒ Object (private)
385 386 387 |
# File 'lib/mjs/java_script_context.rb', line 385 def method_missing(method, *arguments) JavaScriptProxy.new(self, Mjs::Utils.camelize(method)) end |
Instance Method Details
#<<(javascript) ⇒ Object
Writes raw JavaScript to the page.
Example:
page << "alert('JavaScript with Prototype.');"
290 291 292 |
# File 'lib/mjs/java_script_context.rb', line 290 def <<(javascript) @lines << 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();
You can also pass in a record, which will use ActionController::RecordIdentifier.dom_id to lookup the correct id:
page[@post] # => $('post_45')
page[Post.new] # => $('new_post')
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/mjs/java_script_context.rb', line 37 def [](id) case id when Symbol JavaScriptElementProxy.new(self, "##{id}") when String, NilClass JavaScriptElementProxy.new(self, id) else raise NotImplementedError, "[MJS] RecordIdentifier.dom_id(id)" # JavaScriptElementProxy.new(self, ActionController::RecordIdentifier.dom_id(id)) end end |
#alert(message) ⇒ Object
Displays an alert dialog with the given message
.
Example:
# Generates: alert('This message is from Rails!')
page.alert('This message is from Rails!')
213 214 215 |
# File 'lib/mjs/java_script_context.rb', line 213 def alert() call 'alert', end |
#assign(variable, value) ⇒ Object
Assigns the JavaScript variable
the given value
.
Examples:
# Generates: my_string = "This is mine!";
page.assign 'my_string', 'This is mine!'
# Generates: record_count = 33;
page.assign 'record_count', 33
# Generates: tabulated_total = 47
page.assign 'tabulated_total', @total_from_cart
281 282 283 |
# File 'lib/mjs/java_script_context.rb', line 281 def assign(variable, value) record "#{variable} = #{javascript_object_for(value)}" end |
#call(function, *arguments, &block) ⇒ Object
Calls the JavaScript function
, optionally with the given arguments
.
If a block is given, the block will be passed to a new JavaScriptGenerator; the resulting JavaScript code will then be wrapped inside function() { ... }
and passed as the called function’s final argument.
Examples:
# Generates: Element.replace(my_element, "My content to replace with.")
page.call 'Element.replace', 'my_element', "My content to replace with."
# Generates: alert('My message!')
page.call 'alert', 'My message!'
# Generates:
# my_method(function() {
# $("one").show();
# $("two").hide();
# });
page.call(:my_method) do |p|
p[:one].show
p[:two].hide
end
264 265 266 |
# File 'lib/mjs/java_script_context.rb', line 264 def call(function, *arguments, &block) record "#{function}(#{arguments_for_call(arguments, block)})" end |
#delay(seconds = 1) ⇒ Object
Executes the content of the block after a delay of seconds
. Example:
# Generates:
# setTimeout(function() {
# ;
# new Effect.Fade("notice",{});
# }, 20000);
page.delay(20) do
page.visual_effect :fade, 'notice'
end
304 305 306 307 308 |
# File 'lib/mjs/java_script_context.rb', line 304 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.
326 327 328 |
# File 'lib/mjs/java_script_context.rb', line 326 def draggable(id, = {}) record @context.send(:draggable_element_js, id, ) end |
#drop_receiving(id, options = {}) ⇒ Object
Creates a script.aculo.us drop receiving element. See ActionView::Helpers::ScriptaculousHelper for more information.
332 333 334 |
# File 'lib/mjs/java_script_context.rb', line 332 def drop_receiving(id, = {}) record @context.send(:drop_receiving_element_js, id, ) end |
#each(&callback) ⇒ Object
define :each method for Rack::Response because Merb::Rack::StreamWrapper can’t create response body correctly
13 14 15 |
# File 'lib/mjs/java_script_context.rb', line 13 def each(&callback) callback.call(to_s) end |
#hide(*ids) ⇒ Object
Hides the visible DOM elements with the given ids
.
Example:
# Hide a few people
# Generates: ["person_29", "person_9", "person_0"].each(Element.hide);
page.hide 'person_29', 'person_9', 'person_0'
191 192 193 |
# File 'lib/mjs/java_script_context.rb', line 191 def hide(*ids) loop_on_multiple_args 'Element.hide', ids end |
#insert_html(position, id, *options_for_render) ⇒ 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 preceding the element.
:after
-
HTML is inserted immediately following the element.
options_for_render
may be either a string of HTML to insert, or a hash of options to be passed to ActionView::Base#render. For example:
# Insert the rendered 'navigation' partial just before the DOM
# element with ID 'content'.
# Generates: Element.insert("content", { before: "-- Contents of 'navigation' partial --" });
page.insert_html :before, 'content', :partial => 'navigation'
# Add a list item to the bottom of the <ul> with ID 'list'.
# Generates: Element.insert("list", { bottom: "<li>Last item</li>" });
page.insert_html :bottom, 'list', '<li>Last item</li>'
106 107 108 109 |
# File 'lib/mjs/java_script_context.rb', line 106 def insert_html(position, id, *) content = javascript_object_for(render(*)) record "Element.insert(\"#{id}\", { #{position.to_s.downcase}: #{content} });" end |
#literal(code) ⇒ Object
Returns an object whose to_json
evaluates to code
. Use this to pass a literal JavaScript expression as an argument to another JavaScriptGenerator method.
51 52 53 54 |
# File 'lib/mjs/java_script_context.rb', line 51 def literal(code) raise NotImplementedError, "[MJS] ActiveSupport::JSON::Variable.new(code.to_s)" ActiveSupport::JSON::Variable.new(code.to_s) end |
#redirect_to(location) ⇒ Object
Redirects the browser to the given location
using JavaScript, in the same form as url_for
.
Examples:
# Generates: window.location.href = "/mycontroller";
page.redirect_to(:action => 'index')
# Generates: window.location.href = "/account/signup";
page.redirect_to(:controller => 'account', :action => 'signup')
226 227 228 229 |
# File 'lib/mjs/java_script_context.rb', line 226 def redirect_to(location) url = location.is_a?(String) ? location : @context.url_for(location) record "window.location.href = #{url.inspect}" end |
#reload ⇒ Object
Reloads the browser’s current location
using JavaScript
Examples:
# Generates: window.location.reload();
page.reload
237 238 239 |
# File 'lib/mjs/java_script_context.rb', line 237 def reload record 'window.location.reload()' end |
#remove(*ids) ⇒ Object
Removes the DOM elements with the given ids
from the page.
Example:
# Remove a few people
# Generates: ["person_23", "person_9", "person_2"].each(Element.remove);
page.remove 'person_23', 'person_9', 'person_2'
167 168 169 |
# File 'lib/mjs/java_script_context.rb', line 167 def remove(*ids) loop_on_multiple_args 'Element.remove', ids end |
#replace(id, *options_for_render) ⇒ Object
Replaces the “outer HTML” (i.e., the entire element, not just its contents) of the DOM element with the given id
.
options_for_render
may be either a string of HTML to insert, or a hash of options to be passed to ActionView::Base#render. For example:
# Replace the DOM element having ID 'person-45' with the
# 'person' partial for the appropriate object.
page.replace 'person-45', :partial => 'person', :object => @person
This allows the same partial that is used for the insert_html
to be also used for the input to replace
without resorting to the use of wrapper elements.
Examples:
<div id="people">
<%= render :partial => 'person', :collection => @people %>
</div>
# Insert a new person
#
# Generates: new Insertion.Bottom({object: "Matz", partial: "person"}, "");
page.insert_html :bottom, :partial => 'person', :object => @person
# Replace an existing person
# Generates: Element.replace("person_45", "-- Contents of partial --");
page.replace 'person_45', :partial => 'person', :object => @person
155 156 157 |
# File 'lib/mjs/java_script_context.rb', line 155 def replace(id, *) call 'Element.replace', id, render(*) end |
#replace_html(id, *options_for_render) ⇒ Object
Replaces the inner HTML of the DOM element with the given id
.
options_for_render
may be either a string of HTML to insert, or a hash of options to be passed to ActionView::Base#render. For example:
# Replace the HTML of the DOM element having ID 'person-45' with the
# 'person' partial for the appropriate object.
# Generates: Element.update("person-45", "-- Contents of 'person' partial --");
page.replace_html 'person-45', :partial => 'person', :object => @person
121 122 123 |
# File 'lib/mjs/java_script_context.rb', line 121 def replace_html(id, *) call 'Element.update', id, render(*) 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:
# Generates: $$('#items li').each(function(value) { value.hide(); });
page.select('#items li').each do |value|
value.hide
end
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:
# Generates: var hidden = $$('#items li').collect(function(value, index) { return value.hide(); });
page.select('#items li').collect('hidden') do |item|
item.hide
end
78 79 80 |
# File 'lib/mjs/java_script_context.rb', line 78 def select(pattern) JavaScriptElementCollectionProxy.new(self, pattern) end |
#show(*ids) ⇒ Object
Shows hidden DOM elements with the given ids
.
Example:
# Show a few people
# Generates: ["person_6", "person_13", "person_223"].each(Element.show);
page.show 'person_6', 'person_13', 'person_223'
179 180 181 |
# File 'lib/mjs/java_script_context.rb', line 179 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. See ActionView::Helpers::ScriptaculousHelper for more information.
320 321 322 |
# File 'lib/mjs/java_script_context.rb', line 320 def sortable(id, = {}) record @context.send(:sortable_element_js, id, ) end |
#to_s ⇒ Object
21 22 23 |
# File 'lib/mjs/java_script_context.rb', line 21 def to_s javascript = @lines * $/ end |
#toggle(*ids) ⇒ Object
Toggles the visibility of the DOM elements with the given ids
. Example:
# Show a few people
# Generates: ["person_14", "person_12", "person_23"].each(Element.toggle);
page.toggle 'person_14', 'person_12', 'person_23' # Hides the elements
page.toggle 'person_14', 'person_12', 'person_23' # Shows the previously hidden elements
203 204 205 |
# File 'lib/mjs/java_script_context.rb', line 203 def toggle(*ids) loop_on_multiple_args 'Element.toggle', ids end |
#visual_effect(name, id = nil, options = {}) ⇒ Object
Starts a script.aculo.us visual effect. See ActionView::Helpers::ScriptaculousHelper for more information.
312 313 314 |
# File 'lib/mjs/java_script_context.rb', line 312 def visual_effect(name, id = nil, = {}) record @context.send(:visual_effect, name, id, ) end |