Skip to content

Commit b1fb9b6

Browse files
committedDec 6, 2014
Some more docs and cleanup
1 parent 51e9601 commit b1fb9b6

File tree

6 files changed

+260
-37
lines changed

6 files changed

+260
-37
lines changed
 

‎CHANGELOG.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# EDGE
22

3+
* Add `Browser::Window` class, and make `::Window` an instance of it.
4+
5+
* Make `Document` include `Browser::DocumentMethods` which is a simple
6+
module to define custom methods for `Document`.
7+
38
* Cleanup HTTP implementation.
49

510
* `Element#[]` and `Element#attr` now return `nil` for empty attributes,
@@ -15,7 +20,7 @@
1520
* Add Promise support to `HTTP` get/post/put/delete methods. Also remove
1621
`HTTP#callback` and `#errback` methods.
1722

18-
# 0.2.0 - December 3, 2014
23+
# 0.2.0 - March 12, 2013
1924

2025
* Add `Document.body` and `Document.head` shortcut to element instances.
2126

‎opal/opal-jquery/document.rb

+80-20
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,93 @@
11
require 'opal-jquery/constants'
22
require 'opal-jquery/element'
33

4-
# {Document} is an instance of {Element} that wraps the `document` object as
5-
# a simple jQuery object.
6-
Document = Element.find(`document`)
7-
8-
class << Document
9-
`var $ = #{JQUERY_SELECTOR.to_n}` # cache $ for SPEED
4+
module Browser
5+
# {Document} includes these methods to extend {Element}.
6+
#
7+
# Generally, you will want to use the {::Document} top level instance of
8+
# {Element}.
9+
#
10+
# ## Usage
11+
#
12+
# A useful method on {Document} is the {#ready?} method, which can be used to
13+
# run a block once the document is ready. This is equivalent to passing a
14+
# function to the `jQuery` constructor.
15+
#
16+
# Document.ready? do
17+
# puts "Page is ready to use!"
18+
# end
19+
#
20+
# Just like jQuery, multiple blocks may be passed to {#ready?}.
21+
#
22+
# ### Document head and body elements
23+
#
24+
# Every document has atleast two elements: a `head` and `body`. For
25+
# convenience, these are both exposed as {#head} and {#body} respectively,
26+
# and are just instances of {Element}.
27+
#
28+
# puts Document.head
29+
# puts Document.body
30+
#
31+
# # => #<Element: [<head>]>
32+
# # => #<Element: [<body>]>
33+
#
34+
# ### Events
35+
#
36+
# {Document} instances also have {#on}, {#off} and {#trigger} methods for
37+
# handling events. These all just delegate to their respective methods on
38+
# {Element}, using `document` as the context.
39+
#
40+
# Document.on :click do |evt|
41+
# puts "someone clicked somewhere in the document"
42+
# end
43+
#
44+
module DocumentMethods
45+
`var $ = #{JQUERY_SELECTOR.to_n}` # cache $ for SPEED
1046

11-
def ready?(&block)
12-
`$(#{ block })` if block
13-
end
47+
# Register a block to run once the document/page is ready.
48+
#
49+
# @example
50+
# Document.ready? do
51+
# puts "ready to go"
52+
# end
53+
#
54+
def ready?(&block)
55+
`$(#{block})` if block_given?
56+
end
1457

15-
def title
16-
`document.title`
17-
end
58+
# Returns document title.
59+
#
60+
# @return [String]
61+
def title
62+
`document.title`
63+
end
1864

19-
def title=(title)
20-
`document.title = #{title}`
21-
end
65+
# Set document title.
66+
#
67+
# @param title [String]
68+
def title=(title)
69+
`document.title = title`
70+
end
2271

23-
def head
24-
Element.find(`document.head`)
25-
end
72+
# {Element} instance wrapping `document.head`.
73+
#
74+
# @return [Element]
75+
def head
76+
Element.find `document.head`
77+
end
2678

