Skip to content

Instantly share code, notes, and snippets.

@rexfordkelly
Created August 29, 2016 17:27
Show Gist options
  • Save rexfordkelly/ba2f215d70ca4c7b36a8a502a4d4f68c to your computer and use it in GitHub Desktop.
Save rexfordkelly/ba2f215d70ca4c7b36a8a502a4d4f68c to your computer and use it in GitHub Desktop.
a simple NavTree Data Structure.
/**
A super simple tree structure to make building and
traversing navigation trees stupid simple.
*/
class NavTree {
constructor( label, children ){
this.children = children || []
this.props = {children: this.children, label: label || 'Accordion Menu'}
this._isNavTree = true
this._atChild = -1
}
mount(label, children){
return new NavTree(label, children);
}
set(label, value){
this.props[label] = value
}
get(label){
return this.props[label]
}
getChildren(){
return this.menu_children;
}
toString(){
return JSON.stringify(this.props)
}
addChild( child ){
if( ! child._isNavTree ) return false;
this.children.push( child )
return this.children
}
currentChild(){
return this.children[this._atChild]
}
nextChild(){
if( this._atChild >= this.children.length - 1 ) return false; // stop at end
this._atChild++;
let next = this.children[this._atChild];
next.visited ? next.visited++ : next.visited = 1;
return next || false;
}
prevChild(){
this._atChild--;
if( this._atChild < 0 ) return this._atChild = -1, false
let prev = this.children[this._atChild]
return this.children[this._atChild] || false;
}
}
// export const NavTree = NavTree
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment