From svn_fusion at osgeo.org Mon Oct 1 06:49:20 2018 From: svn_fusion at osgeo.org (svn_fusion at osgeo.org) Date: Mon, 1 Oct 2018 06:49:20 -0700 Subject: [fusion-commits] r3047 - trunk/lib Message-ID: <20181001134920.5DAAB3901F2@trac.osgeo.org> Author: jng Date: 2018-10-01 06:49:20 -0700 (Mon, 01 Oct 2018) New Revision: 3047 Modified: trunk/lib/fusion.js Log: Re-instate client-side Google Maps script loading. It slipped my mind when I removed the client-side loading that we can actually do this in a "safe" manner. We just document.createElement() the script tag instead of document.write()'ing it. This is what we're doing for the widget scripts. As part of this change, several synchronous XHR requests we were making have been converted to async as well. Fixes #664 Modified: trunk/lib/fusion.js =================================================================== --- trunk/lib/fusion.js 2018-08-29 13:47:36 UTC (rev 3046) +++ trunk/lib/fusion.js 2018-10-01 13:49:20 UTC (rev 3047) @@ -399,64 +399,30 @@ } }, this); - var mapAgentUrl = getAgentUrl(); - var bAlreadyInit = (this.newTemplatePath === true && this.appDefJson != null); - var appDefUrl = Fusion.getQueryParam('ApplicationDefinition'); - if (!bAlreadyInit && - appDefUrl && ( - appDefUrl.indexOf('Library') == 0 || - appDefUrl.indexOf('Session') == 0)) { - - var fetchAppDef = function(appDefUrl, sessionId) { - var xhr = getXmlHttpRequest(); - xhr.open("GET", mapAgentUrl + "?OPERATION=GETRESOURCECONTENT&VERSION=1.0.0&LOCALE=en&CLIENTAGENT=MapGuide+Developer&RESOURCEID=" + appDefUrl + "&FORMAT=text%2Fxml&SESSION="+ sessionId, false); - try { - xhr.send(null); - var appDefXML = xhr.responseXML.documentElement; - - if (appDefXML) { - var bingMapKeyElement = appDefXML.getElementsByTagName("BingMapKey")[0]; - if (bingMapKeyElement) { - var bingMapKey = bingMapKeyElement.textContent; - if (!bingMapKey) - bingMapKey = bingMapKeyElement.text; - Fusion.bingMapKey = bingMapKey; - } - onAppDefFetched(); - } - } catch (e) { - if (xhr.statusText == "MgResourceNotFoundException") { - alert("Failed to fetch Application Definition. The specified resource could not be found"); - } //Anything else we can't do anything about as TraceKit is probably not loaded yet + getAgentUrl(function(mapAgentUrl) { + var bAlreadyInit = (this.newTemplatePath === true && this.appDefJson != null); + var appDefUrl = Fusion.getQueryParam('ApplicationDefinition'); + if (!bAlreadyInit && + appDefUrl && ( + appDefUrl.indexOf('Library') == 0 || + appDefUrl.indexOf('Session') == 0)) { + var cb = function() { + fetchAppDef(mapAgentUrl, appDefUrl, Fusion.sessionId, onAppDefFetched); + }; + var passedSessionId = Fusion.getQueryParam('Session'); + if (passedSessionId == null || passedSessionId == "" ){ + createSessionId(Fusion.getQueryParam("Username"), Fusion.getQueryParam("Password"), cb); + } else { + getSiteVersion(passedSessionId, function(version) { + Fusion.sessionId = passedSessionId; + Fusion.siteVersion = version; + cb(); + }); } - }; - - var getSiteVersion = function(sessionId) { - var xhr = getXmlHttpRequest(); - var mapAgentUrl = getAgentUrl(); - xhr.open("GET", mapAgentUrl + "?OPERATION=GETSITEVERSION&VERSION=1.0.0&LOCALE=en&CLIENTAGENT=MapGuide+Developer&SESSION="+ sessionId, false); - xhr.send(null); - var verXML = xhr.responseXML.documentElement; - var el = verXML.getElementsByTagName("Version")[0]; - var version = el.textContent || el.text; - var bits = version.split('.'); - return new Array(parseInt(bits[0]), - parseInt(bits[1]), - parseInt(bits[2]), - parseInt(bits[3])); - }; - - var passedSessionId = Fusion.getQueryParam('Session'); - if (passedSessionId == null || passedSessionId == "" ){ - createSessionId(Fusion.getQueryParam("Username"), Fusion.getQueryParam("Password")); } else { - Fusion.sessionId = passedSessionId; - Fusion.siteVersion = getSiteVersion(passedSessionId); + onAppDefFetched(); } - fetchAppDef(appDefUrl, Fusion.sessionId); - } else { - onAppDefFetched(); - } + }); }, /** @@ -1639,7 +1605,7 @@ /* Load commercial layer API scripts */ /*This function is extracted from Fusion to get the MapAgentUrl */ - var getAgentUrl = function() { + var getAgentUrl = function(callback) { /* * if the application has been loaded from the same host as * fusion is installed in, then technically we don't need to @@ -1666,47 +1632,134 @@ var fusionURL = r + Fusion.getFusionURL() + configUrl; var xhr = getXmlHttpRequest(); - xhr.open("GET", fusionURL, false); + xhr.open("GET", fusionURL, true); + xhr.onload = function (e) { + if (xhr.readyState === 4) { + if (xhr.status === 200) { + //Can't use parseJSON as this JSON file will most likely have comments + eval("Fusion.configuration=" + xhr.responseText); + var s = Fusion.configuration.mapguide.webTierUrl; + /* if it is set, use it ... otherwise assume fusion is installed in + * the default location and compute the web tier url from that + */ + if (s) { + var nLength = s.length; + var slastChar = s.charAt((nLength - 1)); + if (slastChar != '/') { + s = s + "/"; + } + } else { + var idx = Fusion.fusionURL.lastIndexOf('fusion'); + if (idx == -1) { + s = Fusion.fusionURL + "../"; //loaded relatively from within fusion directory + } + else { + s = Fusion.fusionURL.substring(0, idx); + } + } + Fusion.configuration.mapguide.mapAgentUrl = s + 'mapagent/mapagent.fcgi'; + callback(Fusion.configuration.mapguide.mapAgentUrl); + } else { + console.error(xhr.statusText); + } + } + }; + xhr.onerror = function (e) { + console.error(xhr.statusText); + }; xhr.send(null); + } - //Can't use parseJSON as this JSON file will most likely have comments - eval("Fusion.configuration=" + xhr.responseText); - var s = Fusion.configuration.mapguide.webTierUrl; - /* if it is set, use it ... otherwise assume fusion is installed in - * the default location and compute the web tier url from that - */ - if (s) { - var nLength = s.length; - var slastChar = s.charAt((nLength - 1)); - if (slastChar != '/') { - s = s + "/"; - } - } - else { - var idx = Fusion.fusionURL.lastIndexOf('fusion'); - if (idx == -1) { - s = Fusion.fusionURL + "../"; //loaded relatively from within fusion directory - } - else { - s = Fusion.fusionURL.substring(0, idx); - } - } - Fusion.configuration.mapguide.mapAgentUrl = s + 'mapagent/mapagent.fcgi'; - return Fusion.configuration.mapguide.mapAgentUrl; - } + var fetchAppDef = function(mapAgentUrl, appDefUrl, sessionId, onAppDefFetched) { + var xhr = getXmlHttpRequest(); + xhr.open("GET", mapAgentUrl + "?OPERATION=GETRESOURCECONTENT&VERSION=1.0.0&LOCALE=en&CLIENTAGENT=MapGuide+Developer&RESOURCEID=" + appDefUrl + "&FORMAT=text%2Fxml&SESSION="+ sessionId, true); + xhr.onload = function (e) { + if (xhr.readyState === 4) { + if (xhr.status === 200) { + var appDefXML = xhr.responseXML.documentElement; + if (appDefXML) { + var bingMapKeyElement = appDefXML.getElementsByTagName("BingMapKey")[0]; + if (bingMapKeyElement) { + var bingMapKey = bingMapKeyElement.textContent; + if (!bingMapKey) + bingMapKey = bingMapKeyElement.text; + Fusion.bingMapKey = bingMapKey; + } + var googleElement = appDefXML.getElementsByTagName("GoogleScript")[0]; + if (googleElement) { + addCmsScriptElement(googleElement, function() { + return typeof(window.google) != 'undefined'; + }, onAppDefFetched); + } else { + onAppDefFetched(); + } + } + } else if (xhr.statusText == "MgResourceNotFoundException") { + alert("Failed to fetch Application Definition. The specified resource could not be found"); + } //Anything else we can't do anything about as TraceKit is probably not loaded yet + } + }; + xhr.onerror = function (e) { + alert("Error loading appdef: " + xhr.statusText); + }; + xhr.send(null); + }; - var addElement = function(element) { - if (!element) { - return; - } - var src = element.textContent; + var getSiteVersion = function(sessionId, callback) { + var xhr = getXmlHttpRequest(); + xhr.open("GET", mapAgentUrl + "?OPERATION=GETSITEVERSION&VERSION=1.0.0&LOCALE=en&CLIENTAGENT=MapGuide+Developer&SESSION="+ sessionId, true); + xhr.onload = function (e) { + if (xhr.readyState === 4) { + if (xhr.status === 200) { + var verXML = xhr.responseXML.documentElement; + var el = verXML.getElementsByTagName("Version")[0]; + var version = el.textContent || el.text; + var bits = version.split('.'); + callback([ + parseInt(bits[0]), + parseInt(bits[1]), + parseInt(bits[2]), + parseInt(bits[3]) + ]); + } else { + console.error(xhr.statusText); + } + } + }; + xhr.onerror = function (e) { + console.error(xhr.statusText); + }; + xhr.send(null); + }; + var addCmsScriptElement = function(element, readyTest, callback) { + if (!element) { + return; + } + var src = element.textContent; // For IE Browser if (!src) { src = element.text; - } - - document.writeln('