UI adaptation for supporting ONAP portal SDK
[validation.git] / ui / src / main / webapp / static / fusion / sample / js / FusionCharts.js
1 /**
2  * FusionCharts: Flash Player detection and Chart embedding. Version 1.2.3F ( 22
3  * November 2008) - Specialized for FusionChartsFREE Checking Flash Version >=6
4  * and added updateChartXML() for FREE Charts. Version: 1.2.3 (1st September,
5  * 2008) - Added Fix for % and & characters, scaled dimensions, fixes in to
6  * properly handling of double quotes and single quotes in setDataXML()
7  * function. Version: 1.2.2 (10th July, 2008) - Added Fix for % scaled
8  * dimensions, fixes in setDataXML() and setDataURL() functions Version: 1.2.1
9  * (21st December, 2007) - Added setting up Transparent/opaque mode:
10  * setTransparent() function Version: 1.2 (1st November, 2007) - Added FORM
11  * fixes for IE Version: 1.1 (29th June, 2007) - Added Player detection, New
12  * conditional fixes for IE
13  *
14  * Morphed from SWFObject (http://blog.deconcept.com/swfobject/) under MIT
15  * License: http://www.opensource.org/licenses/mit-license.php
16  *
17  */
18 if (typeof infosoftglobal == "undefined")
19     var infosoftglobal = new Object();
20 if (typeof infosoftglobal.FusionChartsUtil == "undefined")
21     infosoftglobal.FusionChartsUtil = new Object();
22 infosoftglobal.FusionCharts = function(swf, id, w, h, debugMode,
23         registerWithJS, c, scaleMode, lang, detectFlashVersion,
24         autoInstallRedirect) {
25     if (!document.getElementById) {
26         return;
27     }
28
29     // Flag to see whether data has been set initially
30     this.initialDataSet = false;
31
32     // Create container objects
33     this.params = new Object();
34     this.variables = new Object();
35     this.attributes = new Array();
36
37     // Set attributes for the SWF
38     if (swf) {
39         this.setAttribute('swf', swf);
40     }
41     if (id) {
42         this.setAttribute('id', id);
43     }
44
45     w = w.toString().replace(/\%$/, "%25");
46     if (w) {
47         this.setAttribute('width', w);
48     }
49     h = h.toString().replace(/\%$/, "%25");
50     if (h) {
51         this.setAttribute('height', h);
52     }
53
54     // Set background color
55     if (c) {
56         this.addParam('bgcolor', c);
57     }
58
59     // Set Quality
60     this.addParam('quality', 'high');
61
62     // Add scripting access parameter
63     this.addParam('allowScriptAccess', 'always');
64
65     // Pass width and height to be appended as chartWidth and chartHeight
66     this.addVariable('chartWidth', w);
67     this.addVariable('chartHeight', h);
68
69     // Whether in debug mode
70     debugMode = debugMode ? debugMode : 0;
71     this.addVariable('debugMode', debugMode);
72     // Pass DOM ID to Chart
73     this.addVariable('DOMId', id);
74     // Whether to registed with JavaScript
75     registerWithJS = registerWithJS ? registerWithJS : 0;
76     this.addVariable('registerWithJS', registerWithJS);
77
78     // Scale Mode of chart
79     scaleMode = scaleMode ? scaleMode : 'noScale';
80     this.addVariable('scaleMode', scaleMode);
81
82     // Application Message Language
83     lang = lang ? lang : 'EN';
84     this.addVariable('lang', lang);
85
86     // Whether to auto detect and re-direct to Flash Player installation
87     this.detectFlashVersion = detectFlashVersion ? detectFlashVersion : 1;
88     this.autoInstallRedirect = autoInstallRedirect ? autoInstallRedirect : 1;
89
90     // Ger Flash Player version
91     this.installedVer = infosoftglobal.FusionChartsUtil.getPlayerVersion();
92
93     if (!window.opera && document.all && this.installedVer.major > 7) {
94         // Only add the onunload cleanup if the Flash Player version supports
95         // External Interface and we are in IE
96         infosoftglobal.FusionCharts.doPrepUnload = true;
97     }
98 }
99
100 infosoftglobal.FusionCharts.prototype = {
101     setAttribute : function(name, value) {
102         this.attributes[name] = value;
103     },
104     getAttribute : function(name) {
105         return this.attributes[name];
106     },
107     addParam : function(name, value) {
108         this.params[name] = value;
109     },
110     getParams : function() {
111         return this.params;
112     },
113     addVariable : function(name, value) {
114         this.variables[name] = value;
115     },
116     getVariable : function(name) {
117         return this.variables[name];
118     },
119     getVariables : function() {
120         return this.variables;
121     },
122     getVariablePairs : function() {
123         var variablePairs = new Array();
124         var key;
125         var variables = this.getVariables();
126         for (key in variables) {
127             variablePairs.push(key + "=" + variables[key]);
128         }
129         return variablePairs;
130     },
131     getSWFHTML : function() {
132         var swfNode = "";
133         if (navigator.plugins && navigator.mimeTypes
134                 && navigator.mimeTypes.length) {
135             // netscape plugin architecture
136             swfNode = '<embed type="application/x-shockwave-flash" src="'
137                     + this.getAttribute('swf') + '" width="'
138                     + this.getAttribute('width') + '" height="'
139                     + this.getAttribute('height') + '"  ';
140             swfNode += ' id="' + this.getAttribute('id') + '" name="'
141                     + this.getAttribute('id') + '" ';
142             var params = this.getParams();
143             for ( var key in params) {
144                 swfNode += [ key ] + '="' + params[key] + '" ';
145             }
146             var pairs = this.getVariablePairs().join("&");
147             if (pairs.length > 0) {
148                 swfNode += 'flashvars="' + pairs + '"';
149             }
150             swfNode += '/>';
151         } else { // PC IE
152             swfNode = '<object id="'
153                     + this.getAttribute('id')
154                     + '" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="'
155                     + this.getAttribute('width') + '" height="'
156                     + this.getAttribute('height') + '">';
157             swfNode += '<param name="movie" value="' + this.getAttribute('swf')
158                     + '" />';
159             var params = this.getParams();
160             for ( var key in params) {
161                 swfNode += '<param name="' + key + '" value="' + params[key]
162                         + '" />';
163             }
164             var pairs = this.getVariablePairs().join("&");
165             if (pairs.length > 0) {
166                 swfNode += '<param name="flashvars" value="' + pairs + '" />';
167             }
168             swfNode += "</object>";
169         }
170         return swfNode;
171     },
172     setDataURL : function(strDataURL) {
173         // This method sets the data URL for the chart.
174         // If being set initially
175         if (this.initialDataSet == false) {
176             this.addVariable('dataURL', strDataURL);
177             // Update flag
178             this.initialDataSet = true;
179         } else {
180             // Else, we update the chart data using External Interface
181             // Get reference to chart object
182             var chartObj = infosoftglobal.FusionChartsUtil.getChartObject(this
183                     .getAttribute('id'));
184
185             if (!chartObj.setDataURL) {
186                 __flash__addCallback(chartObj, "setDataURL");
187             }
188
189             chartObj.setDataURL(strDataURL);
190         }
191     },
192     // This function :
193     // fixes the double quoted attributes to single quotes
194     // Encodes all quotes inside attribute values
195     // Encodes % to %25 and & to %26;
196     encodeDataXML : function(strDataXML) {
197
198         var regExpReservedCharacters = [ "\\$", "\\+" ];
199         var arrDQAtt = strDataXML.match(/=\s*\".*?\"/g);
200         if (arrDQAtt) {
201             for (var i = 0; i < arrDQAtt.length; i++) {
202                 var repStr = arrDQAtt[i].replace(/^=\s*\"|\"$/g, "");
203                 repStr = repStr.replace(/\'/g, "%26apos;");
204                 var strTo = strDataXML.indexOf(arrDQAtt[i]);
205                 var repStrr = "='" + repStr + "'";
206                 var strStart = strDataXML.substring(0, strTo);
207                 var strEnd = strDataXML.substring(strTo + arrDQAtt[i].length);
208                 var strDataXML = strStart + repStrr + strEnd;
209             }
210         }
211
212         strDataXML = strDataXML.replace(/\"/g, "%26quot;");
213         strDataXML = strDataXML.replace(/%(?![\da-f]{2}|[\da-f]{4})/ig, "%25");
214         strDataXML = strDataXML.replace(/\&/g, "%26");
215
216         return strDataXML;
217
218     },
219     setDataXML : function(strDataXML) {
220         // If being set initially
221         if (this.initialDataSet == false) {
222             // This method sets the data XML for the chart INITIALLY.
223             this.addVariable('dataXML', this.encodeDataXML(strDataXML));
224             // Update flag
225             this.initialDataSet = true;
226         } else {
227             // Else, we update the chart data using External Interface
228             // Get reference to chart object
229             var chartObj = infosoftglobal.FusionChartsUtil.getChartObject(this
230                     .getAttribute('id'));
231             chartObj.setDataXML(strDataXML);
232         }
233     },
234     setTransparent : function(isTransparent) {
235         // Sets chart to transparent mode when isTransparent is true (default)
236         // When no parameter is passed, we assume transparent to be true.
237         if (typeof isTransparent == "undefined") {
238             isTransparent = true;
239         }
240         // Set the property
241         if (isTransparent)
242             this.addParam('WMode', 'transparent');
243         else
244             this.addParam('WMode', 'Opaque');
245     },
246
247     render : function(elementId) {
248         // First check for installed version of Flash Player - we need a minimum
249         // of 6
250         if ((this.detectFlashVersion == 1) && (this.installedVer.major < 6)) {
251             if (this.autoInstallRedirect == 1) {
252                 // If we can auto redirect to install the player?
253                 var installationConfirm = window
254                         .confirm("You need Adobe Flash Player 6 (or above) to view the charts. It is a free and lightweight installation from Adobe.com. Please click on Ok to install the same.");
255                 if (installationConfirm) {
256                     window.location = "http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash";
257                 } else {
258                     return false;
259                 }
260             } else {
261                 // Else, do not take an action. It means the developer has
262                 // specified a message in the DIV (and probably a link).
263                 // So, expect the developers to provide a course of way to their
264                 // end users.
265                 // window.alert("You need Adobe Flash Player 8 (or above) to
266                 // view the charts. It is a free and lightweight installation
267                 // from Adobe.com. ");
268                 return false;
269             }
270         } else {
271             // Render the chart
272             var n = (typeof elementId == 'string') ? document
273                     .getElementById(elementId) : elementId;
274             n.innerHTML = this.getSWFHTML();
275
276             // Added <FORM> compatibility
277             // Check if it's added in Mozilla embed array or if already exits
278             if (!document.embeds[this.getAttribute('id')]
279                     && !window[this.getAttribute('id')])
280                 window[this.getAttribute('id')] = document.getElementById(this
281                         .getAttribute('id'));
282             // or else document.forms[formName/formIndex][chartId]
283             return true;
284         }
285     }
286 }
287
288 /* ---- detection functions ---- */
289 infosoftglobal.FusionChartsUtil.getPlayerVersion = function() {
290     var PlayerVersion = new infosoftglobal.PlayerVersion([ 0, 0, 0 ]);
291     if (navigator.plugins && navigator.mimeTypes.length) {
292         var x = navigator.plugins["Shockwave Flash"];
293         if (x && x.description) {
294             PlayerVersion = new infosoftglobal.PlayerVersion(x.description
295                     .replace(/([a-zA-Z]|\s)+/, "").replace(/(\s+r|\s+b[0-9]+)/,
296                             ".").split("."));
297         }
298     } else if (navigator.userAgent
299             && navigator.userAgent.indexOf("Windows CE") >= 0) {
300         // If Windows CE
301         var axo = 1;
302         var counter = 3;
303         while (axo) {
304             try {
305                 counter++;
306                 axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash."
307                         + counter);
308                 PlayerVersion = new infosoftglobal.PlayerVersion([ counter, 0,
309                         0 ]);
310             } catch (e) {
311                 axo = null;
312             }
313         }
314     } else {
315         // Win IE (non mobile)
316         // Do minor version lookup in IE, but avoid Flash Player 6 crashing
317         // issues
318         try {
319             var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
320         } catch (e) {
321             try {
322                 var axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
323                 PlayerVersion = new infosoftglobal.PlayerVersion([ 6, 0, 21 ]);
324                 axo.AllowScriptAccess = "always"; // error if player version <
325                                                     // 6.0.47 (thanks to Michael
326                                                     // Williams @ Adobe for this
327                                                     // code)
328             } catch (e) {
329                 if (PlayerVersion.major == 6) {
330                     return PlayerVersion;
331                 }
332             }
333             try {
334                 axo = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
335             } catch (e) {
336             }
337         }
338         if (axo != null) {
339             PlayerVersion = new infosoftglobal.PlayerVersion(axo.GetVariable(
340                     "$version").split(" ")[1].split(","));
341         }
342     }
343     return PlayerVersion;
344 }
345 infosoftglobal.PlayerVersion = function(arrVersion) {
346     this.major = arrVersion[0] != null ? parseInt(arrVersion[0]) : 0;
347     this.minor = arrVersion[1] != null ? parseInt(arrVersion[1]) : 0;
348     this.rev = arrVersion[2] != null ? parseInt(arrVersion[2]) : 0;
349 }
350 // ------------ Fix for Out of Memory Bug in IE in FP9 ---------------//
351 /* Fix for video streaming bug */
352 infosoftglobal.FusionChartsUtil.cleanupSWFs = function() {
353     var objects = document.getElementsByTagName("OBJECT");
354     for (var i = objects.length - 1; i >= 0; i--) {
355         objects[i].style.display = 'none';
356         for ( var x in objects[i]) {
357             if (typeof objects[i][x] == 'function') {
358                 objects[i][x] = function() {
359                 };
360             }
361         }
362     }
363 }
364 // Fixes bug in fp9
365 if (infosoftglobal.FusionCharts.doPrepUnload) {
366     if (!infosoftglobal.unloadSet) {
367         infosoftglobal.FusionChartsUtil.prepUnload = function() {
368             __flash_unloadHandler = function() {
369             };
370             __flash_savedUnloadHandler = function() {
371             };
372             window.attachEvent("onunload",
373                     infosoftglobal.FusionChartsUtil.cleanupSWFs);
374         }
375         window.attachEvent("onbeforeunload",
376                 infosoftglobal.FusionChartsUtil.prepUnload);
377         infosoftglobal.unloadSet = true;
378     }
379 }
380 /* Add document.getElementById if needed (mobile IE < 5) */
381 if (!document.getElementById && document.all) {
382     document.getElementById = function(id) {
383         return document.all[id];
384     }
385 }
386 /* Add Array.push if needed (ie5) */
387 if (Array.prototype.push == null) {
388     Array.prototype.push = function(item) {
389         this[this.length] = item;
390         return this.length;
391     }
392 }
393
394 /* Function to return Flash Object from ID */
395 infosoftglobal.FusionChartsUtil.getChartObject = function(id) {
396     var chartRef = null;
397     if (navigator.appName.indexOf("Microsoft Internet") == -1) {
398         if (document.embeds && document.embeds[id])
399             chartRef = document.embeds[id];
400         else
401             chartRef = window.document[id];
402     } else {
403         chartRef = window[id];
404     }
405     if (!chartRef)
406         chartRef = document.getElementById(id);
407
408     return chartRef;
409 }
410 /*
411  * Function to update chart's data at client side (FOR FusionCharts vFREE and
412  * 2.x
413  */
414 infosoftglobal.FusionChartsUtil.updateChartXML = function(chartId, strXML) {
415     // Get reference to chart object
416     var chartObj = infosoftglobal.FusionChartsUtil.getChartObject(chartId);
417     // Set dataURL to null
418     chartObj.SetVariable("_root.dataURL", "");
419     // Set the flag
420     chartObj.SetVariable("_root.isNewData", "1");
421     // Set the actual data
422     chartObj.SetVariable("_root.newData", strXML);
423     // Go to the required frame
424     chartObj.TGotoLabel("/", "JavaScriptHandler");
425 }
426
427 /* Aliases for easy usage */
428 var getChartFromId = infosoftglobal.FusionChartsUtil.getChartObject;
429 var updateChartXML = infosoftglobal.FusionChartsUtil.updateChartXML;
430 var FusionCharts = infosoftglobal.FusionCharts;