Debian Codegraph @

dojox/storage/LocalStorageProvider.js

raw symbols SCIP
1 define([
2 "dojo/_base/declare",
3 "dojox/storage/Provider",
4 "dojox/storage/manager",
5 "dojo/_base/array",
6 "dojo/_base/lang",
7 "dojo/json"
8 ], function(declare, Provider, storageManager, array, lang, JSON){
9 var LocalStorageProvider = declare("dojox.storage.LocalStorageProvider", [Provider], {
10 store: null,
11 initialize: function(){
12
13 this.store = localStorage;
14
15 this.initialized = true;
16 storageManager.loaded();
17 },
18
19 isAvailable: function(){ /*Boolean*/
20 return typeof localStorage != "undefined";
21 },
22
23 put: function( /*string*/ key,
24 /*object*/ value,
25 /*function*/ resultsHandler,
26 /*string?*/ namespace){
27
28 // TODO: Use the events as specified in http://dev.w3.org/html5/webstorage/#the-storage-event ?
29 // Currently, the storage event is not reliable around browsers.
30
31 this._assertIsValidKey(key);
32
33 namespace = namespace||this.DEFAULT_NAMESPACE;
34 this._assertIsValidNamespace(namespace);
35
36 var fullKey = this.getFullKey(key,namespace);
37
38 // prepending a prefix to a string value
39 // will result in that prefix not being
40 // usable as a value, so we better use
41 // toJson() always.
42 value = JSON.stringify(value);
43
44 try { // ua may raise an QUOTA_EXCEEDED_ERR exception
45 this.store.setItem(fullKey,value);
46
47 if(resultsHandler){
48 resultsHandler(this.SUCCESS, key, null, namespace);
49 }
50 } catch(e) {
51 if(resultsHandler){
52 resultsHandler(this.FAILED, key, e.toString(), namespace);
53 }
54 }
55 },
56
57 get: function(/*string*/ key, /*string?*/ namespace){ /*Object*/
58 this._assertIsValidKey(key);
59
60 namespace = namespace||this.DEFAULT_NAMESPACE;
61 this._assertIsValidNamespace(namespace);
62
63 // get our full key name, which is namespace + key
64 key = this.getFullKey(key, namespace);
65
66 return JSON.parse(this.store.getItem(key));
67 },
68
69 getKeys: function(/*string?*/ namespace){ /*Array*/
70 namespace = namespace||this.DEFAULT_NAMESPACE;
71 this._assertIsValidNamespace(namespace);
72
73 namespace = '__'+namespace+'_'
74
75 var keys = [];
76 for(var i = 0; i < this.store.length; i++){
77 var currentKey = this.store.key(i);
78 if(this._beginsWith(currentKey,namespace)){
79 currentKey = currentKey.substring(namespace.length);
80 keys.push(currentKey);
81 }
82 }
83
84 return keys;
85 },
86
87 clear: function(/*string?*/ namespace){
88 // Um, well, the 'specs' in Provider.js say that if
89 // no namespace is given, this method should nuke
90 // the *complete* storage. As other components might
91 // be using localStorage too, this might not be a
92 // good idea, so this method won't do it.
93
94 namespace = namespace||this.DEFAULT_NAMESPACE;
95 this._assertIsValidNamespace(namespace);
96
97 namespace = '__'+namespace+'_';
98
99 var keys = [];
100 for(var i = 0; i < this.store.length; i++){
101 if(this._beginsWith(this.store.key(i),namespace)){
102 keys.push(this.store.key(i));
103 }
104 }
105
106 array.forEach(keys, lang.hitch(this.store, "removeItem"));
107 },
108
109 remove: function(/*string*/ key, /*string?*/ namespace){
110 namespace = namespace||this.DEFAULT_NAMESPACE;
111 this._assertIsValidNamespace(namespace);
112
113 this.store.removeItem(this.getFullKey(key, namespace));
114 },
115
116 getNamespaces: function(){ /*string[]*/
117 // There must be a better way than
118 // to execute a regex on *every*
119 // item in the store.
120
121 var results = [ this.DEFAULT_NAMESPACE];
122
123 var found = {};
124 found[this.DEFAULT_NAMESPACE] = true;
125 var tester = /^__([^_]*)_/;
126
127 for(var i = 0; i < this.store.length; i++){
128 var currentKey = this.store.key(i);
129 if(tester.test(currentKey) == true){
130 var currentNS = currentKey.match(tester)[1];
131 if(typeof found[currentNS] == "undefined"){
132 found[currentNS] = true;
133 results.push(currentNS);
134 }
135 }
136 }
137
138 return results;
139 },
140
141 isPermanent: function(){ /*Boolean*/
142 return true;
143 },
144
145 getMaximumSize: function(){ /* mixed */
146 return dojox.storage.SIZE_NO_LIMIT;
147 },
148
149 hasSettingsUI: function(){ /*Boolean*/
150 return false;
151 },
152
153 isValidKey: function(/*string*/ keyName){ /*Boolean*/
154 if(keyName === null || keyName === undefined){
155 return false;
156 }
157
158 return /^[0-9A-Za-z_-]*$/.test(keyName);
159 },
160
161 isValidNamespace: function(/*string*/ keyName){ /*Boolean*/
162 // we *must* prevent namespaces from having
163 // underscores - else lookup of namespaces
164 // via RegEx (e.g. in getNamespaces ) would
165 // return wrong results.
166 //
167 // The only way around this would be to
168 // disallow underscores in keys.
169
170 if(keyName === null || keyName === undefined){
171 return false;
172 }
173
174 return /^[0-9A-Za-z-]*$/.test(keyName);
175 },
176
177 getFullKey: function(key, namespace){
178 // checks for valid namespace and
179 // key are already performed.
180 return "__" + namespace + "_" + key;
181 },
182
183 _beginsWith: function(/* string */ haystack, /* string */ needle) {
184 if(needle.length > haystack.length) {
185 return false;
186 }
187 return haystack.substring(0,needle.length) === needle;
188 },
189
190 _assertIsValidNamespace: function(/* string */ namespace){
191 if(this.isValidNamespace(namespace) === false){
192 throw new Error("Invalid namespace given: " + namespace);
193 }
194 },
195
196 _assertIsValidKey: function(/* string */ key){
197 if(this.isValidKey(key) === false){
198 throw new Error("Invalid key given: " + key);
199 }
200 }
201 });
202 storageManager.register("dojox.storage.LocalStorageProvider", new LocalStorageProvider());
203 return LocalStorageProvider;
204 });