Sha256: a0d87f934778a1f0977a8ea2d97871262e824db72b7f4ee3a00a6b65f737b3f9

Contents?: true

Size: 1.31 KB

Versions: 13

Compression:

Stored size: 1.31 KB

Contents

require 'bowser/event'

module Bowser
  module EventTarget
    # Add the block as a handler for the specified event name. Will use either
    # `addEventListener` or `addListener` if they exist.
    #
    # @param event_name [String] the name of the event
    # @return [Proc] the block to pass to `off` to remove this handler
    # @yieldparam event [Bowser::Event] the event object
    def on event_name, &block
      wrapper = proc { |event| block.call Event.new(event) }

      if `#@native.addEventListener !== undefined`
        `#@native.addEventListener(event_name, wrapper)`
      elsif `#@native.addListener !== undefined`
        `#@native.addListener(event_name, wrapper)`
      else
        warn "[Bowser] Not entirely sure how to add an event listener to #{self}"
      end

      wrapper
    end

    # Remove an event handler
    #
    # @param event_name [String] the name of the event
    # @block the handler to remove, as returned from `on`
    def off event_name, &block
      if `#@native.removeEventListener !== undefined`
        `#@native.removeEventListener(event_name, block)`
      elsif `#@native.removeListener !== undefined`
        `#@native.removeListener(event_name, block)`
      else
        warn "[Bowser] Not entirely sure how to remove an event listener from #{self}"
      end

      nil
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
bowser-1.1.3 opal/bowser/event_target.rb
bowser-1.1.0 opal/bowser/event_target.rb
bowser-1.0.5 opal/bowser/event_target.rb
bowser-1.0.4 opal/bowser/event_target.rb
bowser-1.0.3 opal/bowser/event_target.rb
bowser-1.0.2 opal/bowser/event_target.rb
bowser-1.0.1 opal/bowser/event_target.rb
bowser-1.0.0 opal/bowser/event_target.rb
bowser-0.5.4 opal/bowser/event_target.rb
bowser-0.5.3 opal/bowser/event_target.rb
bowser-0.5.2 opal/bowser/event_target.rb
bowser-0.5.1 opal/bowser/event_target.rb
bowser-0.5.0 opal/bowser/event_target.rb