import { Disposable } from '../utils/dispose' ; import { PathUtil } from '../utils/pathUtil' ; export enum NavEditCommands { DISABLE_BACK , ENABLE_BACK , DISABLE_FORWARD , ENABLE_FORWARD } export interface NavResponse { actions : Array < NavEditCommands > ; address ? : string ; } export class PageHistory extends Disposable { private _history = new Array < string > ( ) ; private _backstep = 0 ; private _current_back_enabled = false ; private _current_forward_enabled = false ; returns public get currentCommands ( ) : Array < NavEditCommands > { const action = new Array < NavEditCommands > ( ) ; if ( this . _current_back_enabled ) { action . push ( NavEditCommands . ENABLE_BACK ) ; } else { action . push ( NavEditCommands . DISABLE_BACK ) ; } if ( this . _current_forward_enabled ) { action . push ( NavEditCommands . ENABLE_FORWARD ) ; } else { action . push ( NavEditCommands . DISABLE_FORWARD ) ; } return action ; } description returns { NavResponse } public goForward ( ) { const action = new Array < NavEditCommands > ( ) ; if ( this . _backstep > 0 ) { const path = this . _history [ this . _backstep - 1 ] ; this . _backstep -= 1 ; if ( this . _backstep == 0 ) { action . push ( NavEditCommands . DISABLE_FORWARD ) ; this . _current_forward_enabled = false ; } if ( this . _backstep == this . _history . length - 2 ) { action . push ( NavEditCommands . ENABLE_BACK ) ; this . _current_back_enabled = true ; } return { actions : action , address : path } ; } else { return { actions : action } ; } } description returns { NavResponse } public goBackward ( ) { const action = new Array < NavEditCommands > ( ) ; if ( this . _backstep < this . _history . length - 1 ) { const path = this . _history [ this . _backstep + 1 ] ; this . _backstep += 1 ; if ( this . _backstep == this . _history . length - 1 ) { action . push ( NavEditCommands . DISABLE_BACK ) ; this . _current_back_enabled = false ; } if ( this . _backstep == 1 ) { action . push ( NavEditCommands . ENABLE_FORWARD ) ; this . _current_forward_enabled = true ; } return { actions : action , address : path } ; } else { return { actions : action } ; } } description param { string } address returns { NavResponse | undefined } public addHistory ( address ) { address = PathUtil . ConvertToUnixPath ( address ) ; address = PathUtil . EscapePathParts ( address ) ; const action = new Array < NavEditCommands > ( ) ; if ( this . _backstep < this . _history . length && address == this . _history [ this . _backstep ] ) { return undefined ; } if ( this . _backstep > 0 ) { this . _history = this . _history . slice ( this . _backstep ) ; } if ( this . _history . length == 1 ) { action . push ( NavEditCommands . ENABLE_BACK ) ; this . _current_back_enabled = true ; } this . _history . unshift ( address ) ; this . _backstep = 0 ; action . push ( NavEditCommands . DISABLE_FORWARD ) ; this . _current_forward_enabled = false ; return { actions : action } ; } }