forked from freightCrane/freightCrane
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jStorage.js
96 lines (86 loc) · 3.44 KB
/
jStorage.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
/*!
* jStorage
* https://github.com/flowertwig-org/jStorage
*
* Copyright 2013 Mattias Blomqvist and other contributors
* Released under the MIT license
* https://github.com/flowertwig-org/jStorage/blob/master/LICENSE
*
* Date: 21:09 2013-12-30
*/
// Creating a closure to avoid leaking variables into global scope,
// and using the variable undefined to get a X-browser compatible
// way of comparing with undefined, see this stackoverflow answer:
// http://stackoverflow.com/questions/135448/how-do-i-check-to-see-if-an-object-has-a-property-in-javascript#answer-135568
(function (window, undefined) {
// Upgrading to EcmaScript 5, and generating more helpful execptions and errors.
// Even though: http://bugs.jquery.com/ticket/13335, we've decided to go this path
// for now to write better code. We only test in modern browsers right now. If
// this bothers you(you use Firefox < 18 and your debug trace crashes it f.x.),
// poke us and we'll bake a version without it. For now, deal with it, since we
// don't have any legacy browsers to test with ;)
"use strict";
var jStorage = function (config) {
// The jStorage object is actually just the init constructor 'enhanced'
return new jStorage.fn.init(config);
};
var error = function (msg) {
// prepend with our libName to be nice, not everyone has nice debugging tools.
throw "jStorage: " + msg;
};
jStorage.fn = jStorage.prototype = {
init: function (config) {
this._provider = false;
// Do some inital sanity checking of our input.
if(config === undefined || !config) error("No config, please consult the readme ;)");
if(config.name === undefined) error("No name in config.");
if (jStorage.providers[config.name]) {
var provider = jStorage.providers[config.name];
this._provider = provider;
// Calling the callback now becomes the provider
// modules responsibility
provider.init(this, config);
} else {
error('Storage provider "' + config.name + '" was not loaded.');
}
},
get: function (name, callback) {
if (this._provider) {
this._provider.get(name, callback);
}
},
set: function(name, content, callback) {
if (this._provider) {
this._provider.set(name, content, callback);
}
},
move: function (currentName, newName, callback) {
if (this._provider) {
this._provider.move(currentName, newName, callback);
}
},
del: function (name, callback) {
if (this._provider) {
this._provider.del(name, callback);
}
},
list: function (name, callback) {
if (this._provider) {
this._provider.list(name, callback);
}
},
exists: function (name, callback) {
//console.log('exists');
if (this._provider) {
this._provider.exists(name, callback);
}
}
};
// Placeholder for our provider modules.
// Each module will register itself here.
jStorage.providers = {
};
// Give the init function the jStorage prototype for later instantiation
jStorage.fn.init.prototype = jStorage.fn;
window.jStorage = jStorage;
}(window));