PageRenderTime 65ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 1ms

/public/javascripts/FusionCharts.js

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