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