* Selects an option from the list * * @method select * @param {String} elem The element, index, or value to select * @param {Boolean} disabled Selects disabled options * @return {Node} The selected element * @example * var elm = new Drop
( elem, disabled )
| 507 | * elm.select("AL"); // selects & returns option with the value "AL" |
| 508 | */ |
| 509 | select( elem, disabled ) { |
| 510 | var i, index, option, combobox, |
| 511 | select = this.data.select; |
| 512 | |
| 513 | if ( typeof elem === "number" ) { |
| 514 | elem = this.item( elem ); |
| 515 | } |
| 516 | |
| 517 | if ( typeof elem === "string" ) { |
| 518 | for ( i = 0; i < this.length; i++ ) { |
| 519 | if ( this.options[ i ].getAttribute( "data-value" ) === elem ) { |
| 520 | elem = this.options[ i ]; |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | // No element or enabled option |
| 526 | if ( !elem || typeof elem === "string" || |
| 527 | ( !disabled && _.hasClass( elem, "dk-option-disabled" ) ) ) { |
| 528 | return false; |
| 529 | } |
| 530 | |
| 531 | if ( _.hasClass( elem, "dk-option" ) ) { |
| 532 | index = this.options.indexOf( elem ); |
| 533 | option = select.options[ index ]; |
| 534 | |
| 535 | if ( this.multiple ) { |
| 536 | _.toggleClass( elem, "dk-option-selected" ); |
| 537 | option.selected = !option.selected; |
| 538 | |
| 539 | if ( _.hasClass( elem, "dk-option-selected" ) ) { |
| 540 | elem.setAttribute( "aria-selected", "true" ); |
| 541 | this.selectedOptions.push( elem ); |
| 542 | } else { |
| 543 | elem.setAttribute( "aria-selected", "false" ); |
| 544 | index = this.selectedOptions.indexOf( elem ); |
| 545 | this.selectedOptions.splice( index, 1 ); |
| 546 | } |
| 547 | } else { |
| 548 | combobox = this.data.elem.firstChild; |
| 549 | |
| 550 | if ( this.selectedOptions.length ) { |
| 551 | _.removeClass( this.selectedOptions[0], "dk-option-selected" ); |
| 552 | this.selectedOptions[0].setAttribute( "aria-selected", "false" ); |
| 553 | } |
| 554 | |
| 555 | _.addClass( elem, "dk-option-selected" ); |
| 556 | elem.setAttribute( "aria-selected", "true" ); |
| 557 | |
| 558 | combobox.setAttribute( "aria-activedescendant", elem.id ); |
| 559 | combobox.className = "dk-selected " + option.className; |
| 560 | combobox.innerHTML = option.innerHTML; |
| 561 | |
| 562 | this.selectedOptions[0] = elem; |
| 563 | option.selected = true; |
| 564 | } |
| 565 | |
| 566 | this.selectedIndex = select.selectedIndex; |
no test coverage detected