diff --git a/src/js/app.js b/src/js/app.js index d39499e5..d3ace910 100644 --- a/src/js/app.js +++ b/src/js/app.js @@ -127,7 +127,7 @@ this.imageUploadService = new pskl.service.ImageUploadService(); this.imageUploadService.init(); - this.savedStatusService = new pskl.service.SavedStatusService(this.piskelController); + this.savedStatusService = new pskl.service.SavedStatusService(this.piskelController, this.historyService); this.savedStatusService.init(); this.backupService = new pskl.service.BackupService(this.piskelController); diff --git a/src/js/service/HistoryService.js b/src/js/service/HistoryService.js index 8c24ebed..7a5bbd32 100644 --- a/src/js/service/HistoryService.js +++ b/src/js/service/HistoryService.js @@ -46,7 +46,8 @@ var state = { action : action, frameIndex : action.state ? action.state.frameIndex : this.piskelController.currentFrameIndex, - layerIndex : action.state ? action.state.layerIndex : this.piskelController.currentLayerIndex + layerIndex : action.state ? action.state.layerIndex : this.piskelController.currentLayerIndex, + uuid: pskl.utils.Uuid.generate() }; var isSnapshot = action.type === ns.HistoryService.SNAPSHOT; @@ -59,6 +60,15 @@ $.publish(Events.HISTORY_STATE_SAVED); }; + ns.HistoryService.prototype.getCurrentStateId = function () { + var state = this.stateQueue[this.currentIndex]; + if (!state) { + return false; + } + + return state.uuid; + }; + ns.HistoryService.prototype.undo = function () { this.loadState(this.currentIndex - 1); }; diff --git a/src/js/service/SavedStatusService.js b/src/js/service/SavedStatusService.js index 9eb5789d..63020353 100644 --- a/src/js/service/SavedStatusService.js +++ b/src/js/service/SavedStatusService.js @@ -1,45 +1,31 @@ (function () { var ns = $.namespace('pskl.service'); - ns.SavedStatusService = function (piskelController) { + ns.SavedStatusService = function (piskelController, historyService) { this.piskelController = piskelController; + this.historyService = historyService; + this.lastSavedStateIndex = ''; + + this.publishStatusUpdateEvent_ = this.publishStatusUpdateEvent_.bind(this); }; ns.SavedStatusService.prototype.init = function () { - $.subscribe(Events.TOOL_RELEASED, this.onToolReleased.bind(this)); - $.subscribe(Events.PISKEL_RESET, this.onPiskelReset.bind(this)); + $.subscribe(Events.TOOL_RELEASED, this.publishStatusUpdateEvent_); + $.subscribe(Events.PISKEL_RESET, this.publishStatusUpdateEvent_); $.subscribe(Events.PISKEL_SAVED, this.onPiskelSaved.bind(this)); - }; - - ns.SavedStatusService.prototype.onPiskelReset = function () { - var piskel = this.piskelController.getPiskel(); - // A first PISKEL_RESET is triggered during the load of a new Piskel, it should be ignored - // putting a firstResetDone flag as a nasty workaround for this - if (piskel.firstResetDone_) { - this.updateDirtyStatus(true); - } else { - piskel.firstResetDone_ = true; - } - }; - - ns.SavedStatusService.prototype.onToolReleased = function () { - this.updateDirtyStatus(true); + this.lastSavedStateIndex = this.historyService.getCurrentStateId(); }; ns.SavedStatusService.prototype.onPiskelSaved = function () { - this.updateDirtyStatus(false); + this.lastSavedStateIndex = this.historyService.getCurrentStateId(); + this.publishStatusUpdateEvent_(); }; - ns.SavedStatusService.prototype.updateDirtyStatus = function (status) { - var piskel = this.piskelController.getPiskel(); - if (piskel.isDirty_ != status) { - piskel.isDirty_ = status; - $.publish(Events.PISKEL_SAVED_STATUS_UPDATE); - } + ns.SavedStatusService.prototype.publishStatusUpdateEvent_ = function () { + $.publish(Events.PISKEL_SAVED_STATUS_UPDATE); }; - ns.SavedStatusService.prototype.isDirty = function (evt) { - var piskel = this.piskelController.getPiskel(); - return piskel.isDirty_; + ns.SavedStatusService.prototype.isDirty = function () { + return (this.lastSavedStateIndex != this.historyService.getCurrentStateId()); }; })();