From aae994b3d580a66d86451bea7ab8efd5d711f110 Mon Sep 17 00:00:00 2001 From: jdescottes Date: Tue, 11 Feb 2014 23:42:38 +0100 Subject: [PATCH] Feature : Saved status and warning msg Added saved status (*) next to title when a Piskel is updated. Upon saving, the * disappears. If the workspace contains an unsaved piskel when leaving the application a message will be displayed to the user, using onbeforeunload. This logic should also be used everywhere we display a confirm message before a navigation. --- js/Events.js | 2 + js/app.js | 3 ++ js/controller/settings/SaveController.js | 1 + js/service/SavedStatusService.js | 63 ++++++++++++++++++++++++ piskel-script-list.js | 1 + 5 files changed, 70 insertions(+) create mode 100644 js/service/SavedStatusService.js diff --git a/js/Events.js b/js/Events.js index aebb0306..b7b4523e 100644 --- a/js/Events.js +++ b/js/Events.js @@ -29,6 +29,8 @@ var Events = { */ PISKEL_RESET: "PISKEL_RESET", + PISKEL_SAVED: "PISKEL_SAVED", + FRAME_SIZE_CHANGED : "FRAME_SIZE_CHANGED", SELECTION_CREATED: "SELECTION_CREATED", diff --git a/js/app.js b/js/app.js index a2179814..c8a93526 100644 --- a/js/app.js +++ b/js/app.js @@ -75,6 +75,9 @@ this.cheatsheetService = new pskl.service.keyboard.CheatsheetService(); this.cheatsheetService.init(); + this.savedStatusService = new pskl.service.SavedStatusService(this.piskelController); + this.savedStatusService.init(); + if (this.isAppEngineVersion) { this.storageService = new pskl.service.AppEngineStorageService(this.piskelController); } else { diff --git a/js/controller/settings/SaveController.js b/js/controller/settings/SaveController.js index 57d9c577..01c7a6f9 100644 --- a/js/controller/settings/SaveController.js +++ b/js/controller/settings/SaveController.js @@ -95,6 +95,7 @@ ns.SaveController.prototype.onSaveSuccess_ = function () { $.publish(Events.CLOSE_SETTINGS_DRAWER); $.publish(Events.SHOW_NOTIFICATION, [{"content": "Successfully saved !"}]); + $.publish(Events.PISKEL_SAVED); }; ns.SaveController.prototype.onSaveError_ = function (status) { diff --git a/js/service/SavedStatusService.js b/js/service/SavedStatusService.js new file mode 100644 index 00000000..2a0024fc --- /dev/null +++ b/js/service/SavedStatusService.js @@ -0,0 +1,63 @@ +(function () { + var ns = $.namespace('pskl.service'); + + ns.SavedStatusService = function (piskelController) { + this.piskelController_ = piskelController; + }; + + 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)); + + window.addEventListener("beforeunload", this.onBeforeUnload.bind(this)); + }; + + ns.SavedStatusService.prototype.onPiskelReset = function () { + var piskel = this.piskelController_.piskel; + // 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 () { + this.updateDirtyStatus(false); + }; + + ns.SavedStatusService.prototype.updateDirtyStatus = function (status) { + var piskel = this.piskelController_.piskel; + if (piskel.isDirty_ !== status) { + piskel.isDirty_ = status; + this.updatePiskelName(); + } + }; + + ns.SavedStatusService.prototype.updatePiskelName = function () { + var piskel = this.piskelController_.piskel; + var name = piskel.getDescriptor().name; + if (piskel.isDirty_) { + $('.piskel-name').html(name + ' *'); + } else { + $('.piskel-name').html(name); + } + }; + + ns.SavedStatusService.prototype.onBeforeUnload = function (evt) { + var piskel = this.piskelController_.piskel; + if (piskel.isDirty_) { + var confirmationMessage = "Your Piskel seem to have unsaved changes"; + + (evt || window.event).returnValue = confirmationMessage; + return confirmationMessage; + } + }; +})(); \ No newline at end of file diff --git a/piskel-script-list.js b/piskel-script-list.js index 0e92ebb6..65247a70 100644 --- a/piskel-script-list.js +++ b/piskel-script-list.js @@ -83,6 +83,7 @@ exports.scripts = [ "js/service/GithubStorageService.js", "js/service/AppEngineStorageService.js", "js/service/HistoryService.js", + "js/service/SavedStatusService.js", "js/service/keyboard/ShortcutService.js", "js/service/keyboard/KeycodeTranslator.js", "js/service/keyboard/CheatsheetService.js",