source: Dev/branches/rest-dojo-ui/client/dojox/storage/manager.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: 8.4 KB
Line 
1dojo.provide("dojox.storage.manager");
2//dojo.require("dojo.AdapterRegistry");
3// FIXME: refactor this to use an AdapterRegistry
4
5dojox.storage.manager = new function(){
6        // summary: A singleton class in charge of the dojox.storage system
7        // description:
8        //              Initializes the storage systems and figures out the best available
9        //              storage options on this platform.
10
11        // currentProvider: Object
12        //      The storage provider that was automagically chosen to do storage
13        //      on this platform, such as dojox.storage.FlashStorageProvider.
14        this.currentProvider = null;
15
16        // available: Boolean
17        //      Whether storage of some kind is available.
18        this.available = false;
19
20  // providers: Array
21  //  Array of all the static provider instances, useful if you want to
22  //  loop through and see what providers have been registered.
23  this.providers = [];
24
25        this._initialized = false;
26
27        this._onLoadListeners = [];
28
29        this.initialize = function(){
30                // summary:
31                //              Initializes the storage system and autodetects the best storage
32                //              provider we can provide on this platform
33                this.autodetect();
34        };
35
36        this.register = function(/*string*/ name, /*Object*/ instance){
37                // summary:
38                //              Registers the existence of a new storage provider; used by
39                //              subclasses to inform the manager of their existence. The
40                //              storage manager will select storage providers based on
41                //              their ordering, so the order in which you call this method
42                //              matters.
43                // name:
44                //              The full class name of this provider, such as
45                //              "dojox.storage.FlashStorageProvider".
46                // instance:
47                //              An instance of this provider, which we will use to call
48                //              isAvailable() on.
49
50                // keep list of providers as a list so that we can know what order
51                // storage providers are preferred; also, store the providers hashed
52                // by name in case someone wants to get a provider that uses
53                // a particular storage backend
54                this.providers.push(instance);
55                this.providers[name] = instance;
56        };
57
58        this.setProvider = function(storageClass){
59                // summary:
60                //              Instructs the storageManager to use the given storage class for
61                //              all storage requests.
62                // description:
63                //              Example-
64                //                      dojox.storage.setProvider(
65                //                              dojox.storage.IEStorageProvider)
66
67        };
68
69        this.autodetect = function(){
70                // summary:
71                //              Autodetects the best possible persistent storage provider
72                //              available on this platform.
73
74                //console.debug("dojox.storage.manager.autodetect");
75
76                if(this._initialized){ // already finished
77                        return;
78                }
79
80                // a flag to force the storage manager to use a particular
81                // storage provider type, such as
82                // djConfig = {forceStorageProvider: "dojox.storage.WhatWGStorageProvider"};
83                var forceProvider = dojo.config["forceStorageProvider"] || false;
84
85                // go through each provider, seeing if it can be used
86                var providerToUse;
87                //FIXME: use dojo.some
88                for(var i = 0; i < this.providers.length; i++){
89                        providerToUse = this.providers[i];
90                        if(forceProvider && forceProvider == providerToUse.declaredClass){
91                                // still call isAvailable for this provider, since this helps some
92                                // providers internally figure out if they are available
93                                // FIXME: This should be refactored since it is non-intuitive
94                                // that isAvailable() would initialize some state
95                                providerToUse.isAvailable();
96                                break;
97                        }else if(!forceProvider && providerToUse.isAvailable()){
98                                break;
99                        }
100                }
101
102                if(!providerToUse){ // no provider available
103                        this._initialized = true;
104                        this.available = false;
105                        this.currentProvider = null;
106                        console.warn("No storage provider found for this platform");
107                        this.loaded();
108                        return;
109                }
110
111                // create this provider and mix in it's properties
112                // so that developers can do dojox.storage.put rather
113                // than dojox.storage.currentProvider.put, for example
114                this.currentProvider = providerToUse;
115                dojo.mixin(dojox.storage, this.currentProvider);
116
117                // have the provider initialize itself
118                dojox.storage.initialize();
119
120                this._initialized = true;
121                this.available = true;
122        };
123
124        this.isAvailable = function(){ /*Boolean*/
125                // summary: Returns whether any storage options are available.
126                return this.available;
127        };
128
129        this.addOnLoad = function(func){ /* void */
130                // summary:
131                //              Adds an onload listener to know when Dojo Offline can be used.
132                // description:
133                //              Adds a listener to know when Dojo Offline can be used. This
134                //              ensures that the Dojo Offline framework is loaded and that the
135                //              local dojox.storage system is ready to be used. This method is
136                //              useful if you don't want to have a dependency on Dojo Events
137                //              when using dojox.storage.
138                // func: Function
139                //              A function to call when Dojo Offline is ready to go
140                this._onLoadListeners.push(func);
141
142                if(this.isInitialized()){
143                        this._fireLoaded();
144                }
145        };
146
147        this.removeOnLoad = function(func){ /* void */
148                // summary: Removes the given onLoad listener
149                for(var i = 0; i < this._onLoadListeners.length; i++){
150                        if(func == this._onLoadListeners[i]){
151                                this._onLoadListeners.splice(i, 1);
152                                break;
153                        }
154                }
155        };
156
157        this.isInitialized = function(){ /*Boolean*/
158                // summary:
159                //              Returns whether the storage system is initialized and ready to
160                //              be used.
161
162                // FIXME: This should REALLY not be in here, but it fixes a tricky
163                // Flash timing bug.
164                // Confirm that this is still needed with the newly refactored Dojo
165                // Flash. Used to be for Internet Explorer. -- Brad Neuberg
166                if(this.currentProvider != null
167                        && this.currentProvider.declaredClass == "dojox.storage.FlashStorageProvider"
168                        && dojox.flash.ready == false){
169                        return false;
170                }else{
171                        return this._initialized;
172                }
173        };
174
175        this.supportsProvider = function(/*string*/ storageClass){ /* Boolean */
176                // summary: Determines if this platform supports the given storage provider.
177                // description:
178                //              Example-
179                //                      dojox.storage.manager.supportsProvider(
180                //                              "dojox.storage.InternetExplorerStorageProvider");
181
182                // construct this class dynamically
183                try{
184                        // dynamically call the given providers class level isAvailable()
185                        // method
186                        var provider = eval("new " + storageClass + "()");
187                        var results = provider.isAvailable();
188                        if(!results){ return false; }
189                        return results;
190                }catch(e){
191                        return false;
192                }
193        };
194
195        this.getProvider = function(){ /* Object */
196                // summary: Gets the current provider
197                return this.currentProvider;
198        };
199
200        this.loaded = function(){
201                // summary:
202                //              The storage provider should call this method when it is loaded
203                //              and ready to be used. Clients who will use the provider will
204                //              connect to this method to know when they can use the storage
205                //              system. You can either use dojo.connect to connect to this
206                //              function, or can use dojox.storage.manager.addOnLoad() to add
207                //              a listener that does not depend on the dojo.event package.
208                // description:
209                //              Example 1-
210                //                      if(dojox.storage.manager.isInitialized() == false){
211                //                              dojo.connect(dojox.storage.manager, "loaded", TestStorage, "initialize");
212                //                      }else{
213                //                              dojo.connect(dojo, "loaded", TestStorage, "initialize");
214                //                      }
215                //              Example 2-
216                //                      dojox.storage.manager.addOnLoad(someFunction);
217
218
219                // FIXME: we should just provide a Deferred for this. That way you
220                // don't care when this happens or has happened. Deferreds are in Base
221                this._fireLoaded();
222        };
223
224        this._fireLoaded = function(){
225                //console.debug("dojox.storage.manager._fireLoaded");
226
227                dojo.forEach(this._onLoadListeners, function(i){
228                        try{
229                                i();
230                        }catch(e){ console.debug(e); }
231                });
232        };
233
234        this.getResourceList = function(){
235                // summary:
236                //              Returns a list of whatever resources are necessary for storage
237                //              providers to work.
238                // description:
239                //              This will return all files needed by all storage providers for
240                //              this particular environment type. For example, if we are in the
241                //              browser environment, then this will return the hidden SWF files
242                //              needed by the FlashStorageProvider, even if we don't need them
243                //              for the particular browser we are working within. This is meant
244                //              to faciliate Dojo Offline, which must retrieve all resources we
245                //              need offline into the offline cache -- we retrieve everything
246                //              needed, in case another browser that requires different storage
247                //              mechanisms hits the local offline cache. For example, if we
248                //              were to sync against Dojo Offline on Firefox 2, then we would
249                //              not grab the FlashStorageProvider resources needed for Safari.
250                var results = [];
251                dojo.forEach(dojox.storage.manager.providers, function(currentProvider){
252                        results = results.concat(currentProvider.getResourceList());
253                });
254
255                return results;
256        }
257};
Note: See TracBrowser for help on using the repository browser.