UNPKG

turbocommons-ts

Version:

General purpose library that implements frequently used and generic software development tasks

1 lines 2.98 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.ModelHistoryManager=void 0;var ObjectUtils_1=require("../utils/ObjectUtils"),ArrayUtils_1=require("../utils/ArrayUtils"),StringUtils_1=require("../utils/StringUtils"),ModelHistoryManager=function(){function t(t){this.maxSnapshots=-1,this._snapshots=[],this._currentState=t,this._initialState=ObjectUtils_1.ObjectUtils.clone(this._currentState)}return t.prototype.setInitialState=function(){this._initialState=ObjectUtils_1.ObjectUtils.clone(this._currentState),this._snapshots=[]},Object.defineProperty(t.prototype,"get",{get:function(){return this._currentState},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"tags",{get:function(){return this._snapshots.map(function(t){return t.tag})},enumerable:!1,configurable:!0}),Object.defineProperty(t.prototype,"snapshots",{get:function(){return this._snapshots.map(function(t){return t.state})},enumerable:!1,configurable:!0}),t.prototype.getSnapshotsByTag=function(t){var s="tags must be a non empty string array. To get the full list of snapshots, use the <snapshots> property";if(!ArrayUtils_1.ArrayUtils.isArray(t))throw new Error(s);if(t.length<=0)throw new Error(s);for(var e=[],i=0;i<this._snapshots.length;i++)t.indexOf(this._snapshots[i].tag)>=0&&e.push(this._snapshots[i].state);return e},t.prototype.saveSnapshot=function(t){if(void 0===t&&(t=""),!StringUtils_1.StringUtils.isString(t))throw new Error("tag must be a string");if(this._snapshots.length>0&&this._snapshots[this._snapshots.length-1].tag===t&&ObjectUtils_1.ObjectUtils.isEqualTo(this._currentState,this._snapshots[this._snapshots.length-1].state))return!1;if(this._snapshots.length<=0&&ObjectUtils_1.ObjectUtils.isEqualTo(this._currentState,this._initialState))return!1;if(this.maxSnapshots>0&&this._snapshots.length>=this.maxSnapshots){var s=this._snapshots.shift();this._initialState=s.state}return this._snapshots.push({state:ObjectUtils_1.ObjectUtils.clone(this._currentState),tag:t}),!0},Object.defineProperty(t.prototype,"isUndoPossible",{get:function(){return this._snapshots.length>0||!ObjectUtils_1.ObjectUtils.isEqualTo(this._currentState,this._initialState)},enumerable:!1,configurable:!0}),t.prototype.undo=function(t){if(void 0===t&&(t=[]),0===this._snapshots.length&&!ObjectUtils_1.ObjectUtils.isEqualTo(this._currentState,this._initialState))return this._currentState=ObjectUtils_1.ObjectUtils.clone(this._initialState),!0;if(this._snapshots.length>0){var s=this._snapshots[this._snapshots.length-1];return ObjectUtils_1.ObjectUtils.isEqualTo(this._currentState,s.state)||t.length>0&&t.indexOf(s.tag)<0?(this._snapshots.pop(),this.undo(t)):(this._currentState=ObjectUtils_1.ObjectUtils.clone(s.state),!0)}return!1},t.prototype.undoAll=function(){return!!this.isUndoPossible&&(this._currentState=ObjectUtils_1.ObjectUtils.clone(this._initialState),this._snapshots=[],!0)},t.prototype.redo=function(){},t}();exports.ModelHistoryManager=ModelHistoryManager;