[fusion-commits] r3048 - in branches/fusion-mg31: . lib

svn_fusion at osgeo.org svn_fusion at osgeo.org
Mon Oct 1 06:57:45 PDT 2018


Author: jng
Date: 2018-10-01 06:57:45 -0700 (Mon, 01 Oct 2018)
New Revision: 3048

Modified:
   branches/fusion-mg31/
   branches/fusion-mg31/lib/fusion.js
Log:
Merged revision(s) 3047 from trunk:
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
........


Index: branches/fusion-mg31
===================================================================
--- branches/fusion-mg31	2018-10-01 13:49:20 UTC (rev 3047)
+++ branches/fusion-mg31	2018-10-01 13:57:45 UTC (rev 3048)

Property changes on: branches/fusion-mg31
___________________________________________________________________
Modified: svn:mergeinfo
## -11,4 +11,4 ##
 /sandbox/robust_error_handling:2818-2825
 /sandbox/stamen:2873-2875
 /sandbox/tiling:2845-2846
-/trunk:2943-3032,3045
\ No newline at end of property
+/trunk:2943-3032,3045,3047
\ No newline at end of property
Modified: branches/fusion-mg31/lib/fusion.js
===================================================================
--- branches/fusion-mg31/lib/fusion.js	2018-10-01 13:49:20 UTC (rev 3047)
+++ branches/fusion-mg31/lib/fusion.js	2018-10-01 13:57:45 UTC (rev 3048)
@@ -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('<script src="' + src + '"><\/script>'); 
+        }
+        var script = document.createElement("script");
+        script.defer = false;
+        script.async = true;
+        script.type = "text/javascript";
+        script.src = src;
+        document.getElementsByTagName('head')[0].appendChild(script);
+
+        var testID;
+        var tester = function() {
+            if (readyTest()) {
+                clearTimeout(testID);
+                //console.log((new Date().getTime()) + " - addCmsScriptElement (" + src + ") - readiness check passed");
+                callback();
+            } else {
+                //console.log((new Date().getTime()) + " - addCmsScriptElement (" + src + ") - readiness check failed. Retrying in 200ms");
+                testID = setTimeout(tester, 200);
+            }
+        };
+        tester();
     }
 
     /**
@@ -1715,7 +1768,7 @@
      * Pre-create a session to avoid the 401 Unauthorized dialog
      *
      */
-    var createSessionId = function(user, pass){
+    var createSessionId = function(user, pass, callback) {
         var xhr = getXmlHttpRequest();
         var url = Fusion.fusionURL+"layers/MapGuide/php/CreateSession.php";
 		var params = "";
@@ -1740,6 +1793,7 @@
                         parseInt(bits[2]),
                         parseInt(bits[3])
                     );
+                    callback();
                 } else {
                     console.error(xhr.statusText);
                 }



More information about the fusion-commits mailing list