0
|
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 /**
|
|
14 * Class for the YUI SWFStore util.
|
|
15 *
|
|
16 * @namespace YAHOO.util
|
|
17 * @class SWFStore
|
|
18 * @uses YAHOO.util.AttributeProvider
|
|
19 * @constructor
|
|
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.
|
|
23 */
|
|
24 YAHOO.util.SWFStore = function(containerID, shareData, useCompression)
|
|
25 {
|
|
26 //browser detection
|
|
27 var browser;
|
|
28 var newValue;
|
|
29 //convert Booleans to strings for flashvars compatibility
|
|
30 shareData = shareData.toString();
|
|
31 useCompression = useCompression.toString();
|
|
32
|
|
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";
|
|
39
|
|
40 if(YAHOO.util.Cookie.get("swfstore") == null || YAHOO.util.Cookie.get("swfstore") == "null" || YAHOO.util.Cookie.get("swfstore") == "")
|
|
41 {
|
|
42
|
|
43 newValue = Math.round(Math.random() * Math.PI * 100000);
|
|
44 YAHOO.util.Cookie.set("swfstore", newValue);
|
|
45
|
|
46 }
|
|
47
|
|
48 else
|
|
49 {
|
|
50 newValue = YAHOO.util.Cookie.get("swfstore");
|
|
51 }
|
|
52
|
|
53
|
|
54 var params =
|
|
55 {
|
|
56 version: 9.115,
|
|
57 useExpressInstall: false,
|
|
58 fixedAttributes:
|
|
59 {allowScriptAccess:"always", allowNetworking:"all", scale:"noScale"},
|
|
60 flashVars:
|
|
61 {shareData: shareData, browser: newValue, useCompression: useCompression}
|
|
62 };
|
|
63
|
|
64
|
|
65
|
|
66 this.embeddedSWF = new YAHOO.widget.SWF(containerID, YAHOO.util.SWFStore.SWFURL, params);
|
|
67
|
|
68
|
|
69 /**
|
|
70 * Fires when an error occurs
|
|
71 *
|
|
72 * @event error
|
|
73 * @param event.type {String} The event type
|
|
74 * @param event.message {String} The data
|
|
75 *
|
|
76 */
|
|
77 this.createEvent("error");
|
|
78
|
|
79 /**
|
|
80 * Fires when there is not enough space available to store the data
|
|
81 *
|
|
82 * @event quotaExceededError
|
|
83 * @param event.type {String} The event type
|
|
84 * @param event.message {String} The data
|
|
85 *
|
|
86 */
|
|
87 this.createEvent("quotaExceededError");
|
|
88
|
|
89 /**
|
|
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
|
|
92 *
|
|
93 * @event securityError
|
|
94 * @param event.type {String} The event type
|
|
95 * @param event.message {String} The data
|
|
96 *
|
|
97 */
|
|
98 this.createEvent("securityError");
|
|
99
|
|
100 /**
|
|
101 * Fires when a store is saved successfully
|
|
102 *
|
|
103 * @event save
|
|
104 * @param event.type {String} The event type
|
|
105 *
|
|
106 */
|
|
107 this.createEvent("save");
|
|
108
|
|
109 /**
|
|
110 * Fires when a store is successfully cleared
|
|
111 *
|
|
112 * @event clear
|
|
113 * @param event.type {String} The event type
|
|
114 *
|
|
115 */
|
|
116 this.createEvent("clear");
|
|
117
|
|
118
|
|
119 /**
|
|
120 * Fires when the save is pending, due to a request for additional storage
|
|
121 *
|
|
122 * @event error
|
|
123 * @param event.type {String} The event type
|
|
124 *
|
|
125 */
|
|
126 this.createEvent("pending");
|
|
127
|
|
128
|
|
129 /**
|
|
130 * Fires as the settings dialog displays
|
|
131 *
|
|
132 * @event openingDialog
|
|
133 * @param event.type {String} The event type
|
|
134 *
|
|
135 */
|
|
136 this.createEvent("openingDialog");
|
|
137
|
|
138 /**
|
|
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.
|
|
143 *
|
|
144 * @event inadequateDimensions
|
|
145 * @param event.type {String} The event type
|
|
146 *
|
|
147 */
|
|
148 this.createEvent("inadequateDimensions");
|
|
149 };
|
|
150
|
|
151 YAHOO.extend(YAHOO.util.SWFStore, YAHOO.util.AttributeProvider,
|
|
152 {
|
|
153
|
|
154
|
|
155
|
|
156 /**
|
|
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
|
|
160 */
|
|
161 on: function(type, listener)
|
|
162 {
|
|
163 this.embeddedSWF.addListener(type, listener);
|
|
164 },
|
|
165
|
|
166 /**
|
|
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
|
|
170 */
|
|
171 addListener: function(type, listener)
|
|
172 {
|
|
173 this.embeddedSWF.addListener(type, listener);
|
|
174 },
|
|
175
|
|
176 /**
|
|
177 * Public accessor to the unique name of the SWFStore instance.
|
|
178 *
|
|
179 * @method toString
|
|
180 * @return {String} Unique name of the SWFStore instance.
|
|
181 */
|
|
182 toString: function()
|
|
183 {
|
|
184 return "SWFStore " + this._id;
|
|
185 },
|
|
186
|
|
187 /**
|
|
188 * Public accessor to the unique name of the SWFStore instance.
|
|
189 *
|
|
190 * @method getShareData
|
|
191 * @return {Boolean} Whether or not data is being shared among browsers
|
|
192 */
|
|
193 getShareData: function()
|
|
194 {
|
|
195 return this.embeddedSWF.callSWF("getShareData");
|
|
196 },
|
|
197 /**
|
|
198 * Public accessor to the unique name of the SWFStore instance.
|
|
199 *
|
|
200 * @method setShareData
|
|
201 * @param {Boolean} Whether or not to share among browsers
|
|
202 */
|
|
203 setShareData: function(value)
|
|
204 {
|
|
205 this.embeddedSWF.callSWF("setShareData", [value]);
|
|
206 },
|
|
207
|
|
208 /**
|
|
209 * Determines if SWF's visible area is large enough to fit the settings panel
|
|
210 *
|
|
211 * @method hasAdequateDimensions
|
|
212 * @return {Boolean} Whether or not to share among browsers
|
|
213 */
|
|
214 hasAdequateDimensions: function()
|
|
215 {
|
|
216 return this.embeddedSWF.callSWF("hasAdequateDimensions");
|
|
217 },
|
|
218
|
|
219 /**
|
|
220 * Public accessor to the unique name of the SWFStore instance.
|
|
221 *
|
|
222 * @method getUseCompression
|
|
223 * @return {Boolean} Whether or compression is being used
|
|
224 */
|
|
225 getUseCompression: function()
|
|
226 {
|
|
227 return this.embeddedSWF.callSWF("getUseCompression");
|
|
228 },
|
|
229
|
|
230 /**
|
|
231 * Public accessor to the unique name of the SWFStore instance.
|
|
232 *
|
|
233 * @method setUseCompression
|
|
234 * @param {Boolean} Whether or to compress stored data
|
|
235 */
|
|
236 setUseCompression: function(value)
|
|
237 {
|
|
238 this.embeddedSWF.callSWF("setUseCompression", [value]);
|
|
239 },
|
|
240
|
|
241 /**
|
|
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>
|
|
246 * @method setItem
|
|
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
|
|
250 *
|
|
251 */
|
|
252 setItem: function(location,data)
|
|
253 {
|
|
254 return this.embeddedSWF.callSWF("setItem", [location, data]);
|
|
255 } ,
|
|
256
|
|
257 /**
|
|
258 * Returns the value of the store at the specified index, if any.
|
|
259 * @method getValueAt
|
|
260 * @param index {Number} The index of the stored item
|
|
261 * @return {Object} The value of the store at that index
|
|
262 *
|
|
263 */
|
|
264 getValueAt: function(index)
|
|
265 {
|
|
266 return this.embeddedSWF.callSWF("getValueAt", [index]);
|
|
267 },
|
|
268
|
|
269 /**
|
|
270 * Returns the key name in storage, if any, at the specified index.
|
|
271 *
|
|
272 * @param index {Number} The index of the "cookie" or store
|
|
273 * @return {Object}The data
|
|
274 * @method setItem
|
|
275 *
|
|
276 */
|
|
277 getNameAt: function(index)
|
|
278 {
|
|
279 return this.embeddedSWF.callSWF("getNameAt", [index]);
|
|
280 },
|
|
281
|
|
282
|
|
283 /**
|
|
284 * Returns the value of the item in storage, if any.
|
|
285 * @method getValueOf
|
|
286 * @param location {String} The name of the "cookie" or store
|
|
287 * @return {Object} The data
|
|
288 *
|
|
289 */
|
|
290 getValueOf: function(location)
|
|
291 {
|
|
292 return this.embeddedSWF.callSWF("getValueOf", [location]);
|
|
293 } ,
|
|
294
|
|
295 /**
|
|
296 * Returns the data type of of the storage.
|
|
297 * <p>May be one of the following types:
|
|
298 * <ul>
|
|
299 * <li>boolean</li>
|
|
300 * <li>function</li>
|
|
301 * <li>number</li>
|
|
302 * <li>object</li>
|
|
303 * <li>string</li>
|
|
304 * <li>number</li>
|
|
305 * <li>xml</li>
|
|
306 * </ul>
|
|
307 * </p>
|
|
308 * @method getTypeOf
|
|
309 * @param location {String} The name of the "cookie" or store
|
|
310 * @return {String} The type
|
|
311 *
|
|
312 */
|
|
313 getTypeOf: function(location)
|
|
314 {
|
|
315 return this.embeddedSWF.callSWF("getTypeOf", [location]);
|
|
316 } ,
|
|
317
|
|
318 /**
|
|
319 * Returns the data type of of the storage.
|
|
320 * <p>May be one of the following types:
|
|
321 * <ul>
|
|
322 * <li>boolean</li>
|
|
323 * <li>function</li>
|
|
324 * <li>number</li>
|
|
325 * <li>object</li>
|
|
326 * <li>string</li>
|
|
327 * <li>number</li>
|
|
328 * <li>xml</li>
|
|
329 * </ul>
|
|
330 * </p>
|
|
331 * @method getTypeAt
|
|
332 * @param location {Number} The index of the "cookie" or store
|
|
333 * @return {String} The type
|
|
334 *
|
|
335 */
|
|
336 getTypeAt: function(index)
|
|
337 {
|
|
338 return this.embeddedSWF.callSWF("getTypeAt", [index]);
|
|
339 } ,
|
|
340
|
|
341 /**
|
|
342 * Returns the items in storage as an array.
|
|
343 * @method getItems
|
|
344 * @return {Object} The data.
|
|
345 * @public
|
|
346 */
|
|
347 getItems: function()
|
|
348 {
|
|
349 return this.embeddedSWF.callSWF("getItems", []);
|
|
350 },
|
|
351
|
|
352 /**
|
|
353 * Removes the item in storage, if any.
|
|
354 * @method removeItem
|
|
355 * @param location {String} The name of the "cookie" or store
|
|
356 *
|
|
357 */
|
|
358 removeItem: function(location)
|
|
359 {
|
|
360 return this.embeddedSWF.callSWF("removeItem", [location]);
|
|
361 } ,
|
|
362
|
|
363 /**
|
|
364 * Removes the item in storage at the specified index, if any.
|
|
365 * @method removeItem
|
|
366 * @param index {Number} The index of the "cookie" or store
|
|
367 *
|
|
368 */
|
|
369 removeItemAt: function(index)
|
|
370 {
|
|
371 return this.embeddedSWF.callSWF("removeItemAt", [index]);
|
|
372 } ,
|
|
373
|
|
374 /**
|
|
375 * Returns the number of items in storage, if any.
|
|
376 * @method getLength
|
|
377 * @return {Number} The number of items
|
|
378 *
|
|
379 */
|
|
380 getLength: function()
|
|
381 {
|
|
382 return this.embeddedSWF.callSWF("getLength", []);
|
|
383 } ,
|
|
384
|
|
385 /**
|
|
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>
|
|
390 * @method clear
|
|
391 */
|
|
392 clear: function()
|
|
393 {
|
|
394 return this.embeddedSWF.callSWF("clear", []);
|
|
395 } ,
|
|
396
|
|
397 /**
|
|
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
|
|
402 */
|
|
403 calculateCurrentSize: function()
|
|
404 {
|
|
405 return this.embeddedSWF.callSWF("calculateCurrentSize", []);
|
|
406 } ,
|
|
407
|
|
408 /**
|
|
409 * Gets the timestamp of the last store. This value is automatically set when
|
|
410 * data is stored.
|
|
411 * @method getModificationDate
|
|
412 * @return {Date} A Date object
|
|
413 */
|
|
414 getModificationDate: function()
|
|
415 {
|
|
416 return this.embeddedSWF.callSWF("getModificationDate", []);
|
|
417 } ,
|
|
418
|
|
419 /**
|
|
420 * This method requests more storage (if the amount is above 100KB or the current setting).
|
|
421 *
|
|
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.
|
|
424 *
|
|
425 * @method setSize
|
|
426 * @param value {Number} The size, in KB
|
|
427 * @return {String}
|
|
428 */
|
|
429 setSize: function(value)
|
|
430 {
|
|
431 var result = this.embeddedSWF.callSWF("setSize", [value]);
|
|
432 return result;
|
|
433 } ,
|
|
434
|
|
435 /**
|
|
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
|
|
441 */
|
|
442 displaySettings: function()
|
|
443 {
|
|
444 this.embeddedSWF.callSWF("displaySettings", []);
|
|
445 }
|
|
446
|
|
447 });
|
|
448
|
|
449
|
|
450 YAHOO.util.SWFStore.SWFURL = "swfstore.swf";
|
|
451
|
|
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"});
|