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.State");
|
Daniel@0
|
11 logger.setLevel(Logger.WARN);
|
Daniel@0
|
12
|
Daniel@0
|
13 /**
|
Daniel@0
|
14 * State stores the configuration of the entire vis interface, i.e.
|
Daniel@0
|
15 * the contents of both entity config grids (collections & recordings)
|
Daniel@0
|
16 *
|
Daniel@0
|
17 * The state can be serialized and unserialized, and this allows for
|
Daniel@0
|
18 * undo / redo mechanism to be implemented.
|
Daniel@0
|
19 */
|
Daniel@0
|
20 ContextModule.State = Backbone.Model.extend({
|
Daniel@0
|
21 defaults: {
|
Daniel@0
|
22 musicCollectionGrid: null,
|
Daniel@0
|
23 musicRecordingGrid: null
|
Daniel@0
|
24 },
|
Daniel@0
|
25
|
Daniel@0
|
26 /**
|
Daniel@0
|
27 * @memberOf App.ContextModule.State
|
Daniel@0
|
28 */
|
Daniel@0
|
29 initialize: function() {
|
Daniel@0
|
30 this._modificationPropagationEnabled = true;
|
Daniel@0
|
31 this._attributesWereModified = false;
|
Daniel@0
|
32
|
Daniel@0
|
33 this.attributes.musicCollectionGrid = new ContextModule.ConfigGrid("collection");
|
Daniel@0
|
34 this.attributes.musicRecordingGrid = new ContextModule.ConfigGrid("recording");
|
Daniel@0
|
35
|
Daniel@0
|
36 this.attributes.musicCollectionGrid.bind("change", this._registerModificationOfAttribute, this);
|
Daniel@0
|
37 this.attributes.musicRecordingGrid.bind("change", this._registerModificationOfAttribute, this);
|
Daniel@0
|
38 },
|
Daniel@0
|
39
|
Daniel@0
|
40 /**
|
Daniel@0
|
41 * @memberOf App.ContextModule.State
|
Daniel@0
|
42 */
|
Daniel@0
|
43 getConfigGridByType: function(type) {
|
Daniel@0
|
44 switch (type) {
|
Daniel@0
|
45 case "recordings":
|
Daniel@0
|
46 return this.attributes.musicRecordingGrid;
|
Daniel@0
|
47 case "collections":
|
Daniel@0
|
48 return this.attributes.musicCollectionGrid;
|
Daniel@0
|
49 default:
|
Daniel@0
|
50 return null;
|
Daniel@0
|
51 }
|
Daniel@0
|
52 },
|
Daniel@0
|
53
|
Daniel@0
|
54 /**
|
Daniel@0
|
55 * @memberOf App.ContextModule.State
|
Daniel@0
|
56 */
|
Daniel@0
|
57 getConfigGridBeingShown: function() {
|
Daniel@0
|
58 return this.attributes.musicRecordingsGridIsShown
|
Daniel@0
|
59 ? this.attributes.musicRecordingGrid
|
Daniel@0
|
60 : this.attributes.musicCollectionGrid;
|
Daniel@0
|
61 },
|
Daniel@0
|
62
|
Daniel@0
|
63 /**
|
Daniel@0
|
64 * @memberOf App.ContextModule.State
|
Daniel@0
|
65 */
|
Daniel@0
|
66 serialize: function() {
|
Daniel@0
|
67 logger.debug("method called: State::serialize");
|
Daniel@0
|
68
|
Daniel@0
|
69 var result = {
|
Daniel@0
|
70 musicCollectionGrid: this.attributes.musicCollectionGrid.serialize(),
|
Daniel@0
|
71 musicRecordingGrid: this.attributes.musicRecordingGrid.serialize()
|
Daniel@0
|
72 };
|
Daniel@0
|
73
|
Daniel@0
|
74 if (this.attributes.musicRecordingsGridIsShown) {
|
Daniel@0
|
75 result.musicRecordingsGridIsShown = true;
|
Daniel@0
|
76 }
|
Daniel@0
|
77
|
Daniel@0
|
78 return result;
|
Daniel@0
|
79 },
|
Daniel@0
|
80
|
Daniel@0
|
81 /**
|
Daniel@0
|
82 * @memberOf App.ContextModule.State
|
Daniel@0
|
83 */
|
Daniel@0
|
84 unserialize: function(serializedAttributes) {
|
Daniel@0
|
85 logger.debug("method called: State::unserialize");
|
Daniel@0
|
86 this._modificationPropagationEnabled = false;
|
Daniel@0
|
87 this._attributesWereModified = false;
|
Daniel@0
|
88
|
Daniel@0
|
89 var fixedSerializedAttributes = serializedAttributes;
|
Daniel@0
|
90 if (!_.isSimpleObject(fixedSerializedAttributes)) {
|
Daniel@0
|
91 logger.warn("State::unserialize called for not an object: ", serializedAttributes);
|
Daniel@0
|
92 fixedSerializedAttributes = {};
|
Daniel@0
|
93 }
|
Daniel@0
|
94 this.attributes.musicCollectionGrid.unserialize(fixedSerializedAttributes.musicCollectionGrid);
|
Daniel@0
|
95 this.attributes.musicRecordingGrid.unserialize(fixedSerializedAttributes.musicRecordingGrid);
|
Daniel@0
|
96
|
Daniel@0
|
97 if (this.attributes.musicRecordingsGridIsShown !== fixedSerializedAttributes.musicRecordingsGridIsShown) {
|
Daniel@0
|
98 this._attributesWereModified = true;
|
Daniel@0
|
99 }
|
Daniel@0
|
100 if (fixedSerializedAttributes.musicRecordingsGridIsShown) {
|
Daniel@0
|
101 this.attributes.musicRecordingsGridIsShown = true;
|
Daniel@0
|
102 } else {
|
Daniel@0
|
103 delete this.attributes.musicRecordingsGridIsShown;
|
Daniel@0
|
104 }
|
Daniel@0
|
105
|
Daniel@0
|
106 this._triggerModificationEventsIfNeeded();
|
Daniel@0
|
107 this._modificationPropagationEnabled = true;
|
Daniel@0
|
108 },
|
Daniel@0
|
109
|
Daniel@0
|
110 _registerModificationOfAttribute: function() {
|
Daniel@0
|
111 this._attributesWereModified = true;
|
Daniel@0
|
112 if (this._modificationPropagationEnabled) {
|
Daniel@0
|
113 this._triggerModificationEventsIfNeeded();
|
Daniel@0
|
114 };
|
Daniel@0
|
115 },
|
Daniel@0
|
116
|
Daniel@0
|
117 _triggerModificationEventsIfNeeded: function() {
|
Daniel@0
|
118 if (this._attributesWereModified) {
|
Daniel@0
|
119 this.trigger("change");
|
Daniel@0
|
120 }
|
Daniel@0
|
121 this._attributesWereModified = false;
|
Daniel@0
|
122 },
|
Daniel@0
|
123 });
|
Daniel@0
|
124
|
Daniel@0
|
125 });
|
Daniel@0
|
126 }, Logger);
|