forked from qishibo/AnotherRedisDesktopManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.js
66 lines (52 loc) · 1.66 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
export default {
getSetting(key) {
let settings = localStorage.getItem('settings');
settings = settings ? JSON.parse(settings) : {};
return key ? settings[key] : settings;
},
addConnection(connection) {
const connections = this.getConnections();
const key = this.getConnectionKey(connection);
if (connections[key]) {
return false;
}
connections[key] = connection;
this.setConnections(connections);
},
getConnections(returnList = false) {
let connections = localStorage.connections || '{}';
connections = JSON.parse(connections);
connections = this.sortByKey(connections);
if (returnList) {
connections = Object.keys(connections).map(key => connections[key]);
}
return connections;
},
editConnection(oldConnection, newConnection) {
const connections = this.getConnections();
const oldKey = this.getConnectionKey(oldConnection);
if (!connections[oldKey]) {
return false;
}
delete connections[oldKey];
const newKey = this.getConnectionKey(newConnection);
connections[newKey] = newConnection;
this.setConnections(connections);
},
setConnections(connections) {
localStorage.connections = JSON.stringify(connections);
},
deleteConnection(connection) {
const connections = this.getConnections();
const key = this.getConnectionKey(connection);
delete connections[key];
this.setConnections(connections);
},
getConnectionKey(connection) {
return connection.host + connection.port + connection.name;
},
sortByKey(obj) {
return Object.keys(obj).sort()
.reduce((acc, c) => { acc[c] = obj[c]; return acc; }, {});
},
};