summaryrefslogtreecommitdiffstats
path: root/web/react/stores/browser_store.jsx
blob: 82cf9a9423f72a774911565dad76e1253070b8a8 (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
// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved.
// See License.txt for license information.
var UserStore = require('../stores/user_store.jsx');

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

var _initialized = false;

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

module.exports.setItem = function(name, value) {
    if (!_initialized) _initialize();
	var user_id = UserStore.getCurrentId();
	localStorage.setItem(user_id + "_" + name, value);
};

module.exports.getItem = function(name) {
    if (!_initialized) _initialize();
	var user_id = UserStore.getCurrentId();
	return localStorage.getItem(user_id + "_" + name);
};

module.exports.removeItem = function(name) {
    if (!_initialized) _initialize();
	var user_id = UserStore.getCurrentId();
	localStorage.removeItem(user_id + "_" + name);
};

module.exports.setGlobalItem = function(name, value) {
    if (!_initialized) _initialize();
	localStorage.setItem(name, value);
};

module.exports.getGlobalItem = function(name) {
    if (!_initialized) _initialize();
	return localStorage.getItem(name);
};

module.exports.removeGlobalItem = function(name) {
    if (!_initialized) _initialize();
	localStorage.removeItem(name);
};

module.exports.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)
module.exports.actionOnItemsWithPrefix = function (prefix, action) {
	var user_id = UserStore.getCurrentId();
	var id_len = user_id.length;
	var prefix_len = prefix.length;
    for (var key in localStorage) {
        if (key.substring(id_len, id_len + prefix_len) === prefix) {
			var userkey = key.substring(id_len);
			action(userkey, BrowserStore.getItem(key));
        }
    }
};

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

        localStorage.setItem("testLocal", '1');
        localStorage.removeItem("testLocal", '1');

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