import { TreeNode } from './tree-node' ; export class TreeNodeIterator { constructor ( private _curNode ) { } public next ( ) { if ( this . _curNode . children . length > 0 && this . _curNode . isExpanded ) { this . _curNode = this . _curNode . children [ 0 ] ; } else { let curIndex = this . _curNode . parent . children . indexOf ( this . _curNode ) ; if ( curIndex < this . _curNode . parent . children . length - 1 ) { this . _curNode = this . _curNode . parent . children [ curIndex + 1 ] ; } else if ( this . _curNode . parent . parent ) { let nextAncestor = this . _findNextAncestor ( this . _curNode ) ; if ( nextAncestor ) { this . _curNode = nextAncestor ; } else { return null ; } } else { return null ; } } return this . _curNode ; } public previous ( ) { let curIndex = this . _curNode . parent . children . indexOf ( this . _curNode ) ; if ( curIndex > 0 ) { let prevSibling = this . _curNode . parent . children [ curIndex - 1 ] ; this . _curNode = this . _findLastDescendant ( prevSibling ) ; } else if ( this . _curNode . parent . parent ) { this . _curNode = this . _curNode . parent ; } else { return null ; } return this . _curNode ; } private _findNextAncestor ( curNode ) { if ( curNode . parent . parent ) { let parentIndex = curNode . parent . parent . children . indexOf ( curNode . parent ) ; if ( parentIndex < curNode . parent . parent . children . length - 1 ) { return curNode . parent . parent . children [ parentIndex + 1 ] ; } else { return this . _findNextAncestor ( curNode . parent ) ; } } return null ; } private _findLastDescendant ( node ) { if ( node . isExpanded && node . children . length > 0 ) { return this . _findLastDescendant ( node . children [ node . children . length - 1 ] ) ; } else { return node ; } } }