46 lines
1.6 KiB
JavaScript
46 lines
1.6 KiB
JavaScript
import { asArray } from './array';
|
|
import { parentAutofocusables } from './DOMutils';
|
|
var getParents = function (node, parents) {
|
|
if (parents === void 0) { parents = []; }
|
|
parents.push(node);
|
|
if (node.parentNode) {
|
|
getParents(node.parentNode, parents);
|
|
}
|
|
return parents;
|
|
};
|
|
export var getCommonParent = function (nodeA, nodeB) {
|
|
var parentsA = getParents(nodeA);
|
|
var parentsB = getParents(nodeB);
|
|
for (var i = 0; i < parentsA.length; i += 1) {
|
|
var currentParent = parentsA[i];
|
|
if (parentsB.indexOf(currentParent) >= 0) {
|
|
return currentParent;
|
|
}
|
|
}
|
|
return false;
|
|
};
|
|
export var getTopCommonParent = function (baseActiveElement, leftEntry, rightEntries) {
|
|
var activeElements = asArray(baseActiveElement);
|
|
var leftEntries = asArray(leftEntry);
|
|
var activeElement = activeElements[0];
|
|
var topCommon = false;
|
|
leftEntries.filter(Boolean).forEach(function (entry) {
|
|
topCommon = getCommonParent(topCommon || entry, entry) || topCommon;
|
|
rightEntries.filter(Boolean).forEach(function (subEntry) {
|
|
var common = getCommonParent(activeElement, subEntry);
|
|
if (common) {
|
|
if (!topCommon || common.contains(topCommon)) {
|
|
topCommon = common;
|
|
}
|
|
else {
|
|
topCommon = getCommonParent(common, topCommon);
|
|
}
|
|
}
|
|
});
|
|
});
|
|
return topCommon;
|
|
};
|
|
export var allParentAutofocusables = function (entries) {
|
|
return entries.reduce(function (acc, node) { return acc.concat(parentAutofocusables(node)); }, []);
|
|
};
|