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('LayoutStorage', function() {
29 removeItem: function() {
36 function LayoutStorage(options) {
41 stringifyStorage: true
44 angular.extend(defaults, options);
45 angular.extend(options, defaults);
47 this.id = options.storageId;
48 this.storage = options.storage;
49 this.storageHash = options.storageHash;
50 this.stringifyStorage = options.stringifyStorage;
51 this.widgetDefinitions = options.widgetDefinitions;
52 this.defaultLayouts = options.defaultLayouts;
53 this.lockDefaultLayouts = options.lockDefaultLayouts;
54 this.widgetButtons = options.widgetButtons;
55 this.explicitSave = options.explicitSave;
56 this.defaultWidgets = options.defaultWidgets;
57 this.settingsModalOptions = options.settingsModalOptions;
58 this.onSettingsClose = options.onSettingsClose;
59 this.onSettingsDismiss = options.onSettingsDismiss;
60 this.options = options;
61 this.options.unsavedChangeCount = 0;
66 this._ensureActiveLayout();
69 LayoutStorage.prototype = {
71 add: function(layouts) {
72 if (!angular.isArray(layouts)) {
76 angular.forEach(layouts, function(layout) {
77 layout.dashboard = layout.dashboard || {};
78 layout.dashboard.storage = self;
79 layout.dashboard.storageId = layout.id = self._getLayoutId.call(self,layout);
80 layout.dashboard.widgetDefinitions = layout.widgetDefinitions || self.widgetDefinitions;
81 layout.dashboard.stringifyStorage = false;
82 layout.dashboard.defaultWidgets = layout.defaultWidgets || self.defaultWidgets;
83 layout.dashboard.widgetButtons = self.widgetButtons;
84 layout.dashboard.explicitSave = self.explicitSave;
85 layout.dashboard.settingsModalOptions = self.settingsModalOptions;
86 layout.dashboard.onSettingsClose = self.onSettingsClose;
87 layout.dashboard.onSettingsDismiss = self.onSettingsDismiss;
88 self.layouts.push(layout);
92 remove: function(layout) {
93 var index = this.layouts.indexOf(layout);
95 this.layouts.splice(index, 1);
96 delete this.states[layout.id];
99 if (layout.active && this.layouts.length) {
100 var nextActive = index > 0 ? index - 1 : 0;
101 this.layouts[nextActive].active = true;
109 layouts: this._serializeLayouts(),
111 storageHash: this.storageHash
114 if (this.stringifyStorage) {
115 state = JSON.stringify(state);
118 this.storage.setItem(this.id, state);
119 this.options.unsavedChangeCount = 0;
124 var serialized = this.storage.getItem(this.id);
130 if (angular.isObject(serialized) && angular.isFunction(serialized.then)) {
131 this._handleAsyncLoad(serialized);
133 this._handleSyncLoad(serialized);
136 this._addDefaultLayouts();
145 setItem: function(id, value) {
146 this.states[id] = value;
150 getItem: function(id) {
151 return this.states[id];
154 removeItem: function(id) {
155 delete this.states[id];
159 getActiveLayout: function() {
160 var len = this.layouts.length;
161 for (var i = 0; i < len; i++) {
162 var layout = this.layouts[i];
170 _addDefaultLayouts: function() {
172 var defaults = this.lockDefaultLayouts ? { locked: true } : {};
173 angular.forEach(this.defaultLayouts, function(layout) {
174 self.add(angular.extend(_.clone(defaults), layout));
178 _serializeLayouts: function() {
180 angular.forEach(this.layouts, function(l) {
186 defaultWidgets: l.dashboard.defaultWidgets
192 _handleSyncLoad: function(serialized) {
196 if (this.stringifyStorage) {
199 deserialized = JSON.parse(serialized);
202 this._addDefaultLayouts();
207 deserialized = serialized;
211 if (this.storageHash !== deserialized.storageHash) {
212 this._addDefaultLayouts();
215 this.states = deserialized.states;
216 this.add(deserialized.layouts);
219 _handleAsyncLoad: function(promise) {
222 angular.bind(self, this._handleSyncLoad),
223 angular.bind(self, this._addDefaultLayouts)
227 _ensureActiveLayout: function() {
228 for (var i = 0; i < this.layouts.length; i++) {
229 var layout = this.layouts[i];
234 if (this.layouts[0]) {
235 this.layouts[0].active = true;
239 _getLayoutId: function(layout) {
244 for (var i = 0; i < this.layouts.length; i++) {
245 var id = this.layouts[i].id;
246 max = Math.max(max, id * 1);
252 return LayoutStorage;