source: Dev/branches/rest-dojo-ui/client/dojox/storage/Provider.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

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