UNPKG

aspen-tree-model

Version:
94 lines 4.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TreeStateWatcher = void 0; const notificar_1 = require("notificar"); const types_1 = require("./types"); class TreeStateWatcher { constructor(treeState, atSurfaceExpandedDirsOnly = false) { this.treeState = treeState; this.atSurfaceExpandedDirsOnly = atSurfaceExpandedDirsOnly; this.events = new notificar_1.Notificar(); this._disposed = false; this.disposables = new notificar_1.DisposablesComposite(); this.currentState = { specVersion: 1, scrollPosition: 0, expandedDirectories: { atSurface: [], buried: [], }, }; this.disposables.add(treeState.onChangeScrollOffset((newOffset) => { this.currentState.scrollPosition = newOffset; this.events.dispatch(types_1.TreeStateEvent.DidChange, types_1.TreeStateWatcherChangeType.ScrollOffset); })); this.disposables.add(treeState.onDidChangeRelativePath((prevPath, newPath) => { let shouldNotify = false; const atSurfaceIdx = this.currentState.expandedDirectories.atSurface.indexOf(prevPath); if (atSurfaceIdx > -1) { this.currentState.expandedDirectories.atSurface[atSurfaceIdx] = newPath; shouldNotify = true; } if (atSurfaceExpandedDirsOnly) { const buriedIdx = this.currentState.expandedDirectories.buried.indexOf(prevPath); if (buriedIdx > -1) { this.currentState.expandedDirectories.buried[buriedIdx] = newPath; shouldNotify = true; } } if (shouldNotify) { this.events.dispatch(types_1.TreeStateEvent.DidChange, types_1.TreeStateWatcherChangeType.PathsUpdated); } })); this.disposables.add(treeState.onDidChangeDirExpansionState((relDirPath, nowExpanded, isVisibleAtSurface) => { let shouldNotify = false; const atSurfaceIdx = this.currentState.expandedDirectories.atSurface.indexOf(relDirPath); if (atSurfaceIdx > -1 && (!nowExpanded || !isVisibleAtSurface)) { this.currentState.expandedDirectories.atSurface.splice(atSurfaceIdx, 1); shouldNotify = true; } else if (nowExpanded && isVisibleAtSurface) { this.currentState.expandedDirectories.atSurface.push(relDirPath); shouldNotify = true; } if (!atSurfaceExpandedDirsOnly) { const buriedIdx = this.currentState.expandedDirectories.buried.indexOf(relDirPath); if (buriedIdx > -1 && (!nowExpanded || isVisibleAtSurface)) { this.currentState.expandedDirectories.buried.splice(buriedIdx, 1); shouldNotify = true; } else if (nowExpanded && !isVisibleAtSurface) { this.currentState.expandedDirectories.buried.push(relDirPath); shouldNotify = true; } } if (shouldNotify) { this.events.dispatch(types_1.TreeStateEvent.DidChange, types_1.TreeStateWatcherChangeType.DirExpansionState); } })); } dispose() { if (this._disposed) { return; } this._disposed = true; this.disposables.dispose(); } onChange(callback) { return this.events.add(types_1.TreeStateEvent.DidChange, callback); } snapshot() { return { ...this.currentState, expandedDirectories: { atSurface: this.currentState.expandedDirectories.atSurface.slice(), buried: this.currentState.expandedDirectories.buried.slice(), }, }; } toString() { return JSON.stringify(this.currentState); } } exports.TreeStateWatcher = TreeStateWatcher; //# sourceMappingURL=TreeStateWatcher.js.map