Merge pull request #471 from juliandescottes/GMartigny-issue_#447_dirty_state_on_replace

G martigny issue #447 dirty state on replace
This commit is contained in:
Julian Descottes 2016-05-22 15:09:57 +02:00
commit e272fbe32f
3 changed files with 26 additions and 30 deletions

View File

@ -127,7 +127,7 @@
this.imageUploadService = new pskl.service.ImageUploadService(); this.imageUploadService = new pskl.service.ImageUploadService();
this.imageUploadService.init(); 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.savedStatusService.init();
this.backupService = new pskl.service.BackupService(this.piskelController); this.backupService = new pskl.service.BackupService(this.piskelController);

View File

@ -46,7 +46,8 @@
var state = { var state = {
action : action, action : action,
frameIndex : action.state ? action.state.frameIndex : this.piskelController.currentFrameIndex, 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; var isSnapshot = action.type === ns.HistoryService.SNAPSHOT;
@ -59,6 +60,15 @@
$.publish(Events.HISTORY_STATE_SAVED); $.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 () { ns.HistoryService.prototype.undo = function () {
this.loadState(this.currentIndex - 1); this.loadState(this.currentIndex - 1);
}; };

View File

@ -1,45 +1,31 @@
(function () { (function () {
var ns = $.namespace('pskl.service'); var ns = $.namespace('pskl.service');
ns.SavedStatusService = function (piskelController) { ns.SavedStatusService = function (piskelController, historyService) {
this.piskelController = piskelController; this.piskelController = piskelController;
this.historyService = historyService;
this.lastSavedStateIndex = '';
this.publishStatusUpdateEvent_ = this.publishStatusUpdateEvent_.bind(this);
}; };
ns.SavedStatusService.prototype.init = function () { ns.SavedStatusService.prototype.init = function () {
$.subscribe(Events.TOOL_RELEASED, this.onToolReleased.bind(this)); $.subscribe(Events.TOOL_RELEASED, this.publishStatusUpdateEvent_);
$.subscribe(Events.PISKEL_RESET, this.onPiskelReset.bind(this)); $.subscribe(Events.PISKEL_RESET, this.publishStatusUpdateEvent_);
$.subscribe(Events.PISKEL_SAVED, this.onPiskelSaved.bind(this)); $.subscribe(Events.PISKEL_SAVED, this.onPiskelSaved.bind(this));
}; this.lastSavedStateIndex = this.historyService.getCurrentStateId();
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);
}; };
ns.SavedStatusService.prototype.onPiskelSaved = function () { ns.SavedStatusService.prototype.onPiskelSaved = function () {
this.updateDirtyStatus(false); this.lastSavedStateIndex = this.historyService.getCurrentStateId();
this.publishStatusUpdateEvent_();
}; };
ns.SavedStatusService.prototype.updateDirtyStatus = function (status) { ns.SavedStatusService.prototype.publishStatusUpdateEvent_ = function () {
var piskel = this.piskelController.getPiskel(); $.publish(Events.PISKEL_SAVED_STATUS_UPDATE);
if (piskel.isDirty_ != status) {
piskel.isDirty_ = status;
$.publish(Events.PISKEL_SAVED_STATUS_UPDATE);
}
}; };
ns.SavedStatusService.prototype.isDirty = function (evt) { ns.SavedStatusService.prototype.isDirty = function () {
var piskel = this.piskelController.getPiskel(); return (this.lastSavedStateIndex != this.historyService.getCurrentStateId());
return piskel.isDirty_;
}; };
})(); })();