Daniel@0: "use strict"; Daniel@0: Daniel@0: App.module("ContextModule", function(ContextModule, App, Backbone, Marionette, $, _, Logger) { Daniel@0: Daniel@0: // Define private variables Daniel@0: var logger = null; Daniel@0: Daniel@0: ContextModule.addInitializer(function(options){ Daniel@0: Daniel@0: logger = Logger.get("ContextModule.AppContextManager"); Daniel@0: logger.setLevel(Logger.DEBUG); Daniel@0: Daniel@0: /** Daniel@0: * AppContextManager saves the given app context into the local storage and restores it Daniel@0: */ Daniel@0: ContextModule.AppContextManager = Backbone.Marionette.Object.extend({ Daniel@0: Daniel@0: /** Daniel@0: * @memberOf App.ContextModule.AppContextManager Daniel@0: */ Daniel@0: initialize: function() { Daniel@0: this.defaultSerializedAppContexts = { Daniel@0: "": { Daniel@0: stateHistory: { Daniel@0: currentSerializedState: { Daniel@0: musicCollectionGrid: { Daniel@0: selectedEntityConfigClientId: "def0", Daniel@0: entityConfigs: [ Daniel@0: {clientId: "def0", parameters: {"library": "CHARM", composer: "Mozart"}}, Daniel@0: {clientId: "def1", parameters: {"library": "CHARM", composer: "Haydn"}} Daniel@0: ], Daniel@0: viewConfigs: [ Daniel@0: {clientId: "def2", parameters: {kind: "list"}}, Daniel@0: {clientId: "def3", parameters: {kind: "tonic-histogram"}} Daniel@0: ], Daniel@0: }, Daniel@0: musicRecordingGrid: { Daniel@0: viewConfigs: [ Daniel@0: {clientId: "def4", parameters: {kind: "properties"}}, Daniel@0: {clientId: "def5", parameters: {kind: "midi-pitch-histogram"}}, Daniel@0: ], Daniel@0: } Daniel@0: } Daniel@0: } Daniel@0: }, Daniel@0: "empty": {}, Daniel@0: }; Daniel@0: }, Daniel@0: Daniel@0: /** Daniel@0: * @memberOf App.ContextModule.AppContextManager Daniel@0: * @returns {Boolean} true if success, false if there was a problem Daniel@0: */ Daniel@0: saveToStorage: function(appContext) { Daniel@0: logger.debug("Saving to storage") Daniel@0: return App.DataModule.Storage.setObjCache(ContextModule, "context", appContext.serialize()); Daniel@0: }, Daniel@0: Daniel@0: /** Daniel@0: * @memberOf App.ContextModule.AppContextManager Daniel@0: * @returns {Boolean} true if success, false if there was an error, and a default was restored Daniel@0: */ Daniel@0: restoreFromStorage: function(appContext, nameOfDefaultIfFailure) { Daniel@0: logger.debug("Restoring from storage") Daniel@0: var serializedAppContext = App.DataModule.Storage.getObjCache(ContextModule, "context"); Daniel@0: if (!serializedAppContext) { Daniel@0: this.restoreDefault(appContext, nameOfDefaultIfFailure); Daniel@0: return false; Daniel@0: } else { Daniel@0: appContext.unserialize(serializedAppContext); Daniel@0: return true; Daniel@0: }; Daniel@0: }, Daniel@0: Daniel@0: restoreDefault: function(appContext, nameOfDefault) { Daniel@0: var name = nameOfDefault; Daniel@0: if (!name) { Daniel@0: name = ""; Daniel@0: } Daniel@0: logger.debug("Restoring default"); Daniel@0: appContext.unserialize(this.defaultSerializedAppContexts[name]); Daniel@0: } Daniel@0: }); Daniel@0: }); Daniel@0: }, Logger);