Module: Browser::Event::Target

Included in:
DOM::Element, Browser::EventSource, HTTP::Request, Screen, Socket, VisualViewport, Window
Defined in:
opal/browser/event/base.rb

Defined Under Namespace

Classes: Callback, Delegate, Delegates

Instance Method Summary collapse

Instance Method Details

#offObject #off(what) ⇒ Object

Overloads:

  • #offObject

    Stop listening for any event.

  • #off(what) ⇒ Object

    Stop listening for an event.

    Parameters:


311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'opal/browser/event/base.rb', line 311

def off(what = nil)
  case what
  when Callback
    callbacks.delete(what)
    detach(what)

  when String
    if what.include?(?*) or what.include?(??)
      off(Regexp.new(what.gsub(/\*/, '.*?').gsub(/\?/, ?.)))
    else
      what = Event.name_for(what)

      callbacks.delete_if {|callback|
        if callback.name == what
          detach(callback)

          true
        end
      }
    end

  when Regexp
    callbacks.delete_if {|callback|
      if callback.name =~ what
        detach(callback)

        true
      end
    }

  else
    callbacks.each {|callback|
      detach(callback)
    }

    callbacks.clear
  end
end

#on(name) {|event| ... } ⇒ Callback #on(name, selector) {|event| ... } ⇒ Delegate

Overloads:

  • #on(name) {|event| ... } ⇒ Callback

    Start listening for an event on the target.

    Parameters:

    • name (String)

      the event name

    Yield Parameters:

    • event (Event)

      the event

    Returns:

  • #on(name, selector) {|event| ... } ⇒ Delegate

    Start listening for an event on the target children.

    Parameters:

    • name (String)

      the event name

    • selector (String)

      the CSS selector to trigger the event on

    Yield Parameters:

    • event (Event)

      the event

    Returns:

Raises:

  • (ArgumentError)

151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'opal/browser/event/base.rb', line 151

def on(name, selector = nil, &block)
  raise ArgumentError, 'no block has been given' unless block

  name = Event.name_for(name)

  if selector
    unless delegate = delegated[name]
      delegate = delegated[name] = Delegates.new

      if %w[blur focus].include?(name)
        delegate.callback = on! name do |e|
          delegate(delegate, e)
        end
      else
        delegate.callback = on name do |e|
          delegate(delegate, e)
        end
      end

      pair = [selector, block]
      delegate.handlers = [pair]

      Delegate.new(self, name, pair)
    else
      pair = [selector, block]
      delegate.handlers << pair

      Delegate.new(self, name, pair)
    end
  else
    callback = Callback.new(self, name, selector, &block)
    callbacks.push(callback)

    attach(callback)
  end
end

#on!(name) {|event| ... } ⇒ Callback

Start listening for an event in the capturing phase.

Parameters:

  • name (String)

    the event name

Yield Parameters:

  • event (Event)

    the event

Returns:

Raises:

  • (ArgumentError)

195
196
197
198
199
200
201
202
203
# File 'opal/browser/event/base.rb', line 195

def on!(name, &block)
  raise ArgumentError, 'no block has been given' unless block

  name     = Event.name_for(name)
  callback = Callback.new(self, name, &block)
  callbacks.push(callback)

  attach!(callback)
end

#one(name) {|event| ... } ⇒ Callback #one(name, selector) {|event| ... } ⇒ Delegate

Overloads:

  • #one(name) {|event| ... } ⇒ Callback

    Start listening for an event on the target. Remove the event after firing so that it is fired at most once.

    Parameters:

    • name (String)

      the event name

    Yield Parameters:

    • event (Event)

      the event

    Returns:

  • #one(name, selector) {|event| ... } ⇒ Delegate

    Start listening for an event on the target children. Remove the event after firing so that it is fired at most once.

    Parameters:

    • name (String)

      the event name

    • selector (String)

      the CSS selector to trigger the event on

    Yield Parameters:

    • event (Event)

      the event

    Returns:

Raises:

  • (ArgumentError)

295
296
297
298
299
300
301
302
303
# File 'opal/browser/event/base.rb', line 295

def one (name, selector = nil, &block)
  raise ArgumentError, 'no block has been given' unless block

  cb = on name, selector do |*args|
    out = block.call(*args)
    cb.off
    out
  end
end

#trigger(event, *args) {|definition| ... } ⇒ Object

Trigger an event on the target.

Parameters:

  • event (String)

    the event name

  • args (Array)

    optional arguments to the event callback

Yield Parameters:

  • definition (Definition)

    definition to customize the event


382
383
384
385
386
387
388
# File 'opal/browser/event/base.rb', line 382

def trigger(event, *args, &block)
  if event.is_a? String
    event = Event.create(event, *args, &block)
  end

  dispatch(event)
end

#trigger!(event, *args) {|definition| ... } ⇒ Object

Trigger an event on the target without bubbling.

Parameters:

  • event (String)

    the event name

  • args (Array)

    optional arguments to the event callback

Yield Parameters:

  • definition (Definition)

    definition to customize the event


396
397
398
399
400
401
# File 'opal/browser/event/base.rb', line 396

def trigger!(event, *args, &block)
  trigger event, *args do |e|
    block.call(e) if block
    e.bubbles = false
  end
end