Module: Browser::Event::Target

Included in:
DOM::Element, Browser::EventSource, Screen, Socket, 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:


280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'opal/browser/event/base.rb', line 280

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

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

Trigger an event on the target.

Parameters:

  • name (String)

    the event name

  • args (Array)

    optional arguments to the event callback

Yield Parameters:

  • definition (Definition)

    definition to customize the event


351
352
353
354
355
356
357
# File 'opal/browser/event/base.rb', line 351

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:

  • name (String)

    the event name

  • args (Array)

    optional arguments to the event callback

Yield Parameters:

  • definition (Definition)

    definition to customize the event


365
366
367
368
369
370
# File 'opal/browser/event/base.rb', line 365

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