Class: Browser::Event
Overview
Wrapper for JS events
Instance Method Summary collapse
-
#alt? ⇒ Boolean
True if the Alt key was pressed when this event fired, false otherwise.
-
#code ⇒ Numeric
The key code associated with this event.
-
#ctrl? ⇒ Boolean
True if the Ctrl key was pressed when this event fired, false otherwise.
-
#initialize(native) ⇒ Event
constructor
A new instance of Event.
-
#meta? ⇒ Boolean
True if the Meta/Command/Windows key was pressed when this event fired, false otherwise.
-
#method_missing(name, *args) ⇒ Object
Return properties on the event not covered by Ruby methods.
-
#prevent ⇒ Browser::Event
(also: #prevent_default)
Prevent the runtime from executing this event’s default behavior.
-
#prevented? ⇒ Boolean
(also: #default_prevented?)
True if ‘prevent` has been called on this event, false otherwise.
-
#shift? ⇒ Boolean
True if the Shift key was pressed when this event fired, false otherwise.
-
#stop_propagation ⇒ Object
Prevent the runtime from bubbling this event up the hierarchy.
-
#target ⇒ Browser::Element
The target for this event.
-
#to_n ⇒ JS
The native event wrapped by this object.
Constructor Details
#initialize(native) ⇒ Event
Returns a new instance of Event.
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.
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/browser/event.rb', line 76 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.
57 58 59 |
# File 'lib/browser/event.rb', line 57 def alt? `#@native.altKey` end |
#code ⇒ Numeric
Returns the key code associated with this event. Only useful for keyboard-based events.
71 72 73 |
# File 'lib/browser/event.rb', line 71 def code `#@native.keyCode` end |
#ctrl? ⇒ Boolean
Returns true if the Ctrl key was pressed when this event fired, false otherwise.
51 52 53 |
# File 'lib/browser/event.rb', line 51 def ctrl? `#@native.ctrlKey` end |
#meta? ⇒ Boolean
Returns true if the Meta/Command/Windows key was pressed when this event fired, false otherwise.
39 40 41 |
# File 'lib/browser/event.rb', line 39 def `#@native.metaKey` end |
#prevent ⇒ Browser::Event Also known as: prevent_default
Prevent the runtime from executing this event’s default behavior. For example, prevent navigation after clicking a link.
13 14 15 16 |
# File 'lib/browser/event.rb', line 13 def prevent `#@native.preventDefault()` self end |
#prevented? ⇒ Boolean Also known as: default_prevented?
Returns true if ‘prevent` has been called on this event, false otherwise.
32 33 34 |
# File 'lib/browser/event.rb', line 32 def prevented? `#@native.defaultPrevented` end |
#shift? ⇒ Boolean
Returns true if the Shift key was pressed when this event fired, false otherwise.
45 46 47 |
# File 'lib/browser/event.rb', line 45 def shift? `#@native.shiftKey` end |
#stop_propagation ⇒ Object
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.
25 26 27 28 |
# File 'lib/browser/event.rb', line 25 def stop_propagation `#@native.stopPropagation()` self end |
#target ⇒ Browser::Element
Handle non-DOM events here
The target for this event
65 66 67 |
# File 'lib/browser/event.rb', line 65 def target Element.new(`#@native.target`) end |
#to_n ⇒ JS
Returns the native event wrapped by this object.
90 91 92 |
# File 'lib/browser/event.rb', line 90 def to_n @native end |