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
-
#listeners ⇒ Hash
readonly
The listeners in a hash.
Instance Method Summary collapse
-
#add_listener(type, capture = false) {|event| ... } ⇒ Function
private
Adds an event listener for the given type.
-
#delegate(type, selector) {|event| ... } ⇒ Object
Delegates the events with the given type if they match the given selector.
-
#off(type = nil, method = nil) ⇒ Object
Removes events.
-
#old_trigger ⇒ Object
The old trigger method.
-
#on(type) {|event| ... } ⇒ Object
Listens on the given type of event without capture.
-
#on!(type) {|event| ... } ⇒ Object
Listens on the given type of event with capture.
-
#remove_listeners(type) ⇒ Object
private
Removes all events with the given type.
-
#trigger(type, data = {}) ⇒ Object
Triggers an event with the given type and data.
Instance Attribute Details
#listeners ⇒ Hash (readonly)
Returns 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.
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
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
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_trigger ⇒ Object
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
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
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
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
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 |