This repository has been archived by the owner on Aug 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(select): keyboard controls in IE
closes #1628
- Loading branch information
1 parent
c5c5f86
commit 69053a3
Showing
1 changed file
with
17 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ angular.module('material.components.select', [ | |
.directive('mdOptgroup', OptgroupDirective) | ||
.provider('$mdSelect', SelectProvider); | ||
|
||
|
||
/** | ||
* @ngdoc directive | ||
* @name mdSelect | ||
|
@@ -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 { | ||
|
@@ -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(); | ||
|
||
|
@@ -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')); | ||
}); | ||
}); | ||
}); | ||
|
@@ -570,6 +571,7 @@ function SelectProvider($$interimElementProvider) { | |
$$rAF(function() { | ||
if (opts.isRemoved) return; | ||
animateSelect(scope, element, opts); | ||
optionNodes = nodesToArray(element[0].querySelectorAll('md-option')); | ||
}); | ||
}); | ||
|
||
|
@@ -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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
gkalpak
Member
|
||
// We lost the previously focused element, reset to middle | ||
index = Math.floor( (optionNodes.length - 1) / 2 ); | ||
} else { | ||
|
@@ -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 { | ||
|
@@ -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; | ||
|
@@ -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; | ||
} | ||
})(); | ||
|
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.