comparison src/DML/MainVisBundle/Resources/assets/marionette/modules/ContextModule/ContextModule.11-State.js @ 0:493bcb69166c

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