comparison src/DML/MainVisBundle/Resources/assets/marionette/modules/ContextModule/ContextModule.40-AppContext.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.AppContext");
11 logger.setLevel(Logger.DEBUG);
12
13 /**
14 * AppContext is an object that describes the current user’s environment.
15 * It keeps the current state, a history of states and state bookmarks.
16 */
17 ContextModule.AppContext = Backbone.Model.extend({
18 defaults: function() {
19 return {
20 state: new ContextModule.State(),
21 stateHistory: new ContextModule.StateHistory(),
22 stateBookmarks: new ContextModule.StateBookmarkCollection()
23 };
24 },
25
26 /**
27 * @memberOf App.ContextModule.AppContext
28 */
29 initialize: function() {
30 this._modificationPropagationEnabled = true;
31 this._linkBetweenStateAndStateHistoryEnabled = true;
32 this._attributesWereModified = false;
33
34 this._registerStateChange();
35
36 this.attributes.state.bind("change", this._registerStateChange, this);
37 this.attributes.stateHistory.bind("change", this._registerStateHistoryChange, this);
38 this.attributes.stateBookmarks.bind("change", this._registerStateBookmarksChange, this);
39 },
40
41 /**
42 * @memberOf App.ContextModule.AppContext
43 */
44 serialize: function() {
45 //logger.debug("method called: AppContext::serialize");
46 var result = {
47 stateHistory: this.attributes.stateHistory.serialize(),
48 stateBookmarks: this.attributes.stateBookmarks.serialize(),
49 };
50
51 return result;
52 },
53
54 /**
55 * @memberOf App.ContextModule.AppContext
56 */
57 unserialize: function(serializedAttributes) {
58 //logger.debug("method called: AppContext::unserialize");
59 this._modificationPropagationEnabled = false;
60
61 var fixedSerializedAttributes = serializedAttributes;
62 if (!_.isSimpleObject(fixedSerializedAttributes)) {
63 logger.warn("AppContext::unserialize called for not an object: ", serializedAttributes);
64 fixedSerializedAttributes = {};
65 }
66 this.attributes.stateBookmarks.unserialize(fixedSerializedAttributes.stateBookmarks);
67
68 var serializedStateHistory = fixedSerializedAttributes["stateHistory"];
69 if (!_.isSimpleObject(serializedStateHistory)) {
70 serializedStateHistory = {};
71 }
72 this._linkBetweenStateAndStateHistoryEnabled = false;
73 this.attributes.state.unserialize(serializedStateHistory.currentSerializedState);
74 serializedStateHistory.currentSerializedState = this.attributes.state.serialize();
75 this.attributes.stateHistory.unserialize(serializedStateHistory);
76 this._linkBetweenStateAndStateHistoryEnabled = true;
77
78 this._triggerModificationEventsIfNeeded();
79 this._modificationPropagationEnabled = true;
80 },
81
82 _registerStateChange: function() {
83 if (this._linkBetweenStateAndStateHistoryEnabled) {
84 this.attributes.stateHistory.set("currentSerializedState", this.attributes.state.serialize());
85 }
86 this._registerModificationOfAttribute();
87 },
88
89 _registerStateHistoryChange: function() {
90 if (this._linkBetweenStateAndStateHistoryEnabled) {
91 this.attributes.state.unserialize(this.attributes.stateHistory.get("currentSerializedState"));
92 }
93 if (this._stateHistoryModificationPropagationEnabled) {
94 this._registerModificationOfAttribute();
95 }
96 },
97
98 _registerStateBookmarksChange: function() {
99 this._registerModificationOfAttribute();
100 },
101
102 _registerModificationOfAttribute: function() {
103 this._attributesWereModified = true;
104 if (this._modificationPropagationEnabled) {
105 this._triggerModificationEventsIfNeeded();
106 };
107 },
108
109 _triggerModificationEventsIfNeeded: function() {
110 if (this._attributesWereModified) {
111 this.trigger("change");
112 }
113 this._attributesWereModified = false;
114 },
115 });
116
117 });
118 }, Logger);