-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStore.js
336 lines (274 loc) · 9.07 KB
/
Store.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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/**
* Implementaion of a generic store you can populate with data
*
* API Short reference:
* Store.create(String category, Array items|Object item);
* Store.update(String category, Array items|Object item);
* Store.get(String category, String id);
* Store.remove(String category, Array items|Object item);
* Store.clean(String category);
*/
(function (global) {
'use strict';
/**
* Object extend, own implementation
*
* @param {Object} object1 Base object
* @param {Object} object2 Object to merge in
* @return {Object} Extended object
*/
var extend = function (object1, object2) {
var i;
for (i in object2) {
if (object2.hasOwnProperty(i)) {
object1[i] = object2[i];
}
}
return object1;
};
// Store with methods
var _Store = {
/**
* Internal storage object to save data
* @type {Object}
*/
storage: {},
/**
* Generate unique IDs
* Taken from http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript
* and shortened UIDs
* @return {String} Unique id
*/
generateUuid: function () {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4();
},
/**
* Find an item based on a name
* @param {Array} category Category in store to find (first level)
* @param {String} id ID which you want to find
* @return {null|Object} Item which was found or null
*/
find: function (category, id) {
var item = null;
var i = 0;
if (!_Store.storage[category]) {
return null;
}
if (typeof category !== 'string') {
throw new Error('Store#find: Category ' + category + ' is not a string.');
}
if (typeof id !== 'string') {
throw new Error('Store#find: ID ' + id + ' is not a string.');
}
for (; _Store.storage[category].length > i; i++) {
if (_Store.storage[category][i].id === id) {
item = _Store.storage[category][i];
}
}
return item;
},
/**
* Create a new category (child) in store
* @param {String} category Name of category you want to create
* @return {Object|false} item, if could be created; false if category is alreay available
*/
createCategory: function (category) {
if (!_Store.storage[category]) {
_Store.storage[category] = [];
return true;
}
return false;
},
/**
* Create a new item in store, update exisiting if that's what we're looking for
* @param {String} category Category to look out for
* @param {Array} items Items that should be created
* @return {Object|false} Created object
*/
create: function (category, items) {
if (!_Store.storage[category]) {
throw new Error('Store: Category ' + category + ' does not exist.');
}
// Should be an array
if (items.constructor !== Array) {
items = [items];
}
items.forEach(function (item) {
if (!item.id) {
item.id = _Store.generateUuid();
}
if (_Store.find(category, item.id)) {
return _Store.update(category, item);
}
// Set an index on _Store item
item.index = _Store.storage[category].length;
_Store.storage[category].push(item);
});
return items;
},
/**
* Remove item from store
* @param {String} category Category to look out for
* @param {Array} ids Name of item that should be removed
* @return {Boolean} true, if removal was successful
*/
remove: function (category, ids) {
// Ensure that we use an array
if (ids.constructor !== Array) {
ids = [ids];
}
ids.forEach(function (id) {
var storedItem = _Store.find(category, id);
if (storedItem !== null) {
_Store.storage[category] = _Store.storage[category].filter(function (item) {
return item.id !== id;
});
}
});
return true;
},
/**
* Update existing items in store
* @param {String} category Category to look out for
* @param {Array|Object} items New items
* @return {Object|Boolean} True, if update was ok; false if item does not exist
*/
update: function (category, items) {
// Ensure that we use an array
if (items.constructor !== Array) {
items = [items];
}
// If category does not exist, throw
if (!_Store.storage[category]) {
throw new Error('Store: Category "' + category + '" does not exist.');
}
items.forEach(function (item) {
var storedItem = _Store.find(category, item.id);
if (storedItem !== null) {
_Store.storage[category][storedItem.index] = extend(storedItem, item);
return true;
}
return false;
});
},
/**
* Overwrite exisiting storage
* @param {String} category Category in store to find (first level)
* @return {Array|false} Category data, false if categroy not present
*/
restore: function (category, store) {
if (!_Store.storage[category]) {
return false;
}
_Store.storage[category] = store;
return _Store.storage[category];
}
};
/**
* API
* @return {Object}
*/
var Store = function (PubSub) {
var api = {
/**
* Get item from storage, proxy for private _Store.find
* @param {Array} category Category in store to find (first level)
* @param {String} id id which you want to find
* @return {null|Object} Item which was found or null
*/
get: function (category, id) {
return _Store.find(category, id);
},
/**
* Create new items
* @param {String} category Category to look out for
* @param {Array|Object} items Item that should be created
* @return {Object|false} Item or false
*/
create: function (category, items) {
PubSub.publish(category + '.create');
_Store.createCategory(category);
return _Store.create(category, items);
},
/**
* Update items in storage, proxy for private _Store.update
* @param {Array} category Category in store to find (first level)
* @param {Array|Object} items Item you want to find
* @return {Boolean} Whether or not update was successfull
*/
update: function (category, items) {
PubSub.publish(category + '.update');
return _Store.update(category, items);
},
/**
* Remove item from storage, proxy for private _Store.remove
* @param {Array} category Category in store to find (first level)
* @param {Array} ids Name which you want to find
* @return {Boolean} Whether or not removal was successfull
*/
remove: function (category, ids) {
PubSub.publish(category + '.remove');
return _Store.remove(category, ids);
},
/**
* Get full storage
* @return {Object} Set full storage
*/
getAll: function () {
return _Store.storage;
},
/**
* Get all elements from a category
* @param {Array} category Category in store to find (first level)
* @return {Array} All items in category
*/
getAllByCategory: function (category) {
return _Store.storage[category];
},
/**
* Clean up a category
* @param {Array} category Category in store to find (first level)
* @return {Array} Empty category
*/
clean: function (category) {
PubSub.publish(category + '.clean');
return _Store.restore(category, []);
},
/**
* Set new store data for category
* @param {Array} category Category in store to find (first level)
* @return {Array} Category data
*/
restore: function (category, store) {
PubSub.publish(category + '.restore');
return _Store.restore(category, store);
}
};
// Make internal methods public for testing purposes
if (process && process.env && process.env.__test) {
api._Store = _Store;
}
return api;
};
/*
* AMD, module loader, global registration
*/
// Expose loaders that implement the Node module pattern.
if (typeof module === 'object' && module && typeof module.exports === 'object') {
var PubSub = require('vanilla-pubsub');
module.exports = Store(PubSub);
// Register as an AMD module
} else if (typeof define === 'function' && define.amd) {
define('Store', ['vanilla-pubsub'], function (PubSub) {
return Store(PubSub);
});
// Export into global space
} else if (typeof global === 'object' && typeof global.document === 'object') {
global.Store = Store(window.PubSub);
}
}(this));