swfstore.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. /*
  2. Copyright (c) 2009, Yahoo! Inc. All rights reserved.
  3. Code licensed under the BSD License:
  4. http://developer.yahoo.net/yui/license.txt
  5. version: 2.8.0r4
  6. */
  7. /**
  8. * Provides a swf based storage implementation
  9. *
  10. * @module swfstore
  11. */
  12. /**
  13. * Class for the YUI SWFStore util.
  14. *
  15. * @namespace YAHOO.util
  16. * @class SWFStore
  17. * @uses YAHOO.util.AttributeProvider
  18. * @constructor
  19. * @param containerId {HTMLElement} Container element for the Flash Player instance.
  20. * @param shareData {Boolean} Whether or not data should be shared across browsers
  21. * @param useCompression {Boolean} Container element for the Flash Player instance.
  22. */
  23. YAHOO.util.SWFStore = function(containerID, shareData, useCompression)
  24. {
  25. //browser detection
  26. var browser;
  27. var newValue;
  28. //convert Booleans to strings for flashvars compatibility
  29. shareData = shareData.toString();
  30. useCompression = useCompression.toString();
  31. if (YAHOO.env.ua.ie) browser = "ie";
  32. else if (YAHOO.env.ua.gecko) browser = "gecko"; //Firefox
  33. else if (YAHOO.env.ua.webkit) browser = "webkit"; // Safari, Webkit
  34. else if (YAHOO.env.ua.caja) browser = "caja";
  35. else if (YAHOO.env.ua.opera) browser = "opera";
  36. else browser = "other";
  37. if(YAHOO.util.Cookie.get("swfstore") == null || YAHOO.util.Cookie.get("swfstore") == "null" || YAHOO.util.Cookie.get("swfstore") == "")
  38. {
  39. newValue = Math.round(Math.random() * Math.PI * 100000);
  40. YAHOO.util.Cookie.set("swfstore", newValue);
  41. }
  42. else
  43. {
  44. newValue = YAHOO.util.Cookie.get("swfstore");
  45. }
  46. var params =
  47. {
  48. version: 9.115,
  49. useExpressInstall: false,
  50. fixedAttributes:
  51. {allowScriptAccess:"always", allowNetworking:"all", scale:"noScale"},
  52. flashVars:
  53. {shareData: shareData, browser: newValue, useCompression: useCompression}
  54. };
  55. this.embeddedSWF = new YAHOO.widget.SWF(containerID, YAHOO.util.SWFStore.SWFURL, params);
  56. /**
  57. * Fires when an error occurs
  58. *
  59. * @event error
  60. * @param event.type {String} The event type
  61. * @param event.message {String} The data
  62. *
  63. */
  64. this.createEvent("error");
  65. /**
  66. * Fires when there is not enough space available to store the data
  67. *
  68. * @event quotaExceededError
  69. * @param event.type {String} The event type
  70. * @param event.message {String} The data
  71. *
  72. */
  73. this.createEvent("quotaExceededError");
  74. /**
  75. * Fires when the url matching for the security whitelist is invalid.
  76. * If no whitelist is used, fires when page's url does not match the embedded swf's url
  77. *
  78. * @event securityError
  79. * @param event.type {String} The event type
  80. * @param event.message {String} The data
  81. *
  82. */
  83. this.createEvent("securityError");
  84. /**
  85. * Fires when a store is saved successfully
  86. *
  87. * @event save
  88. * @param event.type {String} The event type
  89. *
  90. */
  91. this.createEvent("save");
  92. /**
  93. * Fires when a store is successfully cleared
  94. *
  95. * @event clear
  96. * @param event.type {String} The event type
  97. *
  98. */
  99. this.createEvent("clear");
  100. /**
  101. * Fires when the save is pending, due to a request for additional storage
  102. *
  103. * @event error
  104. * @param event.type {String} The event type
  105. *
  106. */
  107. this.createEvent("pending");
  108. /**
  109. * Fires as the settings dialog displays
  110. *
  111. * @event openingDialog
  112. * @param event.type {String} The event type
  113. *
  114. */
  115. this.createEvent("openingDialog");
  116. /**
  117. * Fires when a settings dialog is not able to be displayed due to
  118. * the SWF not being large enough to show it. In this case, the developer
  119. * needs to resize the SWF to width of 215px and height of 138px or above,
  120. * or display an external settings page.
  121. *
  122. * @event inadequateDimensions
  123. * @param event.type {String} The event type
  124. *
  125. */
  126. this.createEvent("inadequateDimensions");
  127. };
  128. YAHOO.extend(YAHOO.util.SWFStore, YAHOO.util.AttributeProvider,
  129. {
  130. /**
  131. * Method to attach listeners to events
  132. * @param type {String} The tyep of event to listen for
  133. * @param listener {String} The function to call
  134. */
  135. on: function(type, listener)
  136. {
  137. this.embeddedSWF.addListener(type, listener);
  138. },
  139. /**
  140. * Method to attach listeners to events
  141. * @param type {String} The tyep of event to listen for
  142. * @param listener {String} The function to call
  143. */
  144. addListener: function(type, listener)
  145. {
  146. this.embeddedSWF.addListener(type, listener);
  147. },
  148. /**
  149. * Public accessor to the unique name of the SWFStore instance.
  150. *
  151. * @method toString
  152. * @return {String} Unique name of the SWFStore instance.
  153. */
  154. toString: function()
  155. {
  156. return "SWFStore " + this._id;
  157. },
  158. /**
  159. * Public accessor to the unique name of the SWFStore instance.
  160. *
  161. * @method getShareData
  162. * @return {Boolean} Whether or not data is being shared among browsers
  163. */
  164. getShareData: function()
  165. {
  166. return this.embeddedSWF.callSWF("getShareData");
  167. },
  168. /**
  169. * Public accessor to the unique name of the SWFStore instance.
  170. *
  171. * @method setShareData
  172. * @param {Boolean} Whether or not to share among browsers
  173. */
  174. setShareData: function(value)
  175. {
  176. this.embeddedSWF.callSWF("setShareData", [value]);
  177. },
  178. /**
  179. * Determines if SWF's visible area is large enough to fit the settings panel
  180. *
  181. * @method hasAdequateDimensions
  182. * @return {Boolean} Whether or not to share among browsers
  183. */
  184. hasAdequateDimensions: function()
  185. {
  186. return this.embeddedSWF.callSWF("hasAdequateDimensions");
  187. },
  188. /**
  189. * Public accessor to the unique name of the SWFStore instance.
  190. *
  191. * @method getUseCompression
  192. * @return {Boolean} Whether or compression is being used
  193. */
  194. getUseCompression: function()
  195. {
  196. return this.embeddedSWF.callSWF("getUseCompression");
  197. },
  198. /**
  199. * Public accessor to the unique name of the SWFStore instance.
  200. *
  201. * @method setUseCompression
  202. * @param {Boolean} Whether or to compress stored data
  203. */
  204. setUseCompression: function(value)
  205. {
  206. this.embeddedSWF.callSWF("setUseCompression", [value]);
  207. },
  208. /**
  209. * Saves data to local storage. It returns a String that can
  210. * be one of three values: "true" if the storage succeeded; "false" if the user
  211. * has denied storage on their machine or storage space allotted is not sufficient.
  212. * <p>The size limit for the passed parameters is ~40Kb.</p>
  213. * @method setItem
  214. * @param data {Object} The data to store
  215. * @param location {String} The name of the "cookie" or store
  216. * @return {Boolean} Whether or not the save was successful
  217. *
  218. */
  219. setItem: function(location,data)
  220. {
  221. return this.embeddedSWF.callSWF("setItem", [location, data]);
  222. } ,
  223. /**
  224. * Returns the value of the store at the specified index, if any.
  225. * @method getValueAt
  226. * @param index {Number} The index of the stored item
  227. * @return {Object} The value of the store at that index
  228. *
  229. */
  230. getValueAt: function(index)
  231. {
  232. return this.embeddedSWF.callSWF("getValueAt", [index]);
  233. },
  234. /**
  235. * Returns the key name in storage, if any, at the specified index.
  236. *
  237. * @param index {Number} The index of the "cookie" or store
  238. * @return {Object}The data
  239. * @method setItem
  240. *
  241. */
  242. getNameAt: function(index)
  243. {
  244. return this.embeddedSWF.callSWF("getNameAt", [index]);
  245. },
  246. /**
  247. * Returns the value of the item in storage, if any.
  248. * @method getValueOf
  249. * @param location {String} The name of the "cookie" or store
  250. * @return {Object} The data
  251. *
  252. */
  253. getValueOf: function(location)
  254. {
  255. return this.embeddedSWF.callSWF("getValueOf", [location]);
  256. } ,
  257. /**
  258. * Returns the data type of of the storage.
  259. * <p>May be one of the following types:
  260. * <ul>
  261. * <li>boolean</li>
  262. * <li>function</li>
  263. * <li>number</li>
  264. * <li>object</li>
  265. * <li>string</li>
  266. * <li>number</li>
  267. * <li>xml</li>
  268. * </ul>
  269. * </p>
  270. * @method getTypeOf
  271. * @param location {String} The name of the "cookie" or store
  272. * @return {String} The type
  273. *
  274. */
  275. getTypeOf: function(location)
  276. {
  277. return this.embeddedSWF.callSWF("getTypeOf", [location]);
  278. } ,
  279. /**
  280. * Returns the data type of of the storage.
  281. * <p>May be one of the following types:
  282. * <ul>
  283. * <li>boolean</li>
  284. * <li>function</li>
  285. * <li>number</li>
  286. * <li>object</li>
  287. * <li>string</li>
  288. * <li>number</li>
  289. * <li>xml</li>
  290. * </ul>
  291. * </p>
  292. * @method getTypeAt
  293. * @param location {Number} The index of the "cookie" or store
  294. * @return {String} The type
  295. *
  296. */
  297. getTypeAt: function(index)
  298. {
  299. return this.embeddedSWF.callSWF("getTypeAt", [index]);
  300. } ,
  301. /**
  302. * Returns the items in storage as an array.
  303. * @method getItems
  304. * @return {Object} The data.
  305. * @public
  306. */
  307. getItems: function()
  308. {
  309. return this.embeddedSWF.callSWF("getItems", []);
  310. },
  311. /**
  312. * Removes the item in storage, if any.
  313. * @method removeItem
  314. * @param location {String} The name of the "cookie" or store
  315. *
  316. */
  317. removeItem: function(location)
  318. {
  319. return this.embeddedSWF.callSWF("removeItem", [location]);
  320. } ,
  321. /**
  322. * Removes the item in storage at the specified index, if any.
  323. * @method removeItem
  324. * @param index {Number} The index of the "cookie" or store
  325. *
  326. */
  327. removeItemAt: function(index)
  328. {
  329. return this.embeddedSWF.callSWF("removeItemAt", [index]);
  330. } ,
  331. /**
  332. * Returns the number of items in storage, if any.
  333. * @method getLength
  334. * @return {Number} The number of items
  335. *
  336. */
  337. getLength: function()
  338. {
  339. return this.embeddedSWF.callSWF("getLength", []);
  340. } ,
  341. /**
  342. * Removes all data in local storage for this domain.
  343. * <p>Be careful when using this method, as it may
  344. * remove stored information that is used by other applications
  345. * in this domain </p>
  346. * @method clear
  347. */
  348. clear: function()
  349. {
  350. return this.embeddedSWF.callSWF("clear", []);
  351. } ,
  352. /**
  353. * Gets the current size, in KB, of the amount of space taken by the current store.
  354. * Note that this is calculated, and may take time depending on the number of items stored
  355. * @method calculateCurrentSize
  356. * @return {Number} The size of the store in KB
  357. */
  358. calculateCurrentSize: function()
  359. {
  360. return this.embeddedSWF.callSWF("calculateCurrentSize", []);
  361. } ,
  362. /**
  363. * Gets the timestamp of the last store. This value is automatically set when
  364. * data is stored.
  365. * @method getModificationDate
  366. * @return {Date} A Date object
  367. */
  368. getModificationDate: function()
  369. {
  370. return this.embeddedSWF.callSWF("getModificationDate", []);
  371. } ,
  372. /**
  373. * This method requests more storage (if the amount is above 100KB or the current setting).
  374. *
  375. * The request dialog has to be displayed within the Flash player itself
  376. * so the SWF it is called from must be visible and at least 215px x 138px (w x h) in size.
  377. *
  378. * @method setSize
  379. * @param value {Number} The size, in KB
  380. * @return {String}
  381. */
  382. setSize: function(value)
  383. {
  384. var result = this.embeddedSWF.callSWF("setSize", [value]);
  385. return result;
  386. } ,
  387. /**
  388. * Displays the settings dialog to allow the user to configure
  389. * storage settings manually. If the SWF height and width are smaller than
  390. * what is allowable to display the local settings panel,
  391. * an openExternalDialog message will be sent to JavaScript.
  392. * @method displaySettings
  393. */
  394. displaySettings: function()
  395. {
  396. this.embeddedSWF.callSWF("displaySettings", []);
  397. }
  398. });
  399. YAHOO.util.SWFStore.SWFURL = "swfstore.swf";
  400. YAHOO.register("swfstore", YAHOO.util.SWFStore, {version: "2.8.0r4", build: "2449"});
  401. YAHOO.register("swfstore", YAHOO.util.SWFStore, {version: "2.8.0r4", build: "2449"});