Daniel@0: "use strict"; Daniel@0: Daniel@0: App.module("ContextModule", function(ContextModule, App, Backbone, Marionette, $, _, Logger) { Daniel@0: Daniel@0: // Define private variables Daniel@0: var logger = null; Daniel@0: Daniel@0: ContextModule.addInitializer(function(options){ Daniel@0: Daniel@0: logger = Logger.get("ContextModule.State"); Daniel@0: logger.setLevel(Logger.WARN); Daniel@0: Daniel@0: /** Daniel@0: * State stores the configuration of the entire vis interface, i.e. Daniel@0: * the contents of both entity config grids (collections & recordings) Daniel@0: * Daniel@0: * The state can be serialized and unserialized, and this allows for Daniel@0: * undo / redo mechanism to be implemented. Daniel@0: */ Daniel@0: ContextModule.State = Backbone.Model.extend({ Daniel@0: defaults: { Daniel@0: musicCollectionGrid: null, Daniel@0: musicRecordingGrid: null Daniel@0: }, Daniel@0: Daniel@0: /** Daniel@0: * @memberOf App.ContextModule.State Daniel@0: */ Daniel@0: initialize: function() { Daniel@0: this._modificationPropagationEnabled = true; Daniel@0: this._attributesWereModified = false; Daniel@0: Daniel@0: this.attributes.musicCollectionGrid = new ContextModule.ConfigGrid("collection"); Daniel@0: this.attributes.musicRecordingGrid = new ContextModule.ConfigGrid("recording"); Daniel@0: Daniel@0: this.attributes.musicCollectionGrid.bind("change", this._registerModificationOfAttribute, this); Daniel@0: this.attributes.musicRecordingGrid.bind("change", this._registerModificationOfAttribute, this); Daniel@0: }, Daniel@0: Daniel@0: /** Daniel@0: * @memberOf App.ContextModule.State Daniel@0: */ Daniel@0: getConfigGridByType: function(type) { Daniel@0: switch (type) { Daniel@0: case "recordings": Daniel@0: return this.attributes.musicRecordingGrid; Daniel@0: case "collections": Daniel@0: return this.attributes.musicCollectionGrid; Daniel@0: default: Daniel@0: return null; Daniel@0: } Daniel@0: }, Daniel@0: Daniel@0: /** Daniel@0: * @memberOf App.ContextModule.State Daniel@0: */ Daniel@0: getConfigGridBeingShown: function() { Daniel@0: return this.attributes.musicRecordingsGridIsShown Daniel@0: ? this.attributes.musicRecordingGrid Daniel@0: : this.attributes.musicCollectionGrid; Daniel@0: }, Daniel@0: Daniel@0: /** Daniel@0: * @memberOf App.ContextModule.State Daniel@0: */ Daniel@0: serialize: function() { Daniel@0: logger.debug("method called: State::serialize"); Daniel@0: Daniel@0: var result = { Daniel@0: musicCollectionGrid: this.attributes.musicCollectionGrid.serialize(), Daniel@0: musicRecordingGrid: this.attributes.musicRecordingGrid.serialize() Daniel@0: }; Daniel@0: Daniel@0: if (this.attributes.musicRecordingsGridIsShown) { Daniel@0: result.musicRecordingsGridIsShown = true; Daniel@0: } Daniel@0: Daniel@0: return result; Daniel@0: }, Daniel@0: Daniel@0: /** Daniel@0: * @memberOf App.ContextModule.State Daniel@0: */ Daniel@0: unserialize: function(serializedAttributes) { Daniel@0: logger.debug("method called: State::unserialize"); Daniel@0: this._modificationPropagationEnabled = false; Daniel@0: this._attributesWereModified = false; Daniel@0: Daniel@0: var fixedSerializedAttributes = serializedAttributes; Daniel@0: if (!_.isSimpleObject(fixedSerializedAttributes)) { Daniel@0: logger.warn("State::unserialize called for not an object: ", serializedAttributes); Daniel@0: fixedSerializedAttributes = {}; Daniel@0: } Daniel@0: this.attributes.musicCollectionGrid.unserialize(fixedSerializedAttributes.musicCollectionGrid); Daniel@0: this.attributes.musicRecordingGrid.unserialize(fixedSerializedAttributes.musicRecordingGrid); Daniel@0: Daniel@0: if (this.attributes.musicRecordingsGridIsShown !== fixedSerializedAttributes.musicRecordingsGridIsShown) { Daniel@0: this._attributesWereModified = true; Daniel@0: } Daniel@0: if (fixedSerializedAttributes.musicRecordingsGridIsShown) { Daniel@0: this.attributes.musicRecordingsGridIsShown = true; Daniel@0: } else { Daniel@0: delete this.attributes.musicRecordingsGridIsShown; Daniel@0: } Daniel@0: Daniel@0: this._triggerModificationEventsIfNeeded(); Daniel@0: this._modificationPropagationEnabled = true; Daniel@0: }, Daniel@0: Daniel@0: _registerModificationOfAttribute: function() { Daniel@0: this._attributesWereModified = true; Daniel@0: if (this._modificationPropagationEnabled) { Daniel@0: this._triggerModificationEventsIfNeeded(); Daniel@0: }; Daniel@0: }, Daniel@0: Daniel@0: _triggerModificationEventsIfNeeded: function() { Daniel@0: if (this._attributesWereModified) { Daniel@0: this.trigger("change"); Daniel@0: } Daniel@0: this._attributesWereModified = false; Daniel@0: }, Daniel@0: }); Daniel@0: Daniel@0: }); Daniel@0: }, Logger);