Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
fix(select): keyboard controls in IE
Browse files Browse the repository at this point in the history
closes #1628
  • Loading branch information
rschmukler committed Feb 24, 2015
1 parent c5c5f86 commit 69053a3
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/components/select/select.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ angular.module('material.components.select', [
.directive('mdOptgroup', OptgroupDirective)
.provider('$mdSelect', SelectProvider);


/**
* @ngdoc directive
* @name mdSelect
Expand Down Expand Up @@ -326,7 +327,7 @@ function SelectMenuDirective($parse, $mdUtil, $mdTheming) {
};

self.selectedLabels = function() {
var selectedOptionEls = Array.prototype.slice.call($element[0].querySelectorAll('md-option[selected]'));
var selectedOptionEls = nodesToArray($element[0].querySelectorAll('md-option[selected]'));
if (selectedOptionEls.length) {
return selectedOptionEls.map(function(el) { return el.textContent; }).join(', ');
} else {
Expand Down Expand Up @@ -528,8 +529,7 @@ function SelectProvider($$interimElementProvider) {
backdrop: opts.hasBackdrop && angular.element('<md-backdrop class="md-select-backdrop">')
});

var optionNodes = opts.selectEl[0].getElementsByTagName('md-option');
var arrayIndexOf = [].indexOf;
var optionNodes = [];

configureAria();

Expand All @@ -542,6 +542,7 @@ function SelectProvider($$interimElementProvider) {
// Don't go forward if the select has been removed in this time...
if (opts.isRemoved) return;
animateSelect(scope, element, opts);
optionNodes = nodesToArray(opts.selectEl[0].getElementsByTagName('md-option'));
});
});
});
Expand Down Expand Up @@ -570,6 +571,7 @@ function SelectProvider($$interimElementProvider) {
$$rAF(function() {
if (opts.isRemoved) return;
animateSelect(scope, element, opts);
optionNodes = nodesToArray(element[0].querySelectorAll('md-option'));
});
});

Expand Down Expand Up @@ -614,7 +616,7 @@ function SelectProvider($$interimElementProvider) {

function focusNextOption() {
var index;
if ((index = arrayIndexOf.call(optionNodes, opts.focusedNode)) == -1) {
if ((index = optionNodes.indexOf(opts.focusedNode)) == -1) {

This comment has been minimized.

Copy link
@ajoslin

ajoslin Feb 26, 2015

Contributor

This doesn't allow the case that md-options could dynamically change while the select is opened. Since we turn optionNodes into an array, it won't live-update anymore.

We should just go back to querySelectorAll in the focusNext and focusPrev.

This comment has been minimized.

Copy link
@gkalpak

gkalpak Feb 27, 2015

Member

I wonder if it would make sense to replace querySelectorAll() above with getElementsByTagName() (which returns a live list).

// We lost the previously focused element, reset to middle
index = Math.floor( (optionNodes.length - 1) / 2 );
} else {
Expand All @@ -625,7 +627,7 @@ function SelectProvider($$interimElementProvider) {
}
function focusPrevOption() {
var index;
if ((index = arrayIndexOf.call(optionNodes, opts.focusedNode)) == -1) {
if ((index = optionNodes.indexOf(opts.focusedNode)) == -1) {
// We lost the previously focused element, reset to middle
index = Math.floor( (optionNodes.length - 1) / 2 );
} else {
Expand Down Expand Up @@ -697,7 +699,7 @@ function SelectProvider($$interimElementProvider) {
maxWidth = parentRect.width - SELECT_EDGE_MARGIN * 2,
isScrollable = contentNode.scrollHeight > contentNode.offsetHeight,
selectedNode = selectNode.querySelector('md-option[selected]'),
optionNodes = selectNode.getElementsByTagName('md-option'),
optionNodes = nodesToArray(selectNode.getElementsByTagName('md-option')),
optgroupNodes = selectNode.getElementsByTagName('md-optgroup');

var centeredNode;
Expand Down Expand Up @@ -810,4 +812,13 @@ function SelectProvider($$interimElementProvider) {
}
}

// Annoying method to copy nodes to an array, thanks to IE
function nodesToArray(nodes) {
var results = [];
for (var i = 0; i < nodes.length; ++i) {
results.push(nodes.item(i));
}
return results;
}
})();

0 comments on commit 69053a3

Please sign in to comment.