File size: 1,502 Bytes
29a5ed9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
window.state = window.state || {};
state = window.state;

state.Store = function Store (prefix) {
    this.prefix = state.constants.LS_PREFIX + (prefix ? prefix + '-' : '');
};

state.Store.prototype.set = function (key, value) {
    if (key.startsWith(this.prefix)) {
        localStorage.setItem(key, value);
    } else {
        localStorage.setItem(this.prefix + key, value);
    }
};

state.Store.prototype.get = function (key) {
    return localStorage.getItem(this.prefix + key);
};

state.Store.prototype.remove = function (key) {
    localStorage.removeItem(this.prefix + key);
};

state.Store.prototype.clear = function () {
    localStorage.clear();
};

state.Store.prototype.clearAll = function () {
    let keys = Object.keys(localStorage);
    for (let i = 0; i < keys.length; i++) {
        if (keys[i].startsWith(state.constants.LS_PREFIX)) {
            localStorage.removeItem(keys[i]);
        }
    }
};

state.Store.prototype.getAll = function () {
    let result = {};
    let keys = Object.keys(localStorage);
    for (let i = 0; i < keys.length; i++) {
        if (keys[i].startsWith(state.constants.LS_PREFIX)) {
            result[keys[i]] = localStorage[keys[i]];
        }
    }
    return result;
};

state.Store.prototype.load = function (json) {
    this.clearAll();
    let keys = Object.keys(json);
    for (let i = 0; i < keys.length; i++) {
        if (keys[i].startsWith(state.constants.LS_PREFIX)) {
            this.set(keys[i], json[keys[i]]);
        }
    }
};