]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/javascript/yui/build/swfstore/swfstore.js
Release 6.5.0
[Github/sugarcrm.git] / include / javascript / yui / build / swfstore / swfstore.js
1 /*
2 Copyright (c) 2011, Yahoo! Inc. All rights reserved.
3 Code licensed under the BSD License:
4 http://developer.yahoo.com/yui/license.html
5 version: 2.9.0
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                                                         {allowedDomain : document.location.hostname, 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                         if(typeof data == "string")
255                         {
256                                 //double encode strings to prevent parsing error
257                                 //http://yuilibrary.com/projects/yui2/ticket/2528593
258                                 data = data.replace(/\\/g, '\\\\');
259                         }
260                         
261                         return this.embeddedSWF.callSWF("setItem", [location, data]);
262                 } ,
263                 
264            /**
265             * Returns the value of the store at the specified index, if any.
266                 * @method getValueAt
267             * @param index {Number} The index of the stored item
268             * @return {Object} The value of the store at that index
269             * 
270             */      
271                 getValueAt: function(index) 
272                 {
273                         return this.embeddedSWF.callSWF("getValueAt", [index]);
274                 },
275
276            /**
277             * Returns the key name in storage, if any, at the specified index.
278             * 
279             * @param index {Number} The index of the "cookie" or store
280                 * @return {Object}The data
281                 * @method setItem
282             * 
283             */      
284                 getNameAt: function(index) 
285                 {
286                         return this.embeddedSWF.callSWF("getNameAt", [index]);
287                 },
288                 
289                 
290             /**
291             * Returns the value of the item in storage, if any.
292             * @method getValueOf
293             * @param location {String} The name of the "cookie" or store
294                 * @return {Object} The data
295             * 
296             */
297                 getValueOf: function(location) 
298                 {
299                         return this.embeddedSWF.callSWF("getValueOf", [location]);
300                 } ,
301
302             /**
303             *  Returns the data type of of the storage.
304                 * <p>May be one of the following types:
305             * <ul>
306             * <li>boolean</li>
307             * <li>function</li>
308             * <li>number</li>
309             * <li>object</li>
310             * <li>string</li>
311             * <li>number</li>
312             * <li>xml</li>
313             * </ul>
314             * </p>
315             * @method getTypeOf
316             * @param location {String} The name of the "cookie" or store
317                 * @return {String} The type
318             * 
319             */
320                 getTypeOf: function(location) 
321                 {
322                         return this.embeddedSWF.callSWF("getTypeOf", [location]);
323                 } ,
324
325             /**
326             *  Returns the data type of of the storage.
327                 * <p>May be one of the following types:
328             * <ul>
329             * <li>boolean</li>
330             * <li>function</li>
331             * <li>number</li>
332             * <li>object</li>
333             * <li>string</li>
334             * <li>number</li>
335             * <li>xml</li>
336             * </ul>
337             * </p>
338             * @method getTypeAt
339             * @param location {Number} The index of the "cookie" or store
340                 * @return {String} The type
341             * 
342             */
343                 getTypeAt: function(index) 
344                 {
345                         return this.embeddedSWF.callSWF("getTypeAt", [index]);
346                 } ,
347                  
348                 /**
349                  * Returns the items in storage as an array.
350                  * @method getItems
351                  * @return {Object} The data.
352                  * @public
353                  */
354                 getItems: function() 
355                 {
356                         return this.embeddedSWF.callSWF("getItems", []);
357                 },
358
359             /**
360             * Removes the item in storage, if any.
361             * @method removeItem
362             * @param location {String} The name of the "cookie" or store
363             * 
364             */
365                 removeItem: function(location) 
366                 {
367                         return this.embeddedSWF.callSWF("removeItem", [location]);
368                 } ,
369
370             /**
371             * Removes the item in storage at the specified index, if any.
372             * @method removeItem
373             * @param index {Number} The index of the "cookie" or store
374             * 
375             */
376                 removeItemAt: function(index) 
377                 {
378                         return this.embeddedSWF.callSWF("removeItemAt", [index]);
379                 } ,
380                 
381             /**
382             * Returns the number of items in storage, if any.
383             * @method getLength
384             * @return {Number} The number of items
385             * 
386             */
387                 getLength: function() 
388                 {
389                         return this.embeddedSWF.callSWF("getLength", []);
390                 } ,
391                 
392            /**
393             * Removes all data in local storage for this domain.
394             * <p>Be careful when using this method, as it may 
395             * remove stored information that is used by other applications
396             * in this domain </p>
397             * @method clear
398             */          
399                 clear: function() 
400                 {
401                         return this.embeddedSWF.callSWF("clear", []);
402                 } ,
403                 
404             /**
405              * Gets the current size, in KB, of the amount of space taken by the current store.
406                  * Note that this is calculated, and may take time depending on the number of items stored
407              * @method calculateCurrentSize
408              * @return {Number} The size of the store in KB
409              */         
410                 calculateCurrentSize: function() 
411                 {
412                         return this.embeddedSWF.callSWF("calculateCurrentSize", []);
413                 } ,
414                 
415             /**
416              * Gets the timestamp of the last store. This value is automatically set when 
417              * data is stored.
418              * @method getModificationDate
419              * @return {Date} A Date object
420              */
421                 getModificationDate: function() 
422                 {
423                         return this.embeddedSWF.callSWF("getModificationDate", []);
424                 } ,
425                 
426                 /**
427                 * This method requests more storage (if the amount is above 100KB or the current setting).
428                 * 
429                 * The request dialog has to be displayed within the Flash player itself
430                 * so the SWF it is called from must be visible and at least 215px x 138px (w x h) in size.
431                 * 
432                 * @method setSize
433                 * @param value {Number} The size, in KB
434                 * @return {String} 
435                 */              
436                 setSize: function(value) 
437                 {
438                         var result = this.embeddedSWF.callSWF("setSize", [value]);
439                         return result;
440                 } ,
441                 
442                 /**
443                  * Displays the settings dialog to allow the user to configure
444                  * storage settings manually. If the SWF height and width are smaller than
445                  * what is allowable to display the local settings panel,
446                  * an openExternalDialog message will be sent to JavaScript.
447                  * @method displaySettings
448                  */             
449                 displaySettings: function() 
450                 {
451                         this.embeddedSWF.callSWF("displaySettings", []);
452                 } 
453
454 });
455
456
457 YAHOO.util.SWFStore.SWFURL = "swfstore.swf";
458 YAHOO.register("swfstore", YAHOO.util.SWFStore, {version: "2.9.0", build: "2800"});