Class: Glimmer::Web::ListenerProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/glimmer/web/listener_proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_elementObject (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

#elementObject (readonly)

Returns the value of attribute element.



27
28
29
# File 'lib/glimmer/web/listener_proxy.rb', line 27

def element
  @element
end

#event_attributeObject (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_nameObject (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_listenerObject (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_listenerObject (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

#selectorObject (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

#registerObject 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

#unregisterObject 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