Debian Codegraph @

dojox/storage/manager.js

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