diff --git a/src/js/Events.js b/src/js/Events.js index cbb967ee..8ef325c8 100644 --- a/src/js/Events.js +++ b/src/js/Events.js @@ -40,8 +40,17 @@ var Events = { HISTORY_STATE_SAVED: 'HISTORY_STATE_SAVED', HISTORY_STATE_LOADED: 'HISTORY_STATE_LOADED', + /** + * Fired when a Piskel is successfully saved + */ PISKEL_SAVED: 'PISKEL_SAVED', + /** + * Fired when a save action starts + */ BEFORE_SAVING_PISKEL: 'BEFORE_SAVING_PISKEL', + /** + * Fired when a save action ends. Always fires, even if saving was not successful + */ AFTER_SAVING_PISKEL: 'AFTER_SAVING_PISKEL', FRAME_SIZE_CHANGED : 'FRAME_SIZE_CHANGED', diff --git a/src/js/controller/settings/SaveController.js b/src/js/controller/settings/SaveController.js index 974c70b7..bf18a698 100644 --- a/src/js/controller/settings/SaveController.js +++ b/src/js/controller/settings/SaveController.js @@ -32,9 +32,6 @@ this.addEventListener(this.saveBrowserButton, 'click', this.saveBrowser_); this.addEventListener('form[name=save-form]', 'submit', this.onSaveFormSubmit_); - $.subscribe(Events.BEFORE_SAVING_PISKEL, this.disableSaveButtons_.bind(this)); - $.subscribe(Events.AFTER_SAVING_PISKEL, this.enableSaveButtons_.bind(this)); - if (pskl.app.isLoggedIn()) { this.authenticatedUserInit_(); } @@ -42,6 +39,13 @@ if (pskl.utils.Environment.detectNodeWebkit()) { this.desktopApplicationInit_(); } + + $.subscribe(Events.BEFORE_SAVING_PISKEL, this.disableSaveButtons_.bind(this)); + $.subscribe(Events.AFTER_SAVING_PISKEL, this.enableSaveButtons_.bind(this)); + + if (pskl.app.storageService.isSaving()) { + this.disableSaveButtons_(); + } }; ns.SaveController.prototype.authenticatedUserInit_ = function () { diff --git a/src/js/service/storage/StorageService.js b/src/js/service/storage/StorageService.js index 7df84c91..dd9e2f41 100644 --- a/src/js/service/storage/StorageService.js +++ b/src/js/service/storage/StorageService.js @@ -3,6 +3,7 @@ ns.StorageService = function (piskelController) { this.piskelController = piskelController; + this.savingFlag_ = false; this.onSaveSuccess_ = this.onSaveSuccess_.bind(this); this.onSaveError_ = this.onSaveError_.bind(this); @@ -12,6 +13,38 @@ pskl.app.shortcutService.addShortcut('ctrl+o', this.onOpenKey_.bind(this)); pskl.app.shortcutService.addShortcut('ctrl+s', this.onSaveKey_.bind(this)); pskl.app.shortcutService.addShortcut('ctrl+shift+s', this.onSaveAsKey_.bind(this)); + + $.subscribe(Events.BEFORE_SAVING_PISKEL, this.setSavingFlag_.bind(this, true)); + $.subscribe(Events.AFTER_SAVING_PISKEL, this.setSavingFlag_.bind(this, false)); + }; + + ns.StorageService.prototype.isSaving = function () { + return this.savingFlag_; + }; + + ns.StorageService.prototype.saveToGallery = function (piskel) { + return this.delegateSave_(pskl.app.galleryStorageService, piskel); + }; + + ns.StorageService.prototype.saveToLocalStorage = function (piskel) { + return this.delegateSave_(pskl.app.localStorageService, piskel); + }; + + ns.StorageService.prototype.saveToFileBrowser = function (piskel) { + return this.delegateSave_(pskl.app.fileDownloadStorageService, piskel); + }; + + ns.StorageService.prototype.saveToFileNodeWebkit = function (piskel, saveAsNew) { + return this.delegateSave_(pskl.app.desktopStorageService, piskel, saveAsNew); + }; + + ns.StorageService.prototype.delegateSave_ = function(delegatedService, piskel, saveAsNew) { + if (this.isSaving_) { + return Q.reject('Already saving'); + } + + $.publish(Events.BEFORE_SAVING_PISKEL); + return delegatedService.save(piskel, saveAsNew).then(this.onSaveSuccess_, this.onSaveError_); }; ns.StorageService.prototype.onOpenKey_ = function () { @@ -39,26 +72,6 @@ // no other implementation for now }; - ns.StorageService.prototype.saveToGallery = function (piskel) { - $.publish(Events.BEFORE_SAVING_PISKEL); - return pskl.app.galleryStorageService.save(piskel).then(this.onSaveSuccess_, this.onSaveError_); - }; - - ns.StorageService.prototype.saveToLocalStorage = function (piskel) { - $.publish(Events.BEFORE_SAVING_PISKEL); - return pskl.app.localStorageService.save(piskel).then(this.onSaveSuccess_, this.onSaveError_); - }; - - ns.StorageService.prototype.saveToFileBrowser = function (piskel) { - $.publish(Events.BEFORE_SAVING_PISKEL); - return pskl.app.fileDownloadStorageService.save(piskel).then(this.onSaveSuccess_, this.onSaveError_); - }; - - ns.StorageService.prototype.saveToFileNodeWebkit = function (piskel, saveAsNew) { - $.publish(Events.BEFORE_SAVING_PISKEL); - return pskl.app.desktopStorageService.save(piskel, saveAsNew).then(this.onSaveSuccess_, this.onSaveError_); - }; - ns.StorageService.prototype.onSaveSuccess_ = function () { $.publish(Events.SHOW_NOTIFICATION, [{'content': 'Successfully saved !'}]); $.publish(Events.PISKEL_SAVED); @@ -79,4 +92,8 @@ $.publish(Events.AFTER_SAVING_PISKEL); window.setTimeout($.publish.bind($, Events.HIDE_NOTIFICATION), 5000); }; + + ns.StorageService.prototype.setSavingFlag_ = function (savingFlag) { + this.savingFlag_ = savingFlag; + }; })();