Sha256: 62fcde80105c3e7bdc2ee9f0349185b600ec40f1047f9f0af28ce5ae38583be5

Contents?: true

Size: 1.46 KB

Versions: 2

Compression:

Stored size: 1.46 KB

Contents

require 'ostruct'

module Browser; class Event

class Custom < Event
  def self.supported?
    Browser.supports? 'Event.Custom'
  end

  class Definition < Definition
    def method_missing(name, value)
      if name.end_with? ?=
        `#@native[#{name[0 .. -2]}] = value`
      end
    end
  end

  if Browser.supports? 'Event.constructor'
    def self.construct(name, desc)
      `new CustomEvent(name, {
        bubbles:    desc.bubbles,
        cancelable: desc.cancelable,
        detail:     desc })`
    end
  elsif Browser.supports? 'Event.create'
    def self.construct(name, desc)
      %x{
        var event = document.createEvent("CustomEvent");
            event.initCustomEvent(name, desc.bubbles, desc.cancelable, desc);

        return event;
      }
    end
  elsif Browser.supports? 'Event.createObject'
    def self.construct(name, desc)
      Native(`document.createEventObject()`).merge!(`{
        type:       name,
        bubbles:    desc.bubbles,
        cancelable: desc.cancelable,
        detail:     desc }`).to_n
    end
  else
    def self.construct(name, desc)
      Native(desc).merge!(`{
        type:       name,
        bubbles:    desc.bubbles,
        cancelable: desc.cancelable,
        detail:     desc }`).to_n
    end
  end

  def initialize(event, callback = nil)
    super(event, callback)

    @detail = Hash.new(`#{event}.detail`)
  end

  def method_missing(id, *)
    return @detail[id] if @detail.has_key?(id)

    super
  end
end

end; end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
diamonds-0.1.5 lib/diamonds/opal/browser/event/custom.rb
opal-browser-0.2.0 opal/browser/event/custom.rb