From 66058223c68b98a21778d2532cbcb25b6d4a1897 Mon Sep 17 00:00:00 2001 From: Vladimir <43984616+Vladimir-Yakovlev@users.noreply.github.com> Date: Wed, 15 Jul 2020 15:11:56 +0300 Subject: [PATCH] Update Tree.js Fixed keyboard navigation: * on complex trees with 3+ levels and more than 1 child on each level; * on trees with 3+ levels with hidden nodes e.g. when using filter. --- src/lib/Tree.js | 54 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 12 deletions(-) diff --git a/src/lib/Tree.js b/src/lib/Tree.js index f8d0452..9e5e1a0 100644 --- a/src/lib/Tree.js +++ b/src/lib/Tree.js @@ -485,17 +485,36 @@ export default class Tree { } nextVisibleNode (node) { + const getVisibleSelftOrNextSibling = function (self) { + let n = self + while (n && n.hidden()) { + n = n.next() + } + return n + } + if (node.hasChildren() && node.expanded()) { - return node.first() + const visibleChild = getVisibleSelftOrNextSibling(node.first()) + if (visibleChild) { + return visibleChild + } } - const nextNode = this.nextNode(node) + const nextVisibleSibling = getVisibleSelftOrNextSibling(node.next()) + if (nextVisibleSibling) { + return nextVisibleSibling + } - if (!nextNode && node.parent) { - return node.parent.next() + let ancestor = node.parent + while (ancestor) { + var ancestorsNextVisibleSibling = getVisibleSelftOrNextSibling(ancestor.next()) + if (ancestorsNextVisibleSibling) { + return ancestorsNextVisibleSibling + } + ancestor = ancestor.parent } - return nextNode + return null } prevNode (node) { @@ -505,17 +524,28 @@ export default class Tree { } prevVisibleNode (node) { - const prevNode = this.prevNode(node) - - if (!prevNode) { - return node.parent + const getVisibleSelfOrPrevSibling = function (self) { + let n = self + while (n && n.hidden()) { + n = n.prev() + } + return n + } + const getLastVisibleDescendantOrSelf = function (self) { + let n = self + while (n && n.hasChildren() && n.expanded()) { + n = getVisibleSelfOrPrevSibling(n.last()) + } + return n } - if (prevNode.hasChildren() && prevNode.expanded()) { - return prevNode.last() + const prevVisibleSibling = getVisibleSelfOrPrevSibling(node.prev()) + const prevVisibleDescendantOfSameParent = getLastVisibleDescendantOrSelf(prevVisibleSibling) + if (prevVisibleDescendantOfSameParent) { + return prevVisibleDescendantOfSameParent } - return prevNode + return node.parent } addToModel (node, index = this.model.length) {