-
Notifications
You must be signed in to change notification settings - Fork 1
/
storage.js
197 lines (161 loc) · 4.35 KB
/
storage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
var storageService = (function(_) {
var STORAGE_KEYS = {
CONNECTIONS: 'connections',
STATE: 'state',
WIDGETS: 'widgets',
DASHBOARDS: 'dashboards'
};
function _save(key, data) {
var value = data;
if(typeof data !== 'string') {
value = JSON.stringify(data);
}
localStorage.setItem(key, value);
}
function _get(key) {
var value = localStorage.getItem(key);
if(!value) {
return {};
}
return JSON.parse(value);
}
function saveConnection(connection) {
var id = Date.now();
var connections = _get(STORAGE_KEYS.CONNECTIONS) || {};
connections[id] = connection;
_save(STORAGE_KEYS.CONNECTIONS, connections);
return id;
}
function getConnection(id) {
var connections = _get(STORAGE_KEYS.CONNECTIONS) || {};
return connections[id];
}
function getConnections() {
return _get(STORAGE_KEYS.CONNECTIONS) || {};
}
function deleteConnection(id) {
var connections = _get(STORAGE_KEYS.CONNECTIONS);
if(!connections[id]) {
return;
}
delete connections[id];
_save(STORAGE_KEYS.CONNECTIONS, connections);
}
function setState(data) {
var current = getState() || {};
var newState = Object.assign(current, data);
_save(STORAGE_KEYS.STATE, newState);
}
function getState() {
return _get(STORAGE_KEYS.STATE) || {};
}
function saveWidget(data) {
var id = Date.now();
var widgets = _get(STORAGE_KEYS.WIDGETS) || {};
widgets[id] = data;
_save(STORAGE_KEYS.WIDGETS, widgets);
return id;
}
function saveDashboard(data) {
var id = Date.now();
var dashboards = _get(STORAGE_KEYS.DASHBOARDS) || {};
dashboards[id] = data;
_save(STORAGE_KEYS.DASHBOARDS, dashboards);
return id;
}
function getDashboard(id) {
var dashboards = _get(STORAGE_KEYS.DASHBOARDS) || {};
return dashboards[id];
}
function getDashboards() {
return _get(STORAGE_KEYS.DASHBOARDS) || {};
}
function deleteDashboard(id) {
var dashboards = _get(STORAGE_KEYS.DASHBOARDS);
if (!dashboards[id]) {
return;
}
delete dashboards[id];
_save(STORAGE_KEYS.DASHBOARDS, dashboards);
}
function updateConnection(id, data) {
var connections = _get(STORAGE_KEYS.CONNECTIONS);
if(!connections[id]) {
var newId = saveConnection(data);
return connections[newId];
}
var updatedConnection = Object.assign(connections[id], data);
connections[id] = updatedConnection;
_save(STORAGE_KEYS.CONNECTIONS, connections);
return updatedConnection;
}
function updateDashboard(id, data) {
var dashboards = _get(STORAGE_KEYS.DASHBOARDS);
if(!dashboards[id]) {
return;
}
var updatedDashboard = Object.assign(dashboards[id], data);
dashboards[id] = updatedDashboard;
_save(STORAGE_KEYS.DASHBOARDS, dashboards);
return updatedDashboard;
}
function updateWidget(id, data) {
var widgets = _get(STORAGE_KEYS.WIDGETS);
if (!widgets[id]) {
var newId = saveWidget(data);
return widgets[newId];
}
var updatedWidget = Object.assign(widgets[id], data);
widgets[id] = updatedWidget;
_save(STORAGE_KEYS.WIDGETS, widgets);
return updatedWidget;
}
function getWidget(id) {
var widgets = _get(STORAGE_KEYS.WIDGETS);
return widgets[id];
}
function getWidgetsByDashboardId(dashboardId) {
var widgets = _get(STORAGE_KEYS.WIDGETS);
var dashboard = getDashboard(dashboardId);
if(!dashboard) {
return [];
}
var dashboardWidgetIds = dashboard.widgets;
return _.pick(widgets, dashboardWidgetIds);
}
function deleteWidget(id) {
var widgets = _get(STORAGE_KEYS.WIDGETS);
if (!widgets[id]) {
return;
}
delete widgets[id];
_save(STORAGE_KEYS.WIDGETS, widgets);
}
return {
connections: {
save: saveConnection,
getAll: getConnections,
get: getConnection,
delete: deleteConnection,
update: updateConnection
},
state: {
get: getState,
set: setState
},
widgets: {
save: saveWidget,
get: getWidget,
getByDashboardId: getWidgetsByDashboardId,
update: updateWidget,
delete: deleteWidget
},
dashboards: {
save: saveDashboard,
getAll: getDashboards,
get: getDashboard,
delete: deleteDashboard,
update: updateDashboard
}
};
})(_)