2 Copyright (c) 2009, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.net/yui/license.txt
8 * Provides a swf based storage implementation
14 * Class for the YUI SWFStore util.
16 * @namespace YAHOO.util
18 * @uses YAHOO.util.AttributeProvider
20 * @param containerId {HTMLElement} Container element for the Flash Player instance.
21 * @param shareData {Boolean} Whether or not data should be shared across browsers
22 * @param useCompression {Boolean} Container element for the Flash Player instance.
24 YAHOO.util.SWFStore = function(containerID, shareData, useCompression)
29 //convert Booleans to strings for flashvars compatibility
30 shareData = shareData.toString();
31 useCompression = useCompression.toString();
33 if (YAHOO.env.ua.ie) browser = "ie";
34 else if (YAHOO.env.ua.gecko) browser = "gecko"; //Firefox
35 else if (YAHOO.env.ua.webkit) browser = "webkit"; // Safari, Webkit
36 else if (YAHOO.env.ua.caja) browser = "caja";
37 else if (YAHOO.env.ua.opera) browser = "opera";
38 else browser = "other";
40 if(YAHOO.util.Cookie.get("swfstore") == null || YAHOO.util.Cookie.get("swfstore") == "null" || YAHOO.util.Cookie.get("swfstore") == "")
43 newValue = Math.round(Math.random() * Math.PI * 100000);
44 YAHOO.util.Cookie.set("swfstore", newValue);
50 newValue = YAHOO.util.Cookie.get("swfstore");
57 useExpressInstall: false,
59 {allowScriptAccess:"always", allowNetworking:"all", scale:"noScale"},
61 {shareData: shareData, browser: newValue, useCompression: useCompression}
66 this.embeddedSWF = new YAHOO.widget.SWF(containerID, YAHOO.util.SWFStore.SWFURL, params);
70 * Fires when an error occurs
73 * @param event.type {String} The event type
74 * @param event.message {String} The data
77 this.createEvent("error");
80 * Fires when there is not enough space available to store the data
82 * @event quotaExceededError
83 * @param event.type {String} The event type
84 * @param event.message {String} The data
87 this.createEvent("quotaExceededError");
90 * Fires when the url matching for the security whitelist is invalid.
91 * If no whitelist is used, fires when page's url does not match the embedded swf's url
93 * @event securityError
94 * @param event.type {String} The event type
95 * @param event.message {String} The data
98 this.createEvent("securityError");
101 * Fires when a store is saved successfully
104 * @param event.type {String} The event type
107 this.createEvent("save");
110 * Fires when a store is successfully cleared
113 * @param event.type {String} The event type
116 this.createEvent("clear");
120 * Fires when the save is pending, due to a request for additional storage
123 * @param event.type {String} The event type
126 this.createEvent("pending");
130 * Fires as the settings dialog displays
132 * @event openingDialog
133 * @param event.type {String} The event type
136 this.createEvent("openingDialog");
139 * Fires when a settings dialog is not able to be displayed due to
140 * the SWF not being large enough to show it. In this case, the developer
141 * needs to resize the SWF to width of 215px and height of 138px or above,
142 * or display an external settings page.
144 * @event inadequateDimensions
145 * @param event.type {String} The event type
148 this.createEvent("inadequateDimensions");
151 YAHOO.extend(YAHOO.util.SWFStore, YAHOO.util.AttributeProvider,
157 * Method to attach listeners to events
158 * @param type {String} The tyep of event to listen for
159 * @param listener {String} The function to call
161 on: function(type, listener)
163 this.embeddedSWF.addListener(type, listener);
167 * Method to attach listeners to events
168 * @param type {String} The tyep of event to listen for
169 * @param listener {String} The function to call
171 addListener: function(type, listener)
173 this.embeddedSWF.addListener(type, listener);
177 * Public accessor to the unique name of the SWFStore instance.
180 * @return {String} Unique name of the SWFStore instance.
184 return "SWFStore " + this._id;
188 * Public accessor to the unique name of the SWFStore instance.
190 * @method getShareData
191 * @return {Boolean} Whether or not data is being shared among browsers
193 getShareData: function()
195 return this.embeddedSWF.callSWF("getShareData");
198 * Public accessor to the unique name of the SWFStore instance.
200 * @method setShareData
201 * @param {Boolean} Whether or not to share among browsers
203 setShareData: function(value)
205 this.embeddedSWF.callSWF("setShareData", [value]);
209 * Determines if SWF's visible area is large enough to fit the settings panel
211 * @method hasAdequateDimensions
212 * @return {Boolean} Whether or not to share among browsers
214 hasAdequateDimensions: function()
216 return this.embeddedSWF.callSWF("hasAdequateDimensions");
220 * Public accessor to the unique name of the SWFStore instance.
222 * @method getUseCompression
223 * @return {Boolean} Whether or compression is being used
225 getUseCompression: function()
227 return this.embeddedSWF.callSWF("getUseCompression");
231 * Public accessor to the unique name of the SWFStore instance.
233 * @method setUseCompression
234 * @param {Boolean} Whether or to compress stored data
236 setUseCompression: function(value)
238 this.embeddedSWF.callSWF("setUseCompression", [value]);
242 * Saves data to local storage. It returns a String that can
243 * be one of three values: "true" if the storage succeeded; "false" if the user
244 * has denied storage on their machine or storage space allotted is not sufficient.
245 * <p>The size limit for the passed parameters is ~40Kb.</p>
247 * @param data {Object} The data to store
248 * @param location {String} The name of the "cookie" or store
249 * @return {Boolean} Whether or not the save was successful
252 setItem: function(location,data)
254 return this.embeddedSWF.callSWF("setItem", [location, data]);
258 * Returns the value of the store at the specified index, if any.
260 * @param index {Number} The index of the stored item
261 * @return {Object} The value of the store at that index
264 getValueAt: function(index)
266 return this.embeddedSWF.callSWF("getValueAt", [index]);
270 * Returns the key name in storage, if any, at the specified index.
272 * @param index {Number} The index of the "cookie" or store
273 * @return {Object}The data
277 getNameAt: function(index)
279 return this.embeddedSWF.callSWF("getNameAt", [index]);
284 * Returns the value of the item in storage, if any.
286 * @param location {String} The name of the "cookie" or store
287 * @return {Object} The data
290 getValueOf: function(location)
292 return this.embeddedSWF.callSWF("getValueOf", [location]);
296 * Returns the data type of of the storage.
297 * <p>May be one of the following types:
309 * @param location {String} The name of the "cookie" or store
310 * @return {String} The type
313 getTypeOf: function(location)
315 return this.embeddedSWF.callSWF("getTypeOf", [location]);
319 * Returns the data type of of the storage.
320 * <p>May be one of the following types:
332 * @param location {Number} The index of the "cookie" or store
333 * @return {String} The type
336 getTypeAt: function(index)
338 return this.embeddedSWF.callSWF("getTypeAt", [index]);
342 * Returns the items in storage as an array.
344 * @return {Object} The data.
349 return this.embeddedSWF.callSWF("getItems", []);
353 * Removes the item in storage, if any.
355 * @param location {String} The name of the "cookie" or store
358 removeItem: function(location)
360 return this.embeddedSWF.callSWF("removeItem", [location]);
364 * Removes the item in storage at the specified index, if any.
366 * @param index {Number} The index of the "cookie" or store
369 removeItemAt: function(index)
371 return this.embeddedSWF.callSWF("removeItemAt", [index]);
375 * Returns the number of items in storage, if any.
377 * @return {Number} The number of items
380 getLength: function()
382 return this.embeddedSWF.callSWF("getLength", []);
386 * Removes all data in local storage for this domain.
387 * <p>Be careful when using this method, as it may
388 * remove stored information that is used by other applications
389 * in this domain </p>
394 return this.embeddedSWF.callSWF("clear", []);
398 * Gets the current size, in KB, of the amount of space taken by the current store.
399 * Note that this is calculated, and may take time depending on the number of items stored
400 * @method calculateCurrentSize
401 * @return {Number} The size of the store in KB
403 calculateCurrentSize: function()
405 return this.embeddedSWF.callSWF("calculateCurrentSize", []);
409 * Gets the timestamp of the last store. This value is automatically set when
411 * @method getModificationDate
412 * @return {Date} A Date object
414 getModificationDate: function()
416 return this.embeddedSWF.callSWF("getModificationDate", []);
420 * This method requests more storage (if the amount is above 100KB or the current setting).
422 * The request dialog has to be displayed within the Flash player itself
423 * so the SWF it is called from must be visible and at least 215px x 138px (w x h) in size.
426 * @param value {Number} The size, in KB
429 setSize: function(value)
431 var result = this.embeddedSWF.callSWF("setSize", [value]);
436 * Displays the settings dialog to allow the user to configure
437 * storage settings manually. If the SWF height and width are smaller than
438 * what is allowable to display the local settings panel,
439 * an openExternalDialog message will be sent to JavaScript.
440 * @method displaySettings
442 displaySettings: function()
444 this.embeddedSWF.callSWF("displaySettings", []);
450 YAHOO.util.SWFStore.SWFURL = "swfstore.swf";
452 YAHOO.register("swfstore", YAHOO.util.SWFStore, {version: "2.8.0r4", build: "2449"});
453 YAHOO.register("swfstore", YAHOO.util.SWFStore, {version: "2.8.0r4", build: "2449"});