* Adds an element to the select. This option will not only add it to the original * select, but create a Dropkick option and add it to the Dropkick select. * * @method add * @param {string} elem HTMLOptionElement * @param {Node/Integer} before HTMLOptionElement/Index of Element *
( elem, before )
| 244 | * select.add("New option", 5); |
| 245 | */ |
| 246 | add( elem, before ) { |
| 247 | var text, option, i; |
| 248 | |
| 249 | if ( typeof elem === "string" ) { |
| 250 | text = elem; |
| 251 | elem = document.createElement("option"); |
| 252 | elem.text = text; |
| 253 | } |
| 254 | |
| 255 | if ( elem.nodeName === "OPTION" ) { |
| 256 | option = _.create( "li", { |
| 257 | "class": "dk-option", |
| 258 | "data-value": elem.value, |
| 259 | "text": elem.text, |
| 260 | "innerHTML": elem.innerHTML, |
| 261 | "role": "option", |
| 262 | "aria-selected": "false", |
| 263 | "id": "dk" + this.data.cacheID + "-" + ( elem.id || elem.value.replace( " ", "-" ) ) |
| 264 | }); |
| 265 | |
| 266 | _.addClass( option, elem.className ); |
| 267 | this.length += 1; |
| 268 | |
| 269 | if ( elem.disabled ) { |
| 270 | _.addClass( option, "dk-option-disabled" ); |
| 271 | option.setAttribute( "aria-disabled", "true" ); |
| 272 | } |
| 273 | |
| 274 | if ( elem.hidden ) { |
| 275 | _.addClass( option, "dk-option-hidden" ); |
| 276 | option.setAttribute( "aria-hidden", "true" ); |
| 277 | } |
| 278 | |
| 279 | this.data.select.add( elem, before ); |
| 280 | |
| 281 | if ( typeof before === "number" ) { |
| 282 | before = this.item( before ); |
| 283 | } |
| 284 | |
| 285 | i = this.options.indexOf( before ); |
| 286 | |
| 287 | if ( i > -1 ) { |
| 288 | before.parentNode.insertBefore( option, before ); |
| 289 | this.options.splice( i, 0, option ); |
| 290 | } else { |
| 291 | this.data.elem.lastChild.appendChild( option ); |
| 292 | this.options.push( option ); |
| 293 | } |
| 294 | |
| 295 | option.addEventListener( "mouseover", this ); |
| 296 | |
| 297 | if ( elem.selected ) { |
| 298 | this.select( i ); |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | /** |
no test coverage detected