Class: Vigilem::DOM::KeyboardEvent

Inherits:
W3C::DOM3::KeyboardEvent show all
Defined in:
lib/vigilem/dom/keyboard_event.rb

Overview

Constant Summary

Constants included from W3C::DOM3::KeyboardEvent::KeyLocations

W3C::DOM3::KeyboardEvent::KeyLocations::DOM_KEY_LOCATION_LEFT, W3C::DOM3::KeyboardEvent::KeyLocations::DOM_KEY_LOCATION_NUMPAD, W3C::DOM3::KeyboardEvent::KeyLocations::DOM_KEY_LOCATION_RIGHT, W3C::DOM3::KeyboardEvent::KeyLocations::DOM_KEY_LOCATION_STANDARD

Constants inherited from W3C::DOM4::Event

W3C::DOM4::Event::AT_TARGET, W3C::DOM4::Event::BUBBLING_PHASE, W3C::DOM4::Event::CAPTURING_PHASE, W3C::DOM4::Event::NONE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from W3C::DOM3::KeyboardEvent

#getModifierState

Methods inherited from W3C::DOM4::Event

#preventDefault, #stopImmediatePropagation, #stopPropagation

Constructor Details

#initialize(type, options = {}) ⇒ KeyboardEvent

@option :modifiers || :modifier_state

Parameters:

  • type (String || Symbol)
  • options (Hash) (defaults to: {})
  • os_specific (Hash)
  • [String] (Hash)

    a customizable set of options

  • [Hash] (Hash)

    a customizable set of options

  • :location (Hash)

    a customizable set of options

  • [TrueClass (Hash)

    a customizable set of options

  • modifiers (Hash)

    a customizable set of options

Raises:

  • (TypeError)

See Also:

  • #SharedKeyboardAndMouseEventInit


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/vigilem/dom/keyboard_event.rb', line 36

def initialize(type, options={})
  raise TypeError, "invalid type `#{type}'" if strict = Vigilem::DOM.strict and not self.class.types.include?(type)
  @type = type
  @key = options[:key] ||= ''
  if options[:location] and strict and (kl = KeyLocations).constants.none? {|const| kl.const_get(const) == options[:location] }
    raise ArgumentError, "invalid location `#{options[:location]}'" 
  end
  @location = options[:location] ||= 0
  @repeat = options[:repeat] ||= false
  @os_specific = self.class.hash_frozen_clone(options[:os_specific] || {})
  
  @modifier_state = self.class.hash_frozen_clone(options[:modifiers] || options[:modifier_state] || {})
  
  mod_dict = self.class.shared_keyboard_and_mouse_event_init(@modifier_state)
  
  super(type, mod_dict.merge(options))
end

Instance Attribute Details

#modifier_stateHash (readonly)

Returns:

  • (Hash)


11
12
13
# File 'lib/vigilem/dom/keyboard_event.rb', line 11

def modifier_state
  @modifier_state
end

#os_specificObject (readonly)

@todo, :chr



11
12
13
# File 'lib/vigilem/dom/keyboard_event.rb', line 11

def os_specific
  @os_specific
end

Class Method Details

.alternative_key_namesArray<Array<Symbol>>

this is not a hash so that the values can be #joined as a regex the last value of each group is the DOM value without the prefix

Returns:

  • (Array<Array<Symbol>>)


113
114
115
116
117
# File 'lib/vigilem/dom/keyboard_event.rb', line 113

def alternative_key_names
  @alternative_key_names ||= [[:alt, :menu, :altKey], [:alt_graph, :AltGraph], [:caps_lock, :CapsLock], [:ctrl, :control, :ctrlKey], 
                              [:Fn], [:fn_lock, :FnLock], [:Hyper], [:meta, :metaKey], [:num_lock, :NumLock], [:OS], 
                              [:scroll_lock, :ScrollLock], [:shift, :shiftKey], [:Super], [:Symbol], [:symbol_lock, :SymbolLock]]
end

.shared_keyboard_and_mouse_event_init_key(key_name) ⇒ Symbol || NilClass

converts a common key to shared_keyboard_and_mouse_event_init_key dom_3 are not prefixed by keyModifierState

Parameters:

Returns:

  • (Symbol || NilClass)


123
124
125
126
127
128
129
130
131
132
# File 'lib/vigilem/dom/keyboard_event.rb', line 123

def shared_keyboard_and_mouse_event_init_key(key_name)
  key_group = alternative_key_names.find {|key_grp| /^(#{key_grp.join('|')})$/i =~ key_name.to_s }
  if key_group
    if @dom_3_attrs.include?(correct_key = key_group.last)
      correct_key 
    else
      "keyModifierState#{correct_key}".to_sym
    end
  end
end

.shared_keyboard_and_mouse_event_init_keysArray<Symbol>

gets the keys for SharedKeyboardAndMouseEventInit

Returns:

  • (Array<Symbol>)


137
138
139
140
141
142
143
144
145
# File 'lib/vigilem/dom/keyboard_event.rb', line 137

def shared_keyboard_and_mouse_event_init_keys
  shared_keyboard_and_mouse_event_init_keys ||= alternative_key_names.map do |key_grp| 
      if @dom_3_attrs.include?(correct_key = key_grp.last) 
        correct_key
      else
        "keyModifierState#{correct_key}".to_sym
      end
    end
end

.SharedKeyboardAndMouseEventInit(hsh_or_ary) ⇒ Hash Also known as: shared_keyboard_and_mouse_event_init

converts common names to SharedKeyboardAndMouseEventInit

Parameters:

  • hsh_or_ary (Hash || Array)

Returns:

  • (Hash)


150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/vigilem/dom/keyboard_event.rb', line 150

def SharedKeyboardAndMouseEventInit(hsh_or_ary)
  
  hsh = if hsh_or_ary.is_a?(Hash)
    hsh_or_ary
  else
    Hash[hsh_or_ary.zip([true] * hsh_or_ary.size)]
  end
  
  alternative_key_names.each_with_object({}) do |key_grp, out_hash|
    skamei_name = shared_keyboard_and_mouse_event_init_key(key_grp.last)
    matched_usr_key = hsh.keys.find {|usr_key| usr_key =~ /^(#{key_grp.join('|')})$/i }
    out_hash[skamei_name] = !!hsh[matched_usr_key]
  end
end

.typesArray<String>

Returns:

  • (Array<String>)


106
107
108
# File 'lib/vigilem/dom/keyboard_event.rb', line 106

def types
  @types ||= %w(keyup keypress keydown)
end

Instance Method Details

#copy_to(key_type) ⇒ KeyboardEvent

Parameters:

Returns:



84
85
86
87
88
# File 'lib/vigilem/dom/keyboard_event.rb', line 84

def copy_to(key_type)
  copy = Support::Utils.deep_dup(self)
  copy.instance_variable_set(:@type, key_type.to_s)
  copy
end

#inspectString

Returns:

  • (String)


98
99
100
# File 'lib/vigilem/dom/keyboard_event.rb', line 98

def inspect
  "<#{self.class}:0x#{self.object_id} #{instance_variables.map {|var| "#{var}=#{instance_variable_get(var).inspect}" }.sort_by {|name| name[1..2]}.join(' ')}>"
end

#to_sString

has @type @key @location, so the object can be distinguised from other KeyboardEvents

Returns:

  • (String)


92
93
94
# File 'lib/vigilem/dom/keyboard_event.rb', line 92

def to_s
  "#{super().chop} @type=#{@type.inspect} @key=#{@key.inspect} @location=#{location.inspect}>"
end