Daniel@0
|
1 "use strict";
|
Daniel@0
|
2
|
Daniel@0
|
3 App.module("ContextModule", function(ContextModule, App, Backbone, Marionette, $, _, Logger) {
|
Daniel@0
|
4
|
Daniel@0
|
5 // Define private variables
|
Daniel@0
|
6 var logger = null;
|
Daniel@0
|
7
|
Daniel@0
|
8 ContextModule.addInitializer(function(options){
|
Daniel@0
|
9
|
Daniel@0
|
10 logger = Logger.get("ContextModule.AppContextManager");
|
Daniel@0
|
11 logger.setLevel(Logger.DEBUG);
|
Daniel@0
|
12
|
Daniel@0
|
13 /**
|
Daniel@0
|
14 * AppContextManager saves the given app context into the local storage and restores it
|
Daniel@0
|
15 */
|
Daniel@0
|
16 ContextModule.AppContextManager = Backbone.Marionette.Object.extend({
|
Daniel@0
|
17
|
Daniel@0
|
18 /**
|
Daniel@0
|
19 * @memberOf App.ContextModule.AppContextManager
|
Daniel@0
|
20 */
|
Daniel@0
|
21 initialize: function() {
|
Daniel@0
|
22 this.defaultSerializedAppContexts = {
|
Daniel@0
|
23 "": {
|
Daniel@0
|
24 stateHistory: {
|
Daniel@0
|
25 currentSerializedState: {
|
Daniel@0
|
26 musicCollectionGrid: {
|
Daniel@0
|
27 selectedEntityConfigClientId: "def0",
|
Daniel@0
|
28 entityConfigs: [
|
Daniel@0
|
29 {clientId: "def0", parameters: {"library": "CHARM", composer: "Mozart"}},
|
Daniel@0
|
30 {clientId: "def1", parameters: {"library": "CHARM", composer: "Haydn"}}
|
Daniel@0
|
31 ],
|
Daniel@0
|
32 viewConfigs: [
|
Daniel@0
|
33 {clientId: "def2", parameters: {kind: "list"}},
|
Daniel@0
|
34 {clientId: "def3", parameters: {kind: "tonic-histogram"}}
|
Daniel@0
|
35 ],
|
Daniel@0
|
36 },
|
Daniel@0
|
37 musicRecordingGrid: {
|
Daniel@0
|
38 viewConfigs: [
|
Daniel@0
|
39 {clientId: "def4", parameters: {kind: "properties"}},
|
Daniel@0
|
40 {clientId: "def5", parameters: {kind: "midi-pitch-histogram"}},
|
Daniel@0
|
41 ],
|
Daniel@0
|
42 }
|
Daniel@0
|
43 }
|
Daniel@0
|
44 }
|
Daniel@0
|
45 },
|
Daniel@0
|
46 "empty": {},
|
Daniel@0
|
47 };
|
Daniel@0
|
48 },
|
Daniel@0
|
49
|
Daniel@0
|
50 /**
|
Daniel@0
|
51 * @memberOf App.ContextModule.AppContextManager
|
Daniel@0
|
52 * @returns {Boolean} true if success, false if there was a problem
|
Daniel@0
|
53 */
|
Daniel@0
|
54 saveToStorage: function(appContext) {
|
Daniel@0
|
55 logger.debug("Saving to storage")
|
Daniel@0
|
56 return App.DataModule.Storage.setObjCache(ContextModule, "context", appContext.serialize());
|
Daniel@0
|
57 },
|
Daniel@0
|
58
|
Daniel@0
|
59 /**
|
Daniel@0
|
60 * @memberOf App.ContextModule.AppContextManager
|
Daniel@0
|
61 * @returns {Boolean} true if success, false if there was an error, and a default was restored
|
Daniel@0
|
62 */
|
Daniel@0
|
63 restoreFromStorage: function(appContext, nameOfDefaultIfFailure) {
|
Daniel@0
|
64 logger.debug("Restoring from storage")
|
Daniel@0
|
65 var serializedAppContext = App.DataModule.Storage.getObjCache(ContextModule, "context");
|
Daniel@0
|
66 if (!serializedAppContext) {
|
Daniel@0
|
67 this.restoreDefault(appContext, nameOfDefaultIfFailure);
|
Daniel@0
|
68 return false;
|
Daniel@0
|
69 } else {
|
Daniel@0
|
70 appContext.unserialize(serializedAppContext);
|
Daniel@0
|
71 return true;
|
Daniel@0
|
72 };
|
Daniel@0
|
73 },
|
Daniel@0
|
74
|
Daniel@0
|
75 restoreDefault: function(appContext, nameOfDefault) {
|
Daniel@0
|
76 var name = nameOfDefault;
|
Daniel@0
|
77 if (!name) {
|
Daniel@0
|
78 name = "";
|
Daniel@0
|
79 }
|
Daniel@0
|
80 logger.debug("Restoring default");
|
Daniel@0
|
81 appContext.unserialize(this.defaultSerializedAppContexts[name]);
|
Daniel@0
|
82 }
|
Daniel@0
|
83 });
|
Daniel@0
|
84 });
|
Daniel@0
|
85 }, Logger);
|