Class: Browser::Event

Inherits:
Object show all
Defined in:
lib/browser/event.rb

Overview

Wrapper for JS events

Instance Method Summary collapse

Constructor Details

#initialize(native) ⇒ Event

Returns a new instance of Event.

Parameters:

  • the (JS)

    native event to wrap



5
6
7
# File 'lib/browser/event.rb', line 5

def initialize native
  @native = native
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Return properties on the event not covered by Ruby methods.



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/browser/event.rb', line 74

def method_missing name, *args
  property = name.gsub(/_[a-z]/) { |match| match[-1, 1].upcase }
  value = `#@native[property]`

  if `!!value && #{Proc === value}`
    value.call(*args)
  elsif `value == null`
    nil
  else
    value
  end
end

Instance Method Details

#alt?Boolean

Returns true if the Alt key was pressed when this event fired, false otherwise.

Returns:

  • (Boolean)

    true if the Alt key was pressed when this event fired, false otherwise



55
56
57
# File 'lib/browser/event.rb', line 55

def alt?
  `#@native.altKey`
end

#codeNumeric

Returns the key code associated with this event. Only useful for keyboard-based events.

Returns:

  • (Numeric)

    the key code associated with this event. Only useful for keyboard-based events.



69
70
71
# File 'lib/browser/event.rb', line 69

def code
  `#@native.keyCode`
end

#ctrl?Boolean

Returns true if the Ctrl key was pressed when this event fired, false otherwise.

Returns:

  • (Boolean)

    true if the Ctrl key was pressed when this event fired, false otherwise



49
50
51
# File 'lib/browser/event.rb', line 49

def ctrl?
  `#@native.ctrlKey`
end

#meta?Boolean

Returns true if the Meta/Command/Windows key was pressed when this event fired, false otherwise.

Returns:

  • (Boolean)

    true if the Meta/Command/Windows key was pressed when this event fired, false otherwise



37
38
39
# File 'lib/browser/event.rb', line 37

def meta?
  `#@native.metaKey`
end

#preventBrowser::Event

Prevent the runtime from executing this event’s default behavior. For example, prevent navigation after clicking a link.

Returns:



13
14
15
16
# File 'lib/browser/event.rb', line 13

def prevent
  `#@native.preventDefault()`
  self
end

#prevented?Boolean

Returns true if ‘prevent` has been called on this event, false otherwise.

Returns:

  • (Boolean)

    true if ‘prevent` has been called on this event, false otherwise



31
32
33
# File 'lib/browser/event.rb', line 31

def prevented?
  `#@native.defaultPrevented`
end

#shift?Boolean

Returns true if the Shift key was pressed when this event fired, false otherwise.

Returns:

  • (Boolean)

    true if the Shift key was pressed when this event fired, false otherwise



43
44
45
# File 'lib/browser/event.rb', line 43

def shift?
  `#@native.shiftKey`
end

#stop_propagationObject

Prevent the runtime from bubbling this event up the hierarchy. This is typically used to keep an event local to the element on which it was triggered, such as keeping a click event on a button from unintentionally triggering a handler on a parent element.

Returns:

  • self



24
25
26
27
# File 'lib/browser/event.rb', line 24

def stop_propagation
  `#@native.stopPropagation()`
  self
end

#targetBrowser::Element

TODO:

Handle non-DOM events here

The target for this event

Returns:



63
64
65
# File 'lib/browser/event.rb', line 63

def target
  Element.new(`#@native.target`)
end

#to_nJS

Returns the native event wrapped by this object.

Returns:

  • (JS)

    the native event wrapped by this object.



88
89
90
# File 'lib/browser/event.rb', line 88

def to_n
  @native
end