Module: DOM::Events

Included in:
FileReader, NODE, Window
Defined in:
opal/fron/event_mock.rb,
opal/fron/dom/modules/events.rb

Overview

Events module for managing events for elements and element like nodes (SVG Element for example).

Features:

  • Store event listeners for listing and removal

  • Shorthand on and on! for non capture / capture

  • Triggering event dynamically

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#listenersHash (readonly)

Returns The listeners in a hash.

Returns:

  • (Hash)

    The listeners in a hash



10
11
12
# File 'opal/fron/dom/modules/events.rb', line 10

def listeners
  @listeners
end

Instance Method Details

#add_listener(type, capture = false) {|event| ... } ⇒ Function (private)

Adds an event listener for the given type.

Parameters:

  • type (String)

    The type

  • capture (Boolean) (defaults to: false)

    To use capture or not

Yield Parameters:

  • event (Event)

    The event

Returns:

  • (Function)

    The native function for later removal



101
102
103
104
105
106
107
108
109
110
# File 'opal/fron/dom/modules/events.rb', line 101

def add_listener(type, capture = false)
  method = `function(e){#{ yield Event.new(`e`)}}`

  @listeners ||= {}
  @listeners[type] ||= []
  @listeners[type] << method

  `#{@el}.addEventListener(#{type},#{method},#{capture})`
  method
end

#delegate(type, selector) {|event| ... } ⇒ Object

Delegates the events with the given type if they match the given selector

Parameters:

  • type (String)

    The type

  • selector (type)

    The selector

Yield Parameters:

  • event (Event)

    The event



84
85
86
87
88
89
# File 'opal/fron/dom/modules/events.rb', line 84

def delegate(type, selector)
  on type do |event|
    break unless event.target.matches selector
    yield event
  end
end

#off(type = nil, method = nil) ⇒ Object

Removes events

  • If type and method given removes the exact event

  • If only type is given removes events with that type

  • If no arguments are passed removes all attached events

Parameters:

  • type (String) (defaults to: nil)

    The type

  • method (Function) (defaults to: nil)

    The listener returned from on



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'opal/fron/dom/modules/events.rb', line 60

def off(type = nil, method = nil)
  return unless @listeners

  if type.nil?
    @listeners.keys.each do |ltype|
      remove_listeners ltype
    end
  elsif method.nil?
    remove_listeners type
  else
    return unless @listeners[type].index(method)
    @listeners[type].delete method
    `#{@el}.removeEventListener(#{type},#{method})`
    `#{@el}.removeEventListener(#{type},#{method},true)`
  end
end

#old_triggerObject

The old trigger method



64
# File 'opal/fron/event_mock.rb', line 64

alias old_trigger trigger

#on(type) {|event| ... } ⇒ Object

Listens on the given type of event without capture

Parameters:

Yield Parameters:

  • event (Event)

    The event



48
49
50
# File 'opal/fron/dom/modules/events.rb', line 48

def on(type, &listener)
  add_listener type, &listener
end

#on!(type) {|event| ... } ⇒ Object

Listens on the given type of event with capture

Parameters:

Yield Parameters:

  • event (Event)

    The event



39
40
41
# File 'opal/fron/dom/modules/events.rb', line 39

def on!(type, &listener)
  add_listener type, true, &listener
end

#remove_listeners(type) ⇒ Object (private)

Removes all events with the given type

Parameters:



115
116
117
118
119
120
# File 'opal/fron/dom/modules/events.rb', line 115

def remove_listeners(type)
  @listeners[type].each do |method|
    @listeners[type].delete method
    `#{@el}.removeEventListener(#{type},#{method})`
  end
end

#trigger(type, data = {}) ⇒ Object

Triggers an event with the given type and data

Parameters:

  • type (String)

    The type

  • data (Hash) (defaults to: {})

    The data



16
17
18
19
20
# File 'opal/fron/dom/modules/events.rb', line 16

def trigger(type, data = {})
  return old_trigger(type, data) if EventMock.mock
  puts "Triggered syntetic event \"#{type}\" for \"#{path}\"" if EventMock.verbose
  EventMock.trigger_event self, type, data
end