summaryrefslogtreecommitdiffstats
path: root/web/react/stores/browser_store.jsx
blob: 4d6eb0b8db38f68a8dc7cf50b61a20017b23458e (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
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
// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
// See License.txt for license information.

var UserStore;
function getPrefix() {
    if (!UserStore) UserStore = require('./user_store.jsx');
    return UserStore.getCurrentId() + '_';
}

// Also change model/utils.go ETAG_ROOT_VERSION
var BROWSER_STORE_VERSION = '.3';

module.exports = {
    _initialized: false,

    _initialize: function() {
        var currentVersion = localStorage.getItem("local_storage_version");
        if (currentVersion !== BROWSER_STORE_VERSION) {
            this.clear();
            localStorage.setItem("local_storage_version", BROWSER_STORE_VERSION);
        }
        this._initialized = true;
    },

    getItem: function(name, defaultValue) {
        return this.getGlobalItem(getPrefix() + name, defaultValue);
    },

    setItem: function(name, value) {
        this.setGlobalItem(getPrefix() + name, value);
    },

    removeItem: function(name) {
        if (!this._initialized) this._initialize();

        localStorage.removeItem(getPrefix() + name);
    },

    setGlobalItem: function(name, value) {
        if (!this._initialized) this._initialize();

        localStorage.setItem(name, JSON.stringify(value));
    },

    getGlobalItem: function(name, defaultValue) {
        if (!this._initialized) this._initialize();

        var result = null;
        try {
            result = JSON.parse(localStorage.getItem(name));
        } catch (err) {}

        if (result === null && typeof defaultValue !== 'undefined') {
            result = defaultValue;
        }

        return result;
    },

    removeGlobalItem: function(name) {
        if (!this._initialized) this._initialize();

        localStorage.removeItem(name);
    },

    clear: function() {
        localStorage.clear();
        sessionStorage.clear();
    },

    /**
     * Preforms the given action on each item that has the given prefix
     * Signiture for action is action(key, value)
     */
    actionOnItemsWithPrefix: function (prefix, action) {
        if (!this._initialized) this._initialize();

        var globalPrefix = getPrefix();
        var globalPrefixiLen = globalPrefix.length;
        for (var key in localStorage) {
            if (key.lastIndexOf(globalPrefix + prefix, 0) === 0) {
                var userkey = key.substring(globalPrefixiLen);
                action(userkey, this.getGlobalItem(key));
            }
        }
    },

    isLocalStorageSupported: function() {
        try {
            sessionStorage.setItem("testSession", '1');
            sessionStorage.removeItem("testSession");

            localStorage.setItem("testLocal", '1');
            if (localStorage.getItem("testLocal") != '1') {
                return false;
            }
            localStorage.removeItem("testLocal", '1');

            return true;
        } catch (e) {
            return false;
        }
    }
};