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