Class: Hyalite::ChangeEventPlugin

Inherits:
Object
  • Object
show all
Defined in:
lib/hyalite/event_plugin/change_event_plugin.rb

Constant Summary collapse

EVENT_TYPES =
{
  change: {
    phasedRegistrationNames: {
      bubbled: "onChange",
      captured: "onChangeCapture"
    },
    dependencies: [
      :topBlur,
      :topChange,
      :topClick,
      :topFocus,
      :topInput,
      :topKeyDown,
      :topKeyUp,
      :topSelectionChange
    ]
  }
}
SUPPORTED_INPUT_TYPES =
[
  'color',
  'date',
  'datetime',
  'datetime-local',
  'email',
  'month',
  'number',
  'password',
  'range',
  'search',
  'tel',
  'text',
  'time',
  'url',
  'week'
]

Instance Method Summary collapse

Instance Method Details

#event_typesObject



40
41
42
# File 'lib/hyalite/event_plugin/change_event_plugin.rb', line 40

def event_types
  EVENT_TYPES
end

#extract_event(top_level_type, top_level_target, top_level_target_id, event) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/hyalite/event_plugin/change_event_plugin.rb', line 58

def extract_event(top_level_type, top_level_target, top_level_target_id, event)
  if should_use_change_event(top_level_target)
    target_id = top_level_target_id if top_level_type == :topChange
  elsif is_text_input_element(top_level_target)
    target_id = top_level_target_id if top_level_type == :topInput
  elsif should_use_click_event(top_level_target)
    target_id = top_level_target_id if top_level_type == :topClick
  end

  if target_id
    SyntheticEvent.new(event).tap do |synthetic_event|
      InstanceHandles.traverse_two_phase(target_id) do |target_id, upwards|
        listener = BrowserEvent.listener_at_phase(target_id, EVENT_TYPES[:change], upwards ? :bubbled : :captured)
        synthetic_event.add_listener(listener, target_id) if listener
      end
    end
  end
end

#is_text_input_element(elem) ⇒ Object



44
45
46
47
# File 'lib/hyalite/event_plugin/change_event_plugin.rb', line 44

def is_text_input_element(elem)
  node_name = elem.node_name.downcase
  (node_name == 'input' && SUPPORTED_INPUT_TYPES.include?(elem.input_type)) || node_name == 'textarea'
end

#should_use_change_event(elem) ⇒ Object



49
50
51
52
# File 'lib/hyalite/event_plugin/change_event_plugin.rb', line 49

def should_use_change_event(elem)
  node_name = elem.node_name.downcase
  node_name == 'select' || (node_name == 'input' && elem.input_type == 'file')
end

#should_use_click_event(elem) ⇒ Object



54
55
56
# File 'lib/hyalite/event_plugin/change_event_plugin.rb', line 54

def should_use_click_event(elem)
  elem.node_name.downcase == 'input' && %w(checkbox radio).include?(elem.input_type)
end