Module: Bewildr::ControlTypeAdditions::ComboBoxAdditions

Defined in:
lib/bewildr/control_type_additions/combo_box_additions.rb

Instance Method Summary collapse

Instance Method Details

#collapse_comboObject

Collapses the combobox



76
77
78
79
80
81
# File 'lib/bewildr/control_type_additions/combo_box_additions.rb', line 76

def collapse_combo
  collapse
  Timeout.timeout(30) do
    sleep 0.2 until expand_state == :collapsed
  end
end

#countObject

Returns the number of items in the combobox



22
23
24
# File 'lib/bewildr/control_type_additions/combo_box_additions.rb', line 22

def count
  items.size
end

#expand_comboObject

Expands the combobox



68
69
70
71
72
73
# File 'lib/bewildr/control_type_additions/combo_box_additions.rb', line 68

def expand_combo
  expand
  Timeout.timeout(30) do
    sleep 0.2 until expand_state == :expanded
  end
end

#itemsObject

Returns a string array containing the element’s item names



7
8
9
# File 'lib/bewildr/control_type_additions/combo_box_additions.rb', line 7

def items
  list_items.collect {|item| item.name}
end

#list_itemsObject

Returns an array containing the combobox items



12
13
14
15
16
17
18
19
# File 'lib/bewildr/control_type_additions/combo_box_additions.rb', line 12

def list_items
  begin
    expand_combo
    get(:type => :list_item, :scope => :children, :how_many => :all)
  ensure
    collapse_combo
  end
end

#select(input) ⇒ Object

Selects a combobox item. Takes a string (and selects the first item whose name matches) or an integer and selects the respective element



27
28
29
30
31
32
33
# File 'lib/bewildr/control_type_additions/combo_box_additions.rb', line 27

def select(input)
  case input
  when String then select_by_name(input)
  when Integer then select_by_index(input)
  else raise ArgumentError, "Select by name or by index"
  end
end

#select_by_index(input) ⇒ Object

Selects the item at the supplied position



48
49
50
51
52
53
54
55
56
# File 'lib/bewildr/control_type_additions/combo_box_additions.rb', line 48

def select_by_index(input)
  raise "Index must be 0 or greater" if input < 0
  begin
    expand_combo
    list_items[input].select
  ensure
    collapse_combo
  end
end

#select_by_name(input) ⇒ Object

Selects the first item whose name matches the input



36
37
38
39
40
41
42
43
44
45
# File 'lib/bewildr/control_type_additions/combo_box_additions.rb', line 36

def select_by_name(input)
  begin
    expand_combo
    my_item = list_items.find {|item| item.name == input}
    raise Bewildr::NoSuchItemInComboBox if my_item.nil?
    my_item.select
  ensure
    collapse_combo
  end
end

#selectedObject

Returns the selected combobox item



59
60
61
62
63
64
65
# File 'lib/bewildr/control_type_additions/combo_box_additions.rb', line 59

def selected
  #TODO: find a way to not need to expand and collapse before getting the selected item
  expand_combo
  collapse_combo
  #get_selection.first
  get_selection.first
end