Class: Event

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

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

Constructor Details

#initialize(raise_error) ⇒ Event

:nodoc:

Raises:



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

call-seq:

evnt.alt? -> true or false

Returns true if the alt key was depressed during the event, false otherwise.

Document['#example'].listen(:click) {|element, event| puts "alt-clicked" if event.alt? }

alt-clicking element ‘#example’ produces:

alt-clicked

Returns:

  • (Boolean)


80
81
82
# File 'lib/source/redshift/event.rb', line 80

def alt?
  `this.__alt__`
end

#base_typeObject

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

#clientObject

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

#codeObject

call-seq:

evnt.code -> integer or nil

If evnt involved a keystroke, returns the ASCII code of the key pressed, otherwise returns nil.

Document['#example'].listen(:keypress) {|element, event| puts event.code }

typing “testn” into textarea ‘#example produces:

116
101
115
116
13


134
135
136
# File 'lib/source/redshift/event.rb', line 134

def code
  `this.__code__ || nil`
end

#ctrl?Boolean

call-seq:

evnt.ctrl? -> true or false

Returns true if the control key was depressed during the event, false otherwise.

Document['#example'].listen(:click) {|element, event| puts "ctrl-clicked" if event.ctrl? }

ctrl-clicking element ‘#example’ produces:

ctrl-clicked

Returns:

  • (Boolean)


150
151
152
# File 'lib/source/redshift/event.rb', line 150

def ctrl?
  `this.__ctrl__`
end

#keyObject

call-seq:

evnt.key -> symbol or nil

If evnt involved a keystroke, returns a symbol representing the key pressed, otherwise returns nil.

Document['#example'].listen(:keypress) {|element, event| puts event.key.inspect }

typing “testn” into textarea ‘#example produces:

:t
:e
:s
:t
:enter


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

call-seq:

evnt.meta? -> true or false

Returns true if the meta key was depressed during the event, false otherwise.

Document['#example'].listen(:click) {|element, event| puts "meta-clicked" if event.meta? }

meta-clicking element ‘#example’ produces:

meta-clicked

Returns:

  • (Boolean)


196
197
198
# File 'lib/source/redshift/event.rb', line 196

def meta?
  `this.__meta__`
end

#pageObject

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_defaultObject

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

call-seq:

evnt.right_click? -> true or false

Returns true if the event was a right click.

elem = Document['#example'].listen(:click) {|element, event| puts "right-clicked" if event.right_click? }

right-clicking element ‘#example’ produces:

right-clicked

Returns:

  • (Boolean)


241
242
243
# File 'lib/source/redshift/event.rb', line 241

def right_click?
  `this.__right_click__`
end

#shift?Boolean

call-seq:

evnt.shift? -> true or false

Returns true if the shift key was depressed during the event, false otherwise.

Document['#example'].listen(:click) {|element, event| puts "shift-clicked" if event.shift? }

shift-clicking element ‘#example’ produces:

shift-clicked

Returns:

  • (Boolean)


257
258
259
# File 'lib/source/redshift/event.rb', line 257

def shift?
  `this.__shift__`
end

#stop_propagationObject

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

#targetObject

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

#wheelObject

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