Class: DOM::Event

Inherits:
Object show all
Defined in:
opal/fron/dom/event.rb

Overview

Event

Constant Summary collapse

SPECIAL_KEYS =

Special keys for converions

{
  8 => 'backspace',
  9 => 'tab',
  12 => 'num',
  13 => 'enter',
  16 => 'shift',
  17 => 'ctrl',
  18 => 'alt',
  19 => 'pause',
  20 => 'capslock',
  27 => 'esc',
  32 => 'space',
  33 => 'pageup',
  34 => 'pagedown',
  35 => 'end',
  36 => 'home',
  37 => 'left',
  38 => 'up',
  39 => 'right',
  40 => 'down',
  44 => 'print',
  45 => 'insert',
  46 => 'delete',
  48 => '0',
  49 => '1',
  50 => '2',
  51 => '3',
  52 => '4',
  53 => '5',
  54 => '6',
  55 => '7',
  56 => '8',
  57 => '9',
  65 => 'a',
  66 => 'b',
  67 => 'c',
  68 => 'd',
  69 => 'e',
  70 => 'f',
  71 => 'g',
  72 => 'h',
  73 => 'i',
  74 => 'j',
  75 => 'k',
  76 => 'l',
  77 => 'm',
  78 => 'n',
  79 => 'o',
  80 => 'p',
  81 => 'q',
  82 => 'r',
  83 => 's',
  84 => 't',
  85 => 'u',
  86 => 'v',
  87 => 'w',
  88 => 'x',
  89 => 'y',
  90 => 'z',
  91 => 'cmd',
  92 => 'cmd',
  93 => 'cmd',
  96 => 'num_0',
  97 => 'num_1',
  98 => 'num_2',
  99 => 'num_3',
  100 => 'num_4',
  101 => 'num_5',
  102 => 'num_6',
  103 => 'num_7',
  104 => 'num_8',
  105 => 'num_9',
  106 => 'multiply',
  107 => 'add',
  108 => 'enter',
  109 => 'subtract',
  110 => 'decimal',
  111 => 'divide',
  124 => 'print',
  144 => 'num',
  145 => 'scroll',
  186 => ';',
  187 => '=',
  188 => ',',
  189 => '-',
  190 => '.',
  191 => '/',
  192 => '`',
  219 => '[',
  220 => '\\',
  221 => ']',
  222 => '\'',
  224 => 'cmd',
  57_392 => 'ctrl',
  63_289 => 'num'
}

Instance Method Summary collapse

Constructor Details

#initialize(event) ⇒ Event

Initializes the event

Parameters:

  • event (Event)

    The native event



105
106
107
# File 'opal/fron/dom/event.rb', line 105

def initialize(event)
  @event = event
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object

Runs missing method calls

Parameters:

  • name (String)

    The name of the method



112
113
114
# File 'opal/fron/dom/event.rb', line 112

def method_missing(name)
  `#{@event}[#{name}] || Opal.NIL`
end

Instance Method Details

#alt?Boolan

Returns whether the alt key has been pressed

Returns:

  • (Boolan)

    True if pressed false if not



237
238
239
# File 'opal/fron/dom/event.rb', line 237

def alt?
  `#{@event}.altKey`
end

#buttonNumeric

Returns the index of the pressed mouse button

Returns:



143
144
145
# File 'opal/fron/dom/event.rb', line 143

def button
  `#{@event}.button`
end

#char_codeNumeric

Returns the character code of the pressed key

Returns:



157
158
159
# File 'opal/fron/dom/event.rb', line 157

def char_code
  `#{@event}.charCode`
end

#client_xNumeric

Returns the clientX coordinate of the event

Returns:



223
224
225
# File 'opal/fron/dom/event.rb', line 223

def client_x
  `#{@event}.clientX`
end

#client_yNumeric

Returns the clientY coordinate of the event

Returns:



230
231
232
# File 'opal/fron/dom/event.rb', line 230

def client_y
  `#{@event}.clientY`
end

#ctrl?Boolan

Returns whether the control key has been pressed

Returns:

  • (Boolan)

    True if pressed false if not



251
252
253
# File 'opal/fron/dom/event.rb', line 251

def ctrl?
  `#{@event}.ctrlKey`
end

#dataObject



116
117
118
# File 'opal/fron/dom/event.rb', line 116

def data
  JSON.from_object(`#{@event}.data`)
end

#data_transferObject

Returns the native dataTransfrer object

Returns:



136
137
138
# File 'opal/fron/dom/event.rb', line 136

def data_transfer
  Native `#{@event}.dataTransfer`
end

#default_prevented?Boolean

Returns whether the default was prevented or not

Returns:

  • (Boolean)

    True if it has false if not



178
179
180
# File 'opal/fron/dom/event.rb', line 178

def default_prevented?
  `#{@event}.defaultPrevented`
end

#keyString

Returns the string represenation of the pressed key

Returns:

  • (String)

    The pressed key



123
124
125
126
# File 'opal/fron/dom/event.rb', line 123

def key
  return SPECIAL_KEYS[key_code] if SPECIAL_KEYS[key_code]
  `String.fromCharCode(#{key_code}).toLowerCase()`
end

#key_codeNumeric

Returns the key code of the pressed key

Returns:



164
165
166
# File 'opal/fron/dom/event.rb', line 164

def key_code
  `#{@event}.keyCode`
end

#meta?Boolan

Returns whether the meta (apple) key has been pressed

Returns:

  • (Boolan)

    True if pressed false if not



258
259
260
# File 'opal/fron/dom/event.rb', line 258

def meta?
  `#{@event}.metaKey`
end

#page_xNumeric

Returns the pageX coordinate of the event

Returns:



195
196
197
# File 'opal/fron/dom/event.rb', line 195

def page_x
  `#{@event}.pageX`
end

#page_yNumeric

Returns the pageY coordinate of the event

Returns:



202
203
204
# File 'opal/fron/dom/event.rb', line 202

def page_y
  `#{@event}.pageY`
end

#prevent_defaultObject

Prevents the default action of the event



183
184
185
# File 'opal/fron/dom/event.rb', line 183

def prevent_default
  `#{@event}.preventDefault()`
end

#screen_xNumeric

Returns the screenX coordinate of the event

Returns:



209
210
211
# File 'opal/fron/dom/event.rb', line 209

def screen_x
  `#{@event}.screenX`
end

#screen_yNumeric

Returns the screenY coordinate of the event

Returns:



216
217
218
# File 'opal/fron/dom/event.rb', line 216

def screen_y
  `#{@event}.screenY`
end

#shift?Boolan

Returns whether the shift key has been pressed

Returns:

  • (Boolan)

    True if pressed false if not



244
245
246
# File 'opal/fron/dom/event.rb', line 244

def shift?
  `#{@event}.shiftKey`
end

#stopObject

Stops the event



169
170
171
172
173
# File 'opal/fron/dom/event.rb', line 169

def stop
  prevent_default
  stop_immediate_propagation
  stop_propagation
end

#stop_immediate_propagationObject

Stops the immediate propagation of the event



129
130
131
# File 'opal/fron/dom/event.rb', line 129

def stop_immediate_propagation
  `#{@event}.stopImmediatePropagation()`
end

#stop_propagationObject

Stops the propagation of the event



188
189
190
# File 'opal/fron/dom/event.rb', line 188

def stop_propagation
  `#{@event}.stopPropagation()`
end

#targetDOM::NODE

Returns the target of the event

Returns:



150
151
152
# File 'opal/fron/dom/event.rb', line 150

def target
  DOM::Element.from_node `#{@event}.target`
end