Class: Glimmer::Web::ListenerProxy
- Inherits:
-
Object
- Object
- Glimmer::Web::ListenerProxy
- Defined in:
- lib/glimmer/web/listener_proxy.rb
Instance Attribute Summary collapse
-
#dom_element ⇒ Object
readonly
Returns the value of attribute dom_element.
-
#element ⇒ Object
readonly
Returns the value of attribute element.
-
#event_attribute ⇒ Object
readonly
Returns the value of attribute event_attribute.
-
#event_name ⇒ Object
readonly
Returns the value of attribute event_name.
-
#js_listener ⇒ Object
readonly
Returns the value of attribute js_listener.
-
#original_event_listener ⇒ Object
readonly
Returns the value of attribute original_event_listener.
-
#selector ⇒ Object
readonly
Returns the value of attribute selector.
Instance Method Summary collapse
-
#initialize(element:, event_attribute:, dom_element:, selector:, original_event_listener:) ⇒ ListenerProxy
constructor
A new instance of ListenerProxy.
- #register ⇒ Object (also: #observe, #reregister)
- #unregister ⇒ Object (also: #unobserve, #deregister)
Constructor Details
#initialize(element:, event_attribute:, dom_element:, selector:, original_event_listener:) ⇒ ListenerProxy
Returns a new instance of ListenerProxy.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/glimmer/web/listener_proxy.rb', line 29 def initialize(element:, event_attribute:, dom_element:, selector:, original_event_listener:) @element = element @event_attribute = event_attribute @event_name = event_attribute.sub(/^on_/, '').sub(/^on/, '') @dom_element = dom_element @selector = selector if !event_attribute.start_with?('on_') # custom event @js_listener = lambda do |js_event| event = EventProxy.new(js_event: js_event, listener: self) result = original_event_listener.call(event) result = true if result.nil? result end end @original_event_listener = original_event_listener end |
Instance Attribute Details
#dom_element ⇒ Object (readonly)
Returns the value of attribute dom_element.
27 28 29 |
# File 'lib/glimmer/web/listener_proxy.rb', line 27 def dom_element @dom_element end |
#element ⇒ Object (readonly)
Returns the value of attribute element.
27 28 29 |
# File 'lib/glimmer/web/listener_proxy.rb', line 27 def element @element end |
#event_attribute ⇒ Object (readonly)
Returns the value of attribute event_attribute.
27 28 29 |
# File 'lib/glimmer/web/listener_proxy.rb', line 27 def event_attribute @event_attribute end |
#event_name ⇒ Object (readonly)
Returns the value of attribute event_name.
27 28 29 |
# File 'lib/glimmer/web/listener_proxy.rb', line 27 def event_name @event_name end |
#js_listener ⇒ Object (readonly)
Returns the value of attribute js_listener.
27 28 29 |
# File 'lib/glimmer/web/listener_proxy.rb', line 27 def js_listener @js_listener end |
#original_event_listener ⇒ Object (readonly)
Returns the value of attribute original_event_listener.
27 28 29 |
# File 'lib/glimmer/web/listener_proxy.rb', line 27 def original_event_listener @original_event_listener end |
#selector ⇒ Object (readonly)
Returns the value of attribute selector.
27 28 29 |
# File 'lib/glimmer/web/listener_proxy.rb', line 27 def selector @selector end |
Instance Method Details
#register ⇒ Object Also known as: observe, reregister
46 47 48 |
# File 'lib/glimmer/web/listener_proxy.rb', line 46 def register @dom_element.on(@event_name, &@js_listener) unless @js_listener.nil? end |
#unregister ⇒ Object Also known as: unobserve, deregister
52 53 54 55 56 |
# File 'lib/glimmer/web/listener_proxy.rb', line 52 def unregister # TODO contribute fix to opal to allow passing observer with & to off with selector not specified as nil @dom_element.off(@event_name, @js_listener) unless @js_listener.nil? @element.listeners_for(@event_attribute).delete(self) end |