Class: Dommy::HTMLOptionsCollection

Inherits:
HTMLCollection show all
Defined in:
lib/dommy/html_collection.rb

Overview

‘HTMLOptionsCollection` — specialized `<select>.options` collection. Adds `add(option, before?)`, `remove(index)`, the `selectedIndex` getter/setter, and a `length=` setter that truncates or extends.

Live, like the parent class. Constructed by ‘HTMLSelectElement` and passed its owner; mutations route through the owner’s tree.

Instance Method Summary collapse

Methods inherited from HTMLCollection

#[], #__js_named_props__, #each, elements_by_tag_name_ns, #empty?, #first, #item, #last, #length, #named_item, #to_a

Methods included from Bridge::Methods

included

Constructor Details

#initialize(owner, &compute) ⇒ HTMLOptionsCollection

Returns a new instance of HTMLOptionsCollection.



168
169
170
171
# File 'lib/dommy/html_collection.rb', line 168

def initialize(owner, &compute)
  super(&compute)
  @owner = owner
end

Instance Method Details

#__js_call__(method, args) ⇒ Object



249
250
251
252
253
254
255
256
257
258
# File 'lib/dommy/html_collection.rb', line 249

def __js_call__(method, args)
  case method
  when "add"
    add(args[0], args[1])
  when "remove"
    remove(args[0])
  else
    super
  end
end

#__js_get__(key) ⇒ Object



225
226
227
228
229
230
231
232
# File 'lib/dommy/html_collection.rb', line 225

def __js_get__(key)
  case key
  when "selectedIndex"
    selected_index
  else
    super
  end
end

#__js_set__(key, value) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/dommy/html_collection.rb', line 234

def __js_set__(key, value)
  case key
  when "selectedIndex"
    self.selected_index = value
  when "length"
    self.length = value
  else
    return Bridge::UNHANDLED
  end

  nil
end

#add(option, before = nil) ⇒ Object

Append (or insert before ‘before`) an option element. `before` accepts either another option (insert before that node) or an integer index. Strings/`null` append.



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/dommy/html_collection.rb', line 176

def add(option, before = nil)
  return nil unless option.respond_to?(:__dommy_backend_node__)

  case before
  when nil
    @owner.append_child(option)
  when Integer
    anchor = item(before)
    anchor ? @owner.insert_before(option, anchor) : @owner.append_child(option)
  else
    if before.respond_to?(:__dommy_backend_node__)
      @owner.insert_before(option, before)
    else
      @owner.append_child(option)
    end
  end

  nil
end

#length=(new_length) ⇒ Object

Setter mirrors ‘<select>.options.length = n` — destructive resize. Shrinks by removing trailing options, grows by appending blank `<option>`s. Real browsers do the same.



213
214
215
216
217
218
219
220
221
222
223
# File 'lib/dommy/html_collection.rb', line 213

def length=(new_length)
  n = new_length.to_i
  current = to_a
  if n < current.length
    current[n..].each(&:remove)
  elsif n > current.length
    (n - current.length).times { @owner.append_child(@owner.document.create_element("option")) }
  end

  n
end

#remove(index) ⇒ Object



196
197
198
199
200
# File 'lib/dommy/html_collection.rb', line 196

def remove(index)
  target = item(index)
  target&.remove
  nil
end

#selected_indexObject



202
203
204
# File 'lib/dommy/html_collection.rb', line 202

def selected_index
  @owner.selected_index
end

#selected_index=(value) ⇒ Object



206
207
208
# File 'lib/dommy/html_collection.rb', line 206

def selected_index=(value)
  @owner.selected_index = value
end