2 * Copyright (c) 2014 DataTorrent, Inc. ALL Rights Reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 angular.module('ui.dashboard')
20 .factory('DashboardState', ['$log', '$q', function ($log, $q) {
21 function DashboardState(storage, id, hash, widgetDefinitions, stringify) {
22 this.storage = storage;
25 this.widgetDefinitions = widgetDefinitions;
26 this.stringify = stringify;
29 DashboardState.prototype = {
31 * Takes array of widget instance objects, serializes,
34 * @param {Array} widgets scope.widgets from dashboard directive
35 * @return {Boolean} true on success, false on failure
37 save: function (widgets) {
43 var serialized = _.map(widgets, function (widget) {
44 return widget.serialize();
47 var item = { widgets: serialized, hash: this.hash };
50 item = JSON.stringify(item);
53 this.storage.setItem(this.id, item);
58 * Loads dashboard state from the storage object.
59 * Can handle a synchronous response or a promise.
61 * @return {Array|Promise} Array of widget definitions or a promise
71 // try loading storage item
72 serialized = this.storage.getItem( this.id );
76 if (angular.isObject(serialized) && angular.isFunction(serialized.then)) {
77 return this._handleAsyncLoad(serialized);
79 // otherwise handle synchronous load
80 return this._handleSyncLoad(serialized);
86 _handleSyncLoad: function(serialized) {
88 var deserialized, result = [];
95 try { // to deserialize the string
97 deserialized = JSON.parse(serialized);
101 // bad JSON, log a warning and return
102 $log.warn('Serialized dashboard state was malformed and could not be parsed: ', serialized);
108 deserialized = serialized;
111 // check hash against current hash
112 if (deserialized.hash !== this.hash) {
114 $log.info('Serialized dashboard from storage was stale (old hash: ' + deserialized.hash + ', new hash: ' + this.hash + ')');
115 this.storage.removeItem(this.id);
121 var savedWidgetDefs = deserialized.widgets;
123 // instantiate widgets from stored data
124 for (var i = 0; i < savedWidgetDefs.length; i++) {
126 // deserialized object
127 var savedWidgetDef = savedWidgetDefs[i];
129 // widget definition to use
130 var widgetDefinition = this.widgetDefinitions.getByName(savedWidgetDef.name);
132 // check for no widget
133 if (!widgetDefinition) {
134 // no widget definition found, remove and return false
135 $log.warn('Widget with name "' + savedWidgetDef.name + '" was not found in given widget definition objects');
139 // check widget-specific storageHash
140 if (widgetDefinition.hasOwnProperty('storageHash') && widgetDefinition.storageHash !== savedWidgetDef.storageHash) {
141 // widget definition was found, but storageHash was stale, removing storage
142 $log.info('Widget Definition Object with name "' + savedWidgetDef.name + '" was found ' +
143 'but the storageHash property on the widget definition is different from that on the ' +
144 'serialized widget loaded from storage. hash from storage: "' + savedWidgetDef.storageHash + '"' +
145 ', hash from WDO: "' + widgetDefinition.storageHash + '"');
149 // push instantiated widget to result array
150 result.push(savedWidgetDef);
156 _handleAsyncLoad: function(promise) {
158 var deferred = $q.defer();
162 var result = self._handleSyncLoad(res);
164 deferred.resolve(result);
166 deferred.reject(result);
171 deferred.reject(res);
175 return deferred.promise;
179 return DashboardState;