summaryrefslogtreecommitdiffstats
path: root/theme/javascript/storage.js
blob: 7a7643cd7e9d14e1ad0cb845d49e336075cd1a5a (plain)
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
var baseKey = '';

/*
 * Simple module for storing data in the browser's local storage
 */
module.exports = {
    setBaseKey: function(key) {
        baseKey = key;
    },

    // Write something in localstorage
    set: function(key, value) {
        key = baseKey+':'+key;

        try {
            localStorage[key] = JSON.stringify(value);
        } catch(e) {}
    },

    // Read a value from localstorage
    get: function(key, def) {
        key = baseKey+':'+key;
        if (localStorage[key] === undefined) return def;
        try {
            var v = JSON.parse(localStorage[key]);
            return v == null ? def : v;;
        } catch(err) {
            return localStorage[key] || def;
        }
    },

    // Remove a key from localstorage
    remove: function(key) {
        key = baseKey+':'+key;
        localStorage.removeItem(key);
    }
};