forked from fex-team/kityminder-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.service.js
62 lines (59 loc) · 1.84 KB
/
memory.service.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* @fileOverview
*
* UI 状态的 LocalStorage 的存取文件,未来可能在离线编辑的时候升级
*
* @author: zhangbobell
* @email : [email protected]
*
* @copyright: Baidu FEX, 2015
*/
angular.module('kityminderEditor')
.service('memory', function() {
function isQuotaExceeded(e) {
var quotaExceeded = false;
if (e) {
if (e.code) {
switch (e.code) {
case 22:
quotaExceeded = true;
break;
case 1014:
// Firefox
if (e.name === 'NS_ERROR_DOM_QUOTA_REACHED') {
quotaExceeded = true;
}
break;
}
} else if (e.number === -2147024882) {
// Internet Explorer 8
quotaExceeded = true;
}
}
return quotaExceeded;
}
return {
get: function(key) {
var value = window.localStorage.getItem(key);
return null || JSON.parse(value);
},
set: function(key, value) {
try {
window.localStorage.setItem(key, JSON.stringify(value));
return true;
} catch(e) {
if (isQuotaExceeded(e)) {
return false;
}
}
},
remove: function(key) {
var value = window.localStorage.getItem(key);
window.localStorage.removeItem(key);
return value;
},
clear: function() {
window.localStorage.clear();
}
}
});