-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAASWebStorageHandler.js
242 lines (213 loc) · 6.67 KB
/
AASWebStorageHandler.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
* Copyright (c) 2021 Lenze SE
* SPDX-License-Identifier: Apache-2.0
*/
class AASWebStorageHandler {
constructor() {
this.setCurrentAAS = this.setCurrentAAS.bind(this);
this.setCurrentRegistry = this.setCurrentRegistry.bind(this);
this.getCurrentAAS = this.getCurrentAAS.bind(this);
this.getCurrentRegistry = this.getCurrentRegistry.bind(this);
this.addAASURL = this.addAASURL.bind(this);
this.addRegistryURL = this.addRegistryURL.bind(this);
this.addURLToMap = this.addURLToMap.bind(this);
this.removeAASURL = this.removeAASURL.bind(this);
this.removeRegistryURL = this.removeRegistryURL.bind(this);
this.removeURLFromMap = this.removeURLFromMap.bind(this);
this.removeAASHost = this.removeAASHost.bind(this);
this.removeRegistryHost = this.removeRegistryHost.bind(this);
this.removeHostFromMap = this.removeHostFromMap.bind(this);
this.AASHostExists = this.AASHostExists.bind(this);
this.registryHostExists = this.registryHostExists.bind(this);
this.hostExists = this.hostExists.bind(this);
this.getAASMap = this.getAASMap.bind(this);
this.getRegistryMap = this.getRegistryMap.bind(this);
this.readMapFromStorage = this.readMapFromStorage.bind(this);
this.writeAASMap = this.writeAASMap.bind(this);
this.writeRegistryMap = this.writeRegistryMap.bind(this);
this.writeMap = this.writeMap.bind(this);
this.aasStorageSet = "aasMap";
this.registryStorageSet = "registryMap";
this.currentAAS = "currentAAS";
this.currentRegistry = "currentRegistry";
this.aasMap = null;
this.registryMap = null;
this.getAASMap();
this.getRegistryMap();
}
setCurrentAAS(aasURL) {
window.localStorage.setItem(this.currentAAS, aasURL);
this.addAASURL(aasURL);
}
setCurrentRegistry(registryURL) {
window.localStorage.setItem(this.currentRegistry, registryURL);
this.addRegistryURL(registryURL);
}
getCurrentAAS() {
var storageData = window.localStorage.getItem(this.currentAAS);
if (storageData)
return storageData;
return null;
}
getCurrentRegistry() {
var storageData = window.localStorage.getItem(this.currentRegistry);
if (storageData)
return storageData;
return null;
}
addAASURL(aasURL, skipCommit = false) {
var ret = this.addURLToMap(aasURL, this.aasMap);
if (ret)
this.aasMap = ret;
if (!skipCommit)
this.writeAASMap();
if (ret)
return true;
return false;
}
addRegistryURL(registryURL, skipCommit = false) {
var ret = this.addURLToMap(registryURL, this.registryMap);
if (ret)
this.registryMap = ret;
if (!skipCommit)
this.writeRegistryMap();
if (ret)
return true;
return false;
}
addURLToMap(URL_, map) {
var url = new URL(URL_);
var hostMap = null;
if (this.hostExists(url.origin, map)) {
hostMap = map.get(url.origin);
}
else {
hostMap = new Map();
map.set(url.origin, hostMap);
var sortArr = Array.from(map);
sortArr.sort();
map = new Map(sortArr);
}
if (hostMap.has(url.pathname))
return null;
hostMap.set(url.pathname, url.href);
var sortArr = Array.from(hostMap);
sortArr.sort();
hostMap = new Map(sortArr);
map.set(url.origin, hostMap);
return map;
}
removeAASURL(aasURL, skipCommit = false) {
var ret = this.removeURLFromMap(aasURL, this.aasMap);
if (ret)
this.aasMap = ret;
if (!skipCommit)
this.writeAASMap();
if (ret)
return true;
return false;
}
removeRegistryURL(registryURL, skipCommit = false) {
var ret = this.removeURLFromMap(registryURL, this.registryMap);
if (ret)
this.registryMap = ret;
if (!skipCommit)
this.writeRegistryMap();
if (ret)
return true;
return false;
}
removeURLFromMap(URL_, map) {
var url = new URL(URL_);
var hostMap = null;
if (!this.hostExists(url.origin, map))
return null;
hostMap = map.get(url.origin);
if (!hostMap.has(url.pathname))
return null;
hostMap.delete(url.pathname, url.href);
if (hostMap.size == 0)
map.delete(url.origin);
return map;
}
removeAASHost(hostURL, skipCommit = false) {
var ret = this.removeHostFromMap(hostURL, this.aasMap);
if (ret)
this.registryMap = ret;
if (!skipCommit)
this.writeAASMap();
if (ret)
return true;
return false;
}
removeRegistryHost(registryURL, skipCommit = false) {
var ret = this.removeHostFromMap(registryURL, this.registryMap);
if (ret)
this.registryMap = ret;
if (!skipCommit)
this.writeRegistryMap();
if (ret)
return true;
return false;
}
removeHostFromMap(URL_, map) {
var url = new URL(URL_);
if (!this.hostExists(url.origin, map))
return null;
map.delete(url.origin);
return map;
}
AASHostExists(hostURL, map) {
return this.hostExists(hostURL, this.aasMap);
}
registryHostExists(hostURL, map) {
return this.hostExists(hostURL, this.registryMap);
}
hostExists(hostURL, map) {
var url = new URL(hostURL);
if (map.has(url.origin))
return true;
return false;
}
getAASMap() {
if (this.aasMap)
return this.aasMap;
this.aasMap = this.readMapFromStorage(this.aasStorageSet);
return this.aasMap;
}
getRegistryMap() {
if (this.registryMap)
return this.registryMap;
this.registryMap =
this.readMapFromStorage(this.registryStorageSet);
return this.registryMap;
}
readMapFromStorage(storageIndicator) {
var storageData = window.localStorage.getItem(storageIndicator);
if (storageData) {
try {
var map = new Map(Object.entries(JSON.parse(storageData)));
for (var [key, value] of map)
map.set(key, new Map(Object.entries(value)));
return map;
}
catch(e) {
return new Map();
}
}
return new Map();
}
writeAASMap() {
this.writeMap(this.aasMap, this.aasStorageSet);
}
writeRegistryMap() {
this.writeMap(this.registryMap, this.registryStorageSet);
}
writeMap(map, storageIndicator) {
var map = new Map(map);
for (var [key, value] of map)
map.set(key, Object.fromEntries(value));
window.localStorage.setItem(storageIndicator,
JSON.stringify(Object.fromEntries(map)));
}
}