-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsessionManager.js
47 lines (41 loc) · 1.82 KB
/
sessionManager.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
app.factory('sessionManager', function () {
return {
/**
* Starts counting how many tabs of this system the user has opened. The count decreases as user closes the tabs.
* And when the count reaches zero, it clears the localStorage of this domain.
*
* Note it has to run in pure javascript, cause services ($window, $localStorage) won't run before the window/tab
* closes. Also, it won't even try to run if the browser doesn't support localStorage.
*
* @param {String} [prefix] If defined, only localStorage items with that prefix will be removed. Otherwise, localStorage will be completely cleared.
*/
start: function (prefix) {
if (!window.localStorage) {
return;
}
var key = "_openTabs";
if (prefix) {
key += '_' + prefix;
}
if (!window.localStorage[key] || isNaN(window.localStorage[key]) || window.localStorage[key] < 0) {
window.localStorage[key] = 0;
}
window.localStorage[key]++;
window.addEventListener('beforeunload', function () {
window.localStorage[key]--;
if (window.localStorage[key] === 0) {
if (!prefix) {
window.localStorage.clear();
} else {
for (var item in window.localStorage) {
if (window.localStorage.hasOwnProperty(item) && item.substring(0, prefix.length) === prefix) {
delete window.localStorage[item];
delete window.localStorage[key];
}
}
}
}
});
}
};
});