27-
def body
28-
Element.find(`document.body`)
79+
# {Element} instance wrapping `document.body`.
80+
#
81+
# @return [Element]
82+
def body
83+
Element.find `document.body`
84+
end
2985
end
3086
end
3187

88+
# Top level {Document} instance wrapping `window.document`.
89+
Document = Element.find(`document`)
90+
Document.send(:extend, Browser::DocumentMethods)
91+
3292
# TODO: this will be removed soon (here for compatibility)
3393
$document = Document

‎opal/opal-jquery/element.rb

+2
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,8 @@ def self.expose(*methods)
305305
# @!method []=(attr, value)
306306
#
307307
# Set the given attribute `attr` on each element in this collection.
308+
#
309+
# @see http://api.jquery.com/attr/
308310
alias_native :[]=, :attr
309311

310312
# @!method add_class(class_name)

‎opal/opal-jquery/event.rb

+108-15
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,87 @@
11
require 'opal-jquery/constants'
22

3-
# Wraps native jQuery event objects.
3+
# {Event} wraps native jQuery events into a ruby api. Instances of events
4+
# can be accessed by {#to_n}.
5+
#
6+
# {Event} instances should not be created directly, as they are usually
7+
# created by one of the dom event handlers in {Element}.
8+
#
9+
# element.on :click do |event|
10+
# puts event
11+
# end
12+
#
13+
# # => #<Event:0x0000000>
14+
#
15+
# ## Usage
16+
#
17+
# {Event} exposes a slightly different API than jQuery, as {Event} tries to
18+
# add some more ruby flavour to the object.
19+
#
20+
# ### Accessing element triggering event
21+
#
22+
# Unlike jQuery, the context of an event handler is not set to the triggering
23+
# element. Instead, the element triggering the event can be accessed from the
24+
# {Event} instance.
25+
#
26+
# #### Current Target
27+
#
28+
# To access the current element in the bubbling phase, {#element} or
29+
# {#current_target} can be used which is the same as `currentTarget` or `this`
30+
# from jQuery.
31+
#
32+
# element.on :click do |event|
33+
# puts "element clicked: #{event.element}
34+
# end
35+
#
36+
# # => "element clicked: #<Element: [<div>]>
37+
#
38+
# #### Target
39+
#
40+
# The {#target} of an event is the actual dom element that triggered the event,
41+
# and this will be the same element through all phases of event bubbling. This
42+
# is the same as the `event.target` jQuery property.
43+
#
44+
# element.on :click do |event|
45+
# puts "actual element: #{event.target}"
46+
# end
47+
#
48+
# # => "actual element: #<Element: [<div>]>
49+
#
50+
# ### Controlling Event Bubbling
51+
#
52+
# Propagation and default behaviour can be controlled on events using {#prevent}
53+
# and {#stop}, which will prevent the browser default and stop event propagation
54+
# respectively.
55+
#
56+
# element.on :click do |event|
57+
# event.prevent # prevent browser default
58+
# event.stop # stop event propagation
59+
# end
60+
#
61+
# If you want to trigger both methods, which is usually the case, then {#kill}
62+
# can be used as a shorthand.
63+
#
64+
# element.on :click do |event|
65+
# event.kill
66+
# puts event.prevented?
67+
# puts event.stopped?
68+
# end
69+
#
70+
# # => true
71+
# # => true
72+
#
473
class Event
574
`var $ = #{JQUERY_SELECTOR.to_n}` # cache $ for SPEED
675

76+
# @private
77+
# @param native [JSObject] native jquery/javascript event
778
def initialize(native)
879
@native = native
980
end
1081

82+
# Returns native javascript event created by jQuery.
83+
#
84+
# @return [JSObject]
1185
def to_n
1286
@native
1387
end
@@ -20,32 +94,47 @@ def type
2094
`#@native.type`
2195
end
2296

23-
##
24-
# Element
25-
26-
def current_target
97+
# Returns the current element in the bubbling cycle of the event. This is
98+
# not the same as the actual dom event that triggered the event, but is
99+
# usually the context element the event was registered with, or the target
100+
# of the css selector used in newer event styles.
101+
#
102+
# @return [Element]
103+
def element
27104
`$(#@native.currentTarget)`
28105
end
29106

