Class: Event
Overview
Event
objects represent user interactions with the browser. Attempting to create an Event
object by calling Event.new
results in NewEventError
.
Event
objects are handled by the methods in the UserEvents
module.
Defined Under Namespace
Classes: NewEventError
Constant Summary collapse
- KEYS =
{ 8 => :backspace, 9 => :tab, 13 => :enter, 27 => :esc, 32 => :space, 37 => :left, 38 => :up, 39 => :right, 40 => :down, 46 => :delete }
Instance Method Summary collapse
-
#alt? ⇒ Boolean
call-seq: evnt.alt? -> true or false.
-
#base_type ⇒ Object
call-seq: evnt.base_type -> symbol.
-
#client ⇒ Object
call-seq: evnt.client -> => integer, :y => integer.
-
#code ⇒ Object
call-seq: evnt.code -> integer or nil.
-
#ctrl? ⇒ Boolean
call-seq: evnt.ctrl? -> true or false.
-
#initialize(raise_error) ⇒ Event
constructor
:nodoc:.
-
#key ⇒ Object
call-seq: evnt.key -> symbol or nil.
-
#kill! ⇒ Object
call-seq: evnt.kill! -> evnt.
-
#meta? ⇒ Boolean
call-seq: evnt.meta? -> true or false.
-
#page ⇒ Object
call-seq: evnt.page -> => numeric, :y => numeric.
-
#prevent_default ⇒ Object
call-seq: evnt.prevent_default -> evnt.
-
#right_click? ⇒ Boolean
call-seq: evnt.right_click? -> true or false.
-
#shift? ⇒ Boolean
call-seq: evnt.shift? -> true or false.
-
#stop_propagation ⇒ Object
call-seq: evnt.stop_propagation -> evnt.
-
#target ⇒ Object
call-seq: evnt.target -> element.
-
#wheel ⇒ Object
call-seq: evnt.wheel -> numeric or nil.
Constructor Details
#initialize(raise_error) ⇒ Event
:nodoc:
64 65 66 |
# File 'lib/source/redshift/event.rb', line 64 def initialize(raise_error) # :nodoc: raise(NewEventError, 'Events can only be initialized by user interactions with the browser') unless `raise_error === null` end |
Instance Method Details
#alt? ⇒ Boolean
80 81 82 |
# File 'lib/source/redshift/event.rb', line 80 def alt? `this.__alt__` end |
#base_type ⇒ Object
call-seq:
evnt.base_type -> symbol
Returns a symbol representing evnt’s event type, or :base
type if evnt is a defined event.
UserEvents.define(:shift_click, :base => 'click', :condition => proc {|element,event| event.shift? })
Document['#example'].listen(:click) {|element, event| puts event.base_type }
Document['#example'].listen(:shift_click) {|element, event| puts event.base_type }
clicking or shift-clicking on element ‘#example’ produces:
click
98 99 100 |
# File 'lib/source/redshift/event.rb', line 98 def base_type `$s(this.__type__)` end |
#client ⇒ Object
call-seq:
evnt.client -> {:x => integer, :y => integer}
Returns a hash representing evnt’s distance in pixels from the left (x) and top (y) edges of the browser viewport.
Document['#example'].listen(:click) {|element,event| puts event.client.inspect }
clicking element ‘#example’ at position (35,45) produces:
{:x => 35, :y => 45}
114 115 116 |
# File 'lib/source/redshift/event.rb', line 114 def client {:x => `this.__client__.x`, :y => `this.__client__.y`} end |
#code ⇒ Object
134 135 136 |
# File 'lib/source/redshift/event.rb', line 134 def code `this.__code__ || nil` end |
#ctrl? ⇒ Boolean
150 151 152 |
# File 'lib/source/redshift/event.rb', line 150 def ctrl? `this.__ctrl__` end |
#key ⇒ Object
170 171 172 |
# File 'lib/source/redshift/event.rb', line 170 def key `this.__key__ || nil` end |
#kill! ⇒ Object
call-seq:
evnt.kill! -> evnt
Stops the event in place, preventing the default action as well as any further propagation, then returns evnt.
180 181 182 |
# File 'lib/source/redshift/event.rb', line 180 def kill! self.stop_propagation.prevent_default end |
#meta? ⇒ Boolean
196 197 198 |
# File 'lib/source/redshift/event.rb', line 196 def `this.__meta__` end |
#page ⇒ Object
call-seq:
evnt.page -> {:x => numeric, :y => numeric}
Returns a hash representing evnt’s distance in pixels from the left (x) and top (y) edges of the current page, including pixels that may have scrolled out of view.
Document['#example'].listen(:click) {|element,event| puts event.page.inspect }
clicking element ‘#example’ at position (35,45) after scrolling down 100 pixels produces:
{:x => 35, :y => 145}
214 215 216 |
# File 'lib/source/redshift/event.rb', line 214 def page {:x => `this.__page__.x`, :y => `this.__page__.y`} end |
#prevent_default ⇒ Object
call-seq:
evnt.prevent_default -> evnt
Instructs the event to abandon its default browser action, then returns evnt.
224 225 226 227 228 |
# File 'lib/source/redshift/event.rb', line 224 def prevent_default `var Native = this.__native__` `Native.preventDefault?Native.preventDefault():Native.returnValue=false` return self end |
#right_click? ⇒ Boolean
241 242 243 |
# File 'lib/source/redshift/event.rb', line 241 def right_click? `this.__right_click__` end |
#shift? ⇒ Boolean
257 258 259 |
# File 'lib/source/redshift/event.rb', line 257 def shift? `this.__shift__` end |
#stop_propagation ⇒ Object
call-seq:
evnt.stop_propagation -> evnt
Instructs the event to stop propagating, then returns evnt.
266 267 268 269 270 |
# File 'lib/source/redshift/event.rb', line 266 def stop_propagation `var Native = this.__native__` `Native.stopPropagation?Native.stopPropagation():Native.cancelBubble=true` return self end |
#target ⇒ Object
call-seq:
evnt.target -> element
Returns the DOM element targeted by evnt, or nil
if no element was targeted. The target of an event may be a different element than elem.
elem = Document['#outer']
elem.listen :click do |element, event|
puts "%s was clicked" % event.target.inspect
puts "%s was indirectly clicked" % element.inspect
end
clicking element ‘#inner’ inside ‘#outer’ produces:
#<Element: DIV id="inner"> was clicked
#<Element: DIV id="outer"> was indirectly clicked
290 291 292 |
# File 'lib/source/redshift/event.rb', line 290 def target `$E(this.__target__)` end |
#wheel ⇒ Object
call-seq:
evnt.wheel -> numeric or nil
Returns a floating point number representing the velocity of the wheel movement executed during evnt. Positive values indicate upward scrolling, negative values indicate downward scrolling. Returns nil
if evnt did not involve the mouse wheel.
Document['#example'].listen(:mouse_wheel) {|element, event| puts event.wheel }
wheeling the mousewheel downward by a single “click” over element ‘#example’ produces:
-1
309 310 311 |
# File 'lib/source/redshift/event.rb', line 309 def wheel `this.__wheel__` end |