@@ -3,20 +3,24 @@ module Browser
3
3
class Event
4
4
include Native
5
5
6
+ # @see https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events
6
7
class Definition
7
8
include Native
8
9
10
+ # @private
9
11
def self . new ( &block )
10
12
data = super ( `{ bubbles: true, cancelable: true }` )
11
13
block . call ( data ) if block
12
14
13
15
data . to_n
14
16
end
15
17
18
+ # Set the event as bubbling.
16
19
def bubbles = ( value )
17
20
`#@native .bubbles = #{ value } `
18
21
end
19
22
23
+ # Set the event as cancelable.
20
24
def cancelable = ( value )
21
25
`#@native .cancelable = #{ value } `
22
26
end
@@ -57,17 +61,22 @@ def self.target(&block)
57
61
class Callback
58
62
attr_reader :target , :name , :selector
59
63
64
+ # @private
60
65
def initialize ( target , name , selector = nil , &block )
61
66
@target = target
62
67
@name = name
63
68
@selector = selector
64
69
@block = block
65
70
end
66
71
67
- def call ( e )
68
- to_proc . call ( e )
72
+ # Call the callback with the given event.
73
+ #
74
+ # @param event [native] the native event object
75
+ def call ( event )
76
+ to_proc . call ( event )
69
77
end
70
78
79
+ # Get the native function linked to the callback.
71
80
def to_proc
72
81
@proc ||= -> event {
73
82
%x{
@@ -86,10 +95,13 @@ def to_proc
86
95
}
87
96
end
88
97
98
+ # @!attribute [r] event
99
+ # @return [Class] the class for the event
89
100
def event
90
101
Event . class_for ( @name )
91
102
end
92
103
104
+ # Stop listening for the event linked to the callback.
93
105
def off
94
106
target . off ( self )
95
107
end
@@ -102,6 +114,7 @@ def initialize(target, name, pair)
102
114
@pair = pair
103
115
end
104
116
117
+ # Stop listening for the event linked to the delegate.
105
118
def off
106
119
delegate = @target . delegated [ @name ]
107
120
delegate . last . delete ( @pair )
@@ -115,6 +128,26 @@ def off
115
128
116
129
Delegates = Struct . new ( :callback , :handlers )
117
130
131
+ # @overload on(name, &block)
132
+ #
133
+ # Start listening for an event on the target.
134
+ #
135
+ # @param name [String] the event name
136
+ #
137
+ # @yieldparam event [Event] the event
138
+ #
139
+ # @return [Callback]
140
+ #
141
+ # @overload on(name, selector, &block)
142
+ #
143
+ # Start listening for an event on the target children.
144
+ #
145
+ # @param name [String] the event name
146
+ # @param selector [String] the CSS selector to trigger the event on
147
+ #
148
+ # @yieldparam event [Event] the event
149
+ #
150
+ # @return [Delegate]
118
151
def on ( name , selector = nil , &block )
119
152
raise ArgumentError , 'no block has been given' unless block
120
153
@@ -152,6 +185,13 @@ def on(name, selector = nil, &block)
152
185
end
153
186
end
154
187
188
+ # Start listening for an event in the capturing phase.
189
+ #
190
+ # @param name [String] the event name
191
+ #
192
+ # @yieldparam event [Event] the event
193
+ #
194
+ # @return [Callback]
155
195
def on! ( name , &block )
156
196
raise ArgumentError , 'no block has been given' unless block
157
197
@@ -230,6 +270,13 @@ def attach!(*)
230
270
end
231
271
end
232
272
273
+ # @overload off()
274
+ # Stop listening for any event.
275
+ #
276
+ # @overload off(what)
277
+ # Stop listening for an event.
278
+ #
279
+ # @param what [Callback, String, Regexp] what to stop listening for
233
280
def off ( what = nil )
234
281
case what
235
282
when Callback
@@ -295,6 +342,12 @@ def detach(callback)
295
342
end
296
343
end
297
344
345
+ # Trigger an event on the target.
346
+ #
347
+ # @param name [String] the event name
348
+ # @param args [Array] optional arguments to the event callback
349
+ #
350
+ # @yieldparam definition [Definition] definition to customize the event
298
351
def trigger ( event , *args , &block )
299
352
if event . is_a? String
300
353
event = Event . create ( event , *args , &block )
@@ -303,7 +356,12 @@ def trigger(event, *args, &block)
303
356
dispatch ( event )
304
357
end
305
358
306
- # Trigger the event without bubbling.
359
+ # Trigger an event on the target without bubbling.
360
+ #
361
+ # @param name [String] the event name
362
+ # @param args [Array] optional arguments to the event callback
363
+ #
364
+ # @yieldparam definition [Definition] definition to customize the event
307
365
def trigger! ( event , *args , &block )
308
366
trigger event , *args do |e |
309
367
block . call ( e ) if block
0 commit comments