Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Tree.js - Fixed keyboard navigation #214

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 42 additions & 12 deletions src/lib/Tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down