annotate src/DML/MainVisBundle/Resources/assets/marionette/modules/ContextModule/ContextModule.40-AppContext.js @ 1:f38015048f48 tip

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