Class: Dommy::HTMLOptionsCollection
- Inherits:
-
HTMLCollection
- Object
- HTMLCollection
- Dommy::HTMLOptionsCollection
- 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
- #__js_call__(method, args) ⇒ Object
- #__js_get__(key) ⇒ Object
- #__js_set__(key, value) ⇒ Object
-
#add(option, before = nil) ⇒ Object
Append (or insert before ‘before`) an option element.
-
#initialize(owner, &compute) ⇒ HTMLOptionsCollection
constructor
A new instance of HTMLOptionsCollection.
-
#length=(new_length) ⇒ Object
Setter mirrors ‘<select>.options.length = n` — destructive resize.
- #remove(index) ⇒ Object
- #selected_index ⇒ Object
- #selected_index=(value) ⇒ Object
Methods inherited from HTMLCollection
#[], #each, #empty?, #first, #item, #last, #length, #named_item, #to_a
Constructor Details
#initialize(owner, &compute) ⇒ HTMLOptionsCollection
Returns a new instance of HTMLOptionsCollection.
119 120 121 122 |
# File 'lib/dommy/html_collection.rb', line 119 def initialize(owner, &compute) super(&compute) @owner = owner end |
Instance Method Details
#__js_call__(method, args) ⇒ Object
196 197 198 199 200 201 202 203 204 205 |
# File 'lib/dommy/html_collection.rb', line 196 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
176 177 178 179 180 181 182 183 |
# File 'lib/dommy/html_collection.rb', line 176 def __js_get__(key) case key when "selectedIndex" selected_index else super end end |
#__js_set__(key, value) ⇒ Object
185 186 187 188 189 190 191 192 193 194 |
# File 'lib/dommy/html_collection.rb', line 185 def __js_set__(key, value) case key when "selectedIndex" self.selected_index = value when "length" self.length = value 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.
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/dommy/html_collection.rb', line 127 def add(option, before = nil) return nil unless option.respond_to?(:__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?(:__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.
164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/dommy/html_collection.rb', line 164 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
147 148 149 150 151 |
# File 'lib/dommy/html_collection.rb', line 147 def remove(index) target = item(index) target&.remove nil end |
#selected_index ⇒ Object
153 154 155 |
# File 'lib/dommy/html_collection.rb', line 153 def selected_index @owner.selected_index end |
#selected_index=(value) ⇒ Object
157 158 159 |
# File 'lib/dommy/html_collection.rb', line 157 def selected_index=(value) @owner.selected_index = value end |