1 | dojo.provide("dojox.storage.Provider"); |
---|
2 | |
---|
3 | dojo.declare("dojox.storage.Provider", null, { |
---|
4 | // summary: |
---|
5 | // A singleton for working with dojox.storage. |
---|
6 | // description: |
---|
7 | // dojox.storage exposes the current available storage provider on this |
---|
8 | // platform. It gives you methods such as dojox.storage.put(), |
---|
9 | // dojox.storage.get(), etc. |
---|
10 | // |
---|
11 | // For more details on dojox.storage, see the primary documentation |
---|
12 | // page at http://manual.dojotoolkit.org/storage.html. |
---|
13 | // |
---|
14 | // Note for storage provider developers who are creating subclasses- |
---|
15 | // This is the base class for all storage providers Specific kinds of |
---|
16 | // Storage Providers should subclass this and implement these methods. |
---|
17 | // You should avoid initialization in storage provider subclass's |
---|
18 | // constructor; instead, perform initialization in your initialize() |
---|
19 | // method. |
---|
20 | |
---|
21 | constructor: function(){ |
---|
22 | }, |
---|
23 | |
---|
24 | // SUCCESS: String |
---|
25 | // Flag that indicates a put() call to a |
---|
26 | // storage provider was successful. |
---|
27 | SUCCESS: "success", |
---|
28 | |
---|
29 | // FAILED: String |
---|
30 | // Flag that indicates a put() call to |
---|
31 | // a storage provider failed. |
---|
32 | FAILED: "failed", |
---|
33 | |
---|
34 | // PENDING: String |
---|
35 | // Flag that indicates a put() call to a |
---|
36 | // storage provider is pending user approval. |
---|
37 | PENDING: "pending", |
---|
38 | |
---|
39 | // SIZE_NOT_AVAILABLE: String |
---|
40 | // Returned by getMaximumSize() if this storage provider can not determine |
---|
41 | // the maximum amount of data it can support. |
---|
42 | SIZE_NOT_AVAILABLE: "Size not available", |
---|
43 | |
---|
44 | // SIZE_NO_LIMIT: String |
---|
45 | // Returned by getMaximumSize() if this storage provider has no theoretical |
---|
46 | // limit on the amount of data it can store. |
---|
47 | SIZE_NO_LIMIT: "No size limit", |
---|
48 | |
---|
49 | // DEFAULT_NAMESPACE: String |
---|
50 | // The namespace for all storage operations. This is useful if several |
---|
51 | // applications want access to the storage system from the same domain but |
---|
52 | // want different storage silos. |
---|
53 | DEFAULT_NAMESPACE: "default", |
---|
54 | |
---|
55 | // onHideSettingsUI: Function |
---|
56 | // If a function is assigned to this property, then when the settings |
---|
57 | // provider's UI is closed this function is called. Useful, for example, |
---|
58 | // if the user has just cleared out all storage for this provider using |
---|
59 | // the settings UI, and you want to update your UI. |
---|
60 | onHideSettingsUI: null, |
---|
61 | |
---|
62 | initialize: function(){ |
---|
63 | // summary: |
---|
64 | // Allows this storage provider to initialize itself. This is |
---|
65 | // called after the page has finished loading, so you can not do |
---|
66 | // document.writes(). Storage Provider subclasses should initialize |
---|
67 | // themselves inside of here rather than in their function |
---|
68 | // constructor. |
---|
69 | console.warn("dojox.storage.initialize not implemented"); |
---|
70 | }, |
---|
71 | |
---|
72 | isAvailable: function(){ /*Boolean*/ |
---|
73 | // summary: |
---|
74 | // Returns whether this storage provider is available on this |
---|
75 | // platform. |
---|
76 | console.warn("dojox.storage.isAvailable not implemented"); |
---|
77 | }, |
---|
78 | |
---|
79 | put: function( /*string*/ key, |
---|
80 | /*object*/ value, |
---|
81 | /*function*/ resultsHandler, |
---|
82 | /*string?*/ namespace){ |
---|
83 | // summary: |
---|
84 | // Puts a key and value into this storage system. |
---|
85 | // example: |
---|
86 | // | var resultsHandler = function(status, key, message, namespace){ |
---|
87 | // | alert("status="+status+", key="+key+", message="+message); |
---|
88 | // | }; |
---|
89 | // | dojox.storage.put("test", "hello world", resultsHandler); |
---|
90 | // description: |
---|
91 | // Important note: if you are using Dojo Storage in conjunction with |
---|
92 | // Dojo Offline, then you don't need to provide |
---|
93 | // a resultsHandler; this is because for Dojo Offline we |
---|
94 | // use Google Gears to persist data, which has unlimited data |
---|
95 | // once the user has given permission. If you are using Dojo |
---|
96 | // Storage apart from Dojo Offline, then under the covers hidden |
---|
97 | // Flash might be used, which is both asychronous and which might |
---|
98 | // get denied; in this case you must provide a resultsHandler. |
---|
99 | // key: |
---|
100 | // A string key to use when retrieving this value in the future. |
---|
101 | // value: |
---|
102 | // A value to store; this can be any JavaScript type. |
---|
103 | // resultsHandler: |
---|
104 | // A callback function that will receive three arguments. The |
---|
105 | // first argument is one of three values: dojox.storage.SUCCESS, |
---|
106 | // dojox.storage.FAILED, or dojox.storage.PENDING; these values |
---|
107 | // determine how the put request went. In some storage systems |
---|
108 | // users can deny a storage request, resulting in a |
---|
109 | // dojox.storage.FAILED, while in other storage systems a storage |
---|
110 | // request must wait for user approval, resulting in a |
---|
111 | // dojox.storage.PENDING status until the request is either |
---|
112 | // approved or denied, resulting in another call back with |
---|
113 | // dojox.storage.SUCCESS. |
---|
114 | // The second argument in the call back is the key name that was being stored. |
---|
115 | // The third argument in the call back is an optional message that |
---|
116 | // details possible error messages that might have occurred during |
---|
117 | // the storage process. |
---|
118 | // namespace: |
---|
119 | // Optional string namespace that this value will be placed into; |
---|
120 | // if left off, the value will be placed into dojox.storage.DEFAULT_NAMESPACE |
---|
121 | |
---|
122 | console.warn("dojox.storage.put not implemented"); |
---|
123 | }, |
---|
124 | |
---|
125 | get: function(/*string*/ key, /*string?*/ namespace){ /*Object*/ |
---|
126 | // summary: |
---|
127 | // Gets the value with the given key. Returns null if this key is |
---|
128 | // not in the storage system. |
---|
129 | // key: |
---|
130 | // A string key to get the value of. |
---|
131 | // namespace: |
---|
132 | // Optional string namespace that this value will be retrieved from; |
---|
133 | // if left off, the value will be retrieved from dojox.storage.DEFAULT_NAMESPACE |
---|
134 | // returns: |
---|
135 | // Any JavaScript object type; null if the key is not present |
---|
136 | console.warn("dojox.storage.get not implemented"); |
---|
137 | }, |
---|
138 | |
---|
139 | hasKey: function(/*string*/ key, /*string?*/ namespace){ |
---|
140 | // summary: |
---|
141 | // Determines whether the storage has the given key. |
---|
142 | return !!this.get(key, namespace); // Boolean |
---|
143 | }, |
---|
144 | |
---|
145 | getKeys: function(/*string?*/ namespace){ /*Array*/ |
---|
146 | // summary: |
---|
147 | // Enumerates all of the available keys in this storage system. |
---|
148 | // returns: |
---|
149 | // Array of available keys |
---|
150 | console.warn("dojox.storage.getKeys not implemented"); |
---|
151 | }, |
---|
152 | |
---|
153 | clear: function(/*string?*/ namespace){ |
---|
154 | // summary: |
---|
155 | // Completely clears this storage system of all of it's values and |
---|
156 | // keys. If 'namespace' is provided just clears the keys in that |
---|
157 | // namespace. |
---|
158 | console.warn("dojox.storage.clear not implemented"); |
---|
159 | }, |
---|
160 | |
---|
161 | remove: function(/*string*/ key, /*string?*/ namespace){ |
---|
162 | // summary: |
---|
163 | // Removes the given key from this storage system. |
---|
164 | console.warn("dojox.storage.remove not implemented"); |
---|
165 | }, |
---|
166 | |
---|
167 | getNamespaces: function(){ /*string[]*/ |
---|
168 | console.warn("dojox.storage.getNamespaces not implemented"); |
---|
169 | }, |
---|
170 | |
---|
171 | isPermanent: function(){ /*Boolean*/ |
---|
172 | // summary: |
---|
173 | // Returns whether this storage provider's values are persisted |
---|
174 | // when this platform is shutdown. |
---|
175 | console.warn("dojox.storage.isPermanent not implemented"); |
---|
176 | }, |
---|
177 | |
---|
178 | getMaximumSize: function(){ /* mixed */ |
---|
179 | // summary: |
---|
180 | // The maximum storage allowed by this provider |
---|
181 | // returns: |
---|
182 | // Returns the maximum storage size |
---|
183 | // supported by this provider, in |
---|
184 | // thousands of bytes (i.e., if it |
---|
185 | // returns 60 then this means that 60K |
---|
186 | // of storage is supported). |
---|
187 | // |
---|
188 | // If this provider can not determine |
---|
189 | // it's maximum size, then |
---|
190 | // dojox.storage.SIZE_NOT_AVAILABLE is |
---|
191 | // returned; if there is no theoretical |
---|
192 | // limit on the amount of storage |
---|
193 | // this provider can return, then |
---|
194 | // dojox.storage.SIZE_NO_LIMIT is |
---|
195 | // returned |
---|
196 | console.warn("dojox.storage.getMaximumSize not implemented"); |
---|
197 | }, |
---|
198 | |
---|
199 | putMultiple: function( /*array*/ keys, |
---|
200 | /*array*/ values, |
---|
201 | /*function*/ resultsHandler, |
---|
202 | /*string?*/ namespace){ |
---|
203 | // summary: |
---|
204 | // Puts multiple keys and values into this storage system. |
---|
205 | // example: |
---|
206 | // | var resultsHandler = function(status, key, message){ |
---|
207 | // | alert("status="+status+", key="+key+", message="+message); |
---|
208 | // | }; |
---|
209 | // | dojox.storage.put(["test"], ["hello world"], resultsHandler); |
---|
210 | // description: |
---|
211 | // Important note: if you are using Dojo Storage in conjunction with |
---|
212 | // Dojo Offline, then you don't need to provide |
---|
213 | // a resultsHandler; this is because for Dojo Offline we |
---|
214 | // use Google Gears to persist data, which has unlimited data |
---|
215 | // once the user has given permission. If you are using Dojo |
---|
216 | // Storage apart from Dojo Offline, then under the covers hidden |
---|
217 | // Flash might be used, which is both asychronous and which might |
---|
218 | // get denied; in this case you must provide a resultsHandler. |
---|
219 | // keys: |
---|
220 | // An array of string keys to use when retrieving this value in the future, |
---|
221 | // one per value to be stored |
---|
222 | // values: |
---|
223 | // An array of values to store; this can be any JavaScript type, though the |
---|
224 | // performance of plain strings is considerably better |
---|
225 | // resultsHandler: |
---|
226 | // A callback function that will receive three arguments. The |
---|
227 | // first argument is one of three values: dojox.storage.SUCCESS, |
---|
228 | // dojox.storage.FAILED, or dojox.storage.PENDING; these values |
---|
229 | // determine how the put request went. In some storage systems |
---|
230 | // users can deny a storage request, resulting in a |
---|
231 | // dojox.storage.FAILED, while in other storage systems a storage |
---|
232 | // request must wait for user approval, resulting in a |
---|
233 | // dojox.storage.PENDING status until the request is either |
---|
234 | // approved or denied, resulting in another call back with |
---|
235 | // dojox.storage.SUCCESS. |
---|
236 | // The second argument in the call back is the key name that was being stored. |
---|
237 | // The third argument in the call back is an optional message that |
---|
238 | // details possible error messages that might have occurred during |
---|
239 | // the storage process. |
---|
240 | // namespace: |
---|
241 | // Optional string namespace that this value will be placed into; |
---|
242 | // if left off, the value will be placed into dojox.storage.DEFAULT_NAMESPACE |
---|
243 | |
---|
244 | for(var i = 0; i < keys.length; i++){ |
---|
245 | dojox.storage.put(keys[i], values[i], resultsHandler, namespace); |
---|
246 | } |
---|
247 | }, |
---|
248 | |
---|
249 | getMultiple: function(/*array*/ keys, /*string?*/ namespace){ /*Object*/ |
---|
250 | // summary: |
---|
251 | // Gets the values corresponding to each of the given keys. |
---|
252 | // Returns a null array element for each given key that is |
---|
253 | // not in the storage system. |
---|
254 | // keys: |
---|
255 | // An array of string keys to get the value of. |
---|
256 | // namespace: |
---|
257 | // Optional string namespace that this value will be retrieved from; |
---|
258 | // if left off, the value will be retrieved from dojox.storage.DEFAULT_NAMESPACE |
---|
259 | // returns: |
---|
260 | // Any JavaScript object type; null if the key is not present |
---|
261 | |
---|
262 | var results = []; |
---|
263 | for(var i = 0; i < keys.length; i++){ |
---|
264 | results.push(dojox.storage.get(keys[i], namespace)); |
---|
265 | } |
---|
266 | |
---|
267 | return results; |
---|
268 | }, |
---|
269 | |
---|
270 | removeMultiple: function(/*array*/ keys, /*string?*/ namespace) { |
---|
271 | // summary: |
---|
272 | // Removes the given keys from this storage system. |
---|
273 | |
---|
274 | for(var i = 0; i < keys.length; i++){ |
---|
275 | dojox.storage.remove(keys[i], namespace); |
---|
276 | } |
---|
277 | }, |
---|
278 | |
---|
279 | isValidKeyArray: function( keys) { |
---|
280 | if(keys === null || keys === undefined || !dojo.isArray(keys)){ |
---|
281 | return false; |
---|
282 | } |
---|
283 | |
---|
284 | // JAC: This could be optimized by running the key validity test |
---|
285 | // directly over a joined string |
---|
286 | return !dojo.some(keys, function(key){ |
---|
287 | return !this.isValidKey(key); |
---|
288 | }, this); // Boolean |
---|
289 | }, |
---|
290 | |
---|
291 | hasSettingsUI: function(){ /*Boolean*/ |
---|
292 | // summary: |
---|
293 | // Determines whether this provider has a settings UI. |
---|
294 | return false; |
---|
295 | }, |
---|
296 | |
---|
297 | showSettingsUI: function(){ |
---|
298 | // summary: |
---|
299 | // If this provider has a settings UI, determined |
---|
300 | // by calling hasSettingsUI(), it is shown. |
---|
301 | console.warn("dojox.storage.showSettingsUI not implemented"); |
---|
302 | }, |
---|
303 | |
---|
304 | hideSettingsUI: function(){ |
---|
305 | // summary: |
---|
306 | // If this provider has a settings UI, hides it. |
---|
307 | console.warn("dojox.storage.hideSettingsUI not implemented"); |
---|
308 | }, |
---|
309 | |
---|
310 | isValidKey: function(/*string*/ keyName){ /*Boolean*/ |
---|
311 | // summary: |
---|
312 | // Subclasses can call this to ensure that the key given is valid |
---|
313 | // in a consistent way across different storage providers. We use |
---|
314 | // the lowest common denominator for key values allowed: only |
---|
315 | // letters, numbers, and underscores are allowed. No spaces. |
---|
316 | if(keyName === null || keyName === undefined){ |
---|
317 | return false; |
---|
318 | } |
---|
319 | |
---|
320 | return /^[0-9A-Za-z_]*$/.test(keyName); |
---|
321 | }, |
---|
322 | |
---|
323 | getResourceList: function(){ /* Array[] */ |
---|
324 | // summary: |
---|
325 | // Returns a list of URLs that this |
---|
326 | // storage provider might depend on. |
---|
327 | // description: |
---|
328 | // This method returns a list of URLs that this |
---|
329 | // storage provider depends on to do its work. |
---|
330 | // This list is used by the Dojo Offline Toolkit |
---|
331 | // to cache these resources to ensure the machinery |
---|
332 | // used by this storage provider is available offline. |
---|
333 | // What is returned is an array of URLs. |
---|
334 | // Note that Dojo Offline uses Gears as its native |
---|
335 | // storage provider, and does not support using other |
---|
336 | // kinds of storage providers while offline anymore. |
---|
337 | |
---|
338 | return []; |
---|
339 | } |
---|
340 | }); |
---|