-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstore.js
More file actions
185 lines (153 loc) · 4.64 KB
/
store.js
File metadata and controls
185 lines (153 loc) · 4.64 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
## store.js by Frank Kohlhepp
## Copyright (c) 2011 - 2012 Frank Kohlhepp
## https://github.com/frankkohlhepp/store-js
## License: MIT-license
*/
(function () {
var has = function (object, key) {
return Object.prototype.hasOwnProperty.call(object, key);
};
var objectGetLength = function (object) {
var count = 0;
for (var key in object) {
if (has(object, key)) { count++; }
}
return count;
};
var arrayIndexOf = function (array, item, from) {
var length = array.length >>> 0;
for (var i = (from < 0) ? Math.max(0, length + from) : from || 0; i < length; i++) {
if (array[i] === item) { return i; }
}
return -1;
};
var arrayContains = function (array, item, from) {
return arrayIndexOf(array, item, from) !== -1;
};
var arrayInclude = function (array, item) {
if (!arrayContains(array, item)) { array.push(item); }
return array;
};
var Store = this.Store = function (name, defaults, watcherSpeed) {
this.name = name;
this.defaults = defaults || {};
this.watcherSpeed = watcherSpeed || 500;
this.listeners = {};
// Apply defaults
this.applyDefaults();
};
Store.clear = function () {
localStorage.clear();
};
Store.prototype.applyDefaults = function () {
for (var key in this.defaults) {
if (has(this.defaults, key) && this.get(key) === undefined) {
this.set(key, this.defaults[key]);
}
}
return this;
};
Store.prototype.watcher = function (force) {
if (this.watcherTimer) {
clearTimeout(this.watcherTimer);
}
if (objectGetLength(this.listeners) || force) {
this.newObject = this.toObject();
if (this.oldObject) {
for (var key in this.newObject) {
if (has(this.newObject, key) && this.newObject[key] !== this.oldObject[key]) {
this.fireEvent(key, this.newObject[key]);
}
}
for (var key in this.oldObject) {
if (has(this.oldObject, key) && !has(this.newObject, key)) {
this.fireEvent(key, this.newObject[key]);
}
}
}
this.oldObject = this.newObject;
var that = this;
this.watcherTimer = setTimeout(function () {
that.watcher();
}, this.watcherSpeed);
}
return this;
};
Store.prototype.get = function (name) {
var value = localStorage.getItem("store." + this.name + "." + name);
if (value === null) { return undefined; }
try { return JSON.parse(value); } catch (e) { return null; }
};
Store.prototype.set = function (name, value) {
if (value === undefined) {
this.remove(name);
} else {
if (typeof value === "function") { value = null; }
try { value = JSON.stringify(value); } catch (e) { value = null; }
localStorage.setItem("store." + this.name + "." + name, value);
}
return this;
};
Store.prototype.remove = function (name) {
localStorage.removeItem("store." + this.name + "." + name);
return this.applyDefaults();
};
Store.prototype.reset = function () {
var name = "store." + this.name + ".";
for (var i = (localStorage.length - 1); i >= 0; i--) {
if (localStorage.key(i).substring(0, name.length) === name) {
localStorage.removeItem(localStorage.key(i));
}
}
return this.applyDefaults();
};
Store.prototype.toObject = function () {
var values = {};
var name = "store." + this.name + ".";
for (var i = (localStorage.length - 1); i >= 0; i--) {
if (localStorage.key(i).substring(0, name.length) === name) {
var key = localStorage.key(i).substring(name.length);
var value = this.get(key);
if (value !== undefined) { values[key] = value; }
}
}
return values;
};
Store.prototype.fromObject = function (values, merge) {
if (!merge) { this.reset(); }
for (var key in values) {
if (has(values, key)) {
this.set(key, values[key]);
}
}
return this;
};
Store.prototype.addEvent = function (selector, callback) {
this.watcher(true);
if (!this.listeners[selector]) { this.listeners[selector] = []; }
arrayInclude(this.listeners[selector], callback);
return this;
};
Store.prototype.removeEvent = function (selector, callback) {
if (this.listeners[selector]) {
for (var i = (this.listeners[selector].length - 1); i >= 0; i--) {
if (this.listeners[selector][i] === callback) { this.listeners[selector].splice(i, 1); }
}
if (!this.listeners[selector].length) { delete this.listeners[selector]; }
}
return this;
};
Store.prototype.fireEvent = function (name, value) {
var selectors = [name, "*"];
for (var i = 0; i < selectors.length; i++) {
var selector = selectors[i];
if (this.listeners[selector]) {
for (var j = 0; j < this.listeners[selector].length; j++) {
this.listeners[selector][j](value, name, this.name);
}
}
}
return this;
};
}());