Provider.js 14 KB

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