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 YAHOO.log("adding '" + type + "' listener");
174 this.embeddedSWF.addListener(type, listener);
178 * Public accessor to the unique name of the SWFStore instance.
181 * @return {String} Unique name of the SWFStore instance.
185 return "SWFStore " + this._id;
189 * Public accessor to the unique name of the SWFStore instance.
191 * @method getShareData
192 * @return {Boolean} Whether or not data is being shared among browsers
194 getShareData: function()
196 return this.embeddedSWF.callSWF("getShareData");
199 * Public accessor to the unique name of the SWFStore instance.
201 * @method setShareData
202 * @param {Boolean} Whether or not to share among browsers
204 setShareData: function(value)
206 YAHOO.log("Setting share data to " + value);
207 this.embeddedSWF.callSWF("setShareData", [value]);
211 * Determines if SWF's visible area is large enough to fit the settings panel
213 * @method hasAdequateDimensions
214 * @return {Boolean} Whether or not to share among browsers
216 hasAdequateDimensions: function()
218 YAHOO.log("dimensions adequate? " + this.embeddedSWF.callSWF("hasAdequateDimensions"));
219 return this.embeddedSWF.callSWF("hasAdequateDimensions");
223 * Public accessor to the unique name of the SWFStore instance.
225 * @method getUseCompression
226 * @return {Boolean} Whether or compression is being used
228 getUseCompression: function()
230 return this.embeddedSWF.callSWF("getUseCompression");
234 * Public accessor to the unique name of the SWFStore instance.
236 * @method setUseCompression
237 * @param {Boolean} Whether or to compress stored data
239 setUseCompression: function(value)
241 YAHOO.log("Setting compression to " + value);
242 this.embeddedSWF.callSWF("setUseCompression", [value]);
246 * Saves data to local storage. It returns a String that can
247 * be one of three values: "true" if the storage succeeded; "false" if the user
248 * has denied storage on their machine or storage space allotted is not sufficient.
249 * <p>The size limit for the passed parameters is ~40Kb.</p>
251 * @param data {Object} The data to store
252 * @param location {String} The name of the "cookie" or store
253 * @return {Boolean} Whether or not the save was successful
256 setItem: function(location,data)
258 YAHOO.log("setting " + location + " to " + data);
259 return this.embeddedSWF.callSWF("setItem", [location, data]);
263 * Returns the value of the store at the specified index, if any.
265 * @param index {Number} The index of the stored item
266 * @return {Object} The value of the store at that index
269 getValueAt: function(index)
271 YAHOO.log("value at " + index + " is " + this.embeddedSWF.callSWF("getValueAt", [index]) );
272 return this.embeddedSWF.callSWF("getValueAt", [index]);
276 * Returns the key name in storage, if any, at the specified index.
278 * @param index {Number} The index of the "cookie" or store
279 * @return {Object}The data
283 getNameAt: function(index)
285 YAHOO.log("name at " + index + " is " + this.embeddedSWF.callSWF("getNameAt", [index]) );
286 return this.embeddedSWF.callSWF("getNameAt", [index]);
291 * Returns the value of the item in storage, if any.
293 * @param location {String} The name of the "cookie" or store
294 * @return {Object} The data
297 getValueOf: function(location)
299 YAHOO.log("value of " + location + " is " + this.embeddedSWF.callSWF("getValueOf", [location]) );
300 return this.embeddedSWF.callSWF("getValueOf", [location]);
304 * Returns the data type of of the storage.
305 * <p>May be one of the following types:
317 * @param location {String} The name of the "cookie" or store
318 * @return {String} The type
321 getTypeOf: function(location)
323 YAHOO.log("type of " + location + " is " + this.embeddedSWF.callSWF("getTypeOf", [location]) );
324 return this.embeddedSWF.callSWF("getTypeOf", [location]);
328 * Returns the data type of of the storage.
329 * <p>May be one of the following types:
341 * @param location {Number} The index of the "cookie" or store
342 * @return {String} The type
345 getTypeAt: function(index)
347 YAHOO.log("type at " + index + " is " + this.embeddedSWF.callSWF("getTypeAt", [index]) );
348 return this.embeddedSWF.callSWF("getTypeAt", [index]);
352 * Returns the items in storage as an array.
354 * @return {Object} The data.
359 return this.embeddedSWF.callSWF("getItems", []);
363 * Removes the item in storage, if any.
365 * @param location {String} The name of the "cookie" or store
368 removeItem: function(location)
370 YAHOO.log("removing " + location);
371 return this.embeddedSWF.callSWF("removeItem", [location]);
375 * Removes the item in storage at the specified index, if any.
377 * @param index {Number} The index of the "cookie" or store
380 removeItemAt: function(index)
382 YAHOO.log("removing item at " + index);
383 return this.embeddedSWF.callSWF("removeItemAt", [index]);
387 * Returns the number of items in storage, if any.
389 * @return {Number} The number of items
392 getLength: function()
394 return this.embeddedSWF.callSWF("getLength", []);
398 * Removes all data in local storage for this domain.
399 * <p>Be careful when using this method, as it may
400 * remove stored information that is used by other applications
401 * in this domain </p>
406 YAHOO.log("clearing all items");
407 return this.embeddedSWF.callSWF("clear", []);
411 * Gets the current size, in KB, of the amount of space taken by the current store.
412 * Note that this is calculated, and may take time depending on the number of items stored
413 * @method calculateCurrentSize
414 * @return {Number} The size of the store in KB
416 calculateCurrentSize: function()
418 YAHOO.log("calculating size");
419 return this.embeddedSWF.callSWF("calculateCurrentSize", []);
423 * Gets the timestamp of the last store. This value is automatically set when
425 * @method getModificationDate
426 * @return {Date} A Date object
428 getModificationDate: function()
430 YAHOO.log("getting date");
431 return this.embeddedSWF.callSWF("getModificationDate", []);
435 * This method requests more storage (if the amount is above 100KB or the current setting).
437 * The request dialog has to be displayed within the Flash player itself
438 * so the SWF it is called from must be visible and at least 215px x 138px (w x h) in size.
441 * @param value {Number} The size, in KB
444 setSize: function(value)
446 var result = this.embeddedSWF.callSWF("setSize", [value]);
447 YAHOO.log("attempt to set size to " + value*1024 + " bytes resulted in " + result);
452 * Displays the settings dialog to allow the user to configure
453 * storage settings manually. If the SWF height and width are smaller than
454 * what is allowable to display the local settings panel,
455 * an openExternalDialog message will be sent to JavaScript.
456 * @method displaySettings
458 displaySettings: function()
460 YAHOO.log("attempting to show settings. are dimensions adequate? " + this.embeddedSWF.callSWF("hasAdequateDimensions"));
461 this.embeddedSWF.callSWF("displaySettings", []);
467 YAHOO.util.SWFStore.SWFURL = "swfstore.swf";
469 YAHOO.register("swfstore", YAHOO.util.SWFStore, {version: "2.8.0r4", build: "2449"});
470 YAHOO.register("swfstore", YAHOO.util.SWFStore, {version: "2.8.0r4", build: "2449"});