-
Notifications
You must be signed in to change notification settings - Fork 8
/
impact-storage.js
97 lines (79 loc) · 2.16 KB
/
impact-storage.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
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
/**
* @impact-storage.js
* @version: 1.01
* @author: Jordan Santell
* @date: October 2011
* @copyright (c) 2011 Jordan Santell, under The MIT License (see LICENSE)
*
* ImpactStorage is a plugin for HTML5/js game framework ImpactJS, giving
* developers an easy-to-use interface to localStorage for their projects.
*/
ig.module(
'plugins.impact-storage'
)
.requires(
'impact.game'
)
.defines(function(){
ig.Storage = ig.Class.extend({
staticInstantiate: function(i) {
return !ig.Storage.instance ? null : ig.Storage.instance;
},
init: function() {
ig.Storage.instance = this;
},
isCapable: function() {
return !(typeof(window.localStorage) === 'undefined');
},
isSet: function(key) {
return !(this.get(key) === null);
},
initUnset: function(key, value) {
if (this.get(key) === null) this.set(key, value);
},
get: function(key) {
if (!this.isCapable()) return null;
try {
return JSON.parse(localStorage.getItem(key));
} catch(e) {
return window.localStorage.getItem(key);
}
},
getInt: function(key) {
return ~~this.get(key);
},
getFloat: function(key) {
return parseFloat(this.get(key));
},
getBool: function(key) {
return !!this.get(key);
},
key: function(n) {
return this.isCapable() ? window.localStorage.key(n) : null;
},
set: function(key, value) {
if (!this.isCapable()) return null;
try {
window.localStorage.setItem(key, JSON.stringify(value));
} catch(e) {
if(e == QUOTA_EXCEEDED_ERR)
console.log('localStorage quota exceeded');
}
},
setHighest: function(key, value) {
if(value > this.getFloat(key)){
this.set(key, value);
return true;
}
return false;
},
remove: function(key) {
if (!this.isCapable()) return null;
window.localStorage.removeItem(key);
},
clear: function() {
if (!this.isCapable()) return null;
window.localStorage.clear();
}
});
});