Dirty status compare last save state index and current state index

Signed-off-by:Guillaume Martigny <guillaume.martigny@gmail.com>
This commit is contained in:
Guillaume Martigny 2016-04-19 17:31:33 +02:00 committed by Julian Descottes
parent 9b6d45e8ed
commit 58a1a6b043
2 changed files with 16 additions and 24 deletions

View File

@ -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);

View File

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