107+
alias current_target element
108+
109+
# Returns the actual element that triggered the dom event.
110+
#
111+
# @return [Element]
30112
def target
31113
`$(#@native.target)`
32114
end
33115

34-
##
35-
# Propagation
36-
116+
# Returns `true` if this event has had its default browser behaviour
117+
# prevented, `false` otherwise.
118+
#
119+
# @return [Boolean]
37120
def prevented?
38121
`#@native.isDefaultPrevented()`
39122
end
40123

124+
# Prevent this event from triggering its default browser behaviour.
41125
def prevent
42126
`#@native.preventDefault()`
43127
end
44128

129+
# Returns `true` if the propagation/bubbling of this event has been stopped,
130+
# `false` otherwise.
131+
#
132+
# @return [Boolean]
45133
def stopped?
46134
`#@native.isPropagationStopped()`
47135
end
48136

137+
# Stop further propagaion of this event.
49138
def stop
50139
`#@native.stopPropagation()`
51140
end
@@ -55,18 +144,14 @@ def stop_immediate
55144
end
56145

57146
# Stops propagation and prevents default action.
147+
#
148+
# @see {#prevent}
149+
# @see {#stop}
58150
def kill
59151
stop
60152
prevent
61153
end
62154

63-
# to be removed?
64-
alias default_prevented? prevented?
65-
alias prevent_default prevent
66-
alias propagation_stopped? stopped?
67-
alias stop_propagation stop
68-
alias stop_immediate_propagation stop_immediate
69-
70155
##
71156
# Keyboard/Mouse/Touch
72157

@@ -97,4 +182,12 @@ def key_code
97182
def which
98183
`#@native.which`
99184
end
185+
186+
# @deprecated These will be removed soon
187+
alias default_prevented? prevented?
188+
alias prevent_default prevent
189+
alias propagation_stopped? stopped?
190+
alias stop_propagation stop
191+
alias stop_immediate_propagation stop_immediate
192+
100193
end

‎opal/opal-jquery/local_storage.rb

+27
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,56 @@ module Browser
1414
# LocalStorage['bar'] # => nil
1515
#
1616
# @see LocalStorage
17+
#
1718
class LocalStorage
1819
def initialize(storage)
1920
@storage = storage
2021
end
2122

23+
# Set a value in storage.
24+
#
25+
# Values stored in {LocalStorage} will be stored as strings. To store any
26+
# other type of object, you will need to convert them to a string first,
27+
# and then convert them back from {#[]}. For this reason it is recommended
28+
# to only store {JSON} based objects in storage, so they can be easily
29+
# converted back and forth.
30+
#
31+
# @param key [String] string key
32+
# @param value [String, #to_s] string or explicitly converted object
2233
def []=(key, value)
2334
%x{
2435
#@storage.setItem(key, value);
2536
return value;
2637
}
2738
end
2839

40+
# Retrieve an object from {LocalStorage}.
41+
#
42+
# Only string values can be stored, so any object will be returned as a
43+
# string. You will need to handle any conversion back into a normal
44+
# object. {JSON.parse} could be used, for example, to parse back into
45+
# arrays or hashes.
46+
#
47+
# If a key is not present in the storage, then `nil` will be returned.
48+
#
49+
# @param key [String] key to lookup
50+
# @return [String, nil]
2951
def [](key)
3052
%x{
3153
var value = #@storage.getItem(key);
3254
return value == null ? nil : value;
3355
}
3456
end
3557

58+
# Removes a specific `key` from storage. If the key does not exist then
59+
# there is no side effect.
60+
#
61+
# @param key [String] key to remove
3662
def delete(key)
3763
`#@storage.removeItem(key)`
3864
end
3965

66+
# Remove all key/values from storage
4067
def clear
4168
`#@storage.clear()`
4269
end

0 commit comments

Comments
 (0)