/
Cache.js
88 lines (76 loc) · 2.16 KB
/
Cache.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
function Cache(name, Model) {
this.name = name;
this.Model = Model;
this.mem = Cache.load(name, Model) || {};
}
Cache.load = function(cache, Model) {
var store;
try {
store = JSON.parse(localStorage[cache]);
if(Model) Object.keys(store).forEach((o) => new Model(store[o]))
} catch(err) {
// console.log('could not parse json when loading', cache, err);
}
return store;
}
Cache.save = function(cache, data) {
localStorage[cache] = JSON.stringify(data);
}
Cache.get = function(cache, name) {
var store;
try {
store = JSON.parse(localStorage[cache]);
} catch(err) {
console.log('could not parse json');
}
return store && store[name];
}
Cache.set = function(cache, name, value) {
if(!localStorage[cache]){
localStorage[cache] = "{}";
}
var store = JSON.parse(localStorage[cache]);
store[name] = value;
localStorage[cache] = JSON.stringify(store);
}
Cache.remove = function(cache, name) {
var c = Cache.load(cache);
delete c[name];
Cache.save(cache, c);
}
Cache.prototype.get = function(key) {
return this.mem[key] || Cache.get(this.name, key);
}
Cache.prototype.set = function(key, val) {
this.mem[key] = val;
return Cache.set(this.name, key, val);
}
Cache.prototype.remove = function(key) {
var yes = confirm('Sure you want to remove this?');
if(yes) {
delete this.mem[key];
Cache.remove(this.name, key);
}
return yes;
}
Cache.prototype.render = function() {
Object.keys(this.mem).forEach((key) => new this.Model(this.mem[key]).render() );
return this;
}
Cache.prototype.renderList = function(container, onSelect) {
var store = this.mem || Cache.load(this.name);
if(!store) return;
var list = Object.keys(store).map((name) => {
var div = document.createElement('div');
div.textContent = name;
div.style.cursor = 'pointer';
div.addEventListener('click', () => {
div.style.backgroundColor = 'lightgray';
list.forEach((d) => delete div.style.backgroundColor)
onSelect(store[name])
}, false);
if(container) container.appendChild(div);
return div;
})
return list;
}