From 86aa0fe8280eb2822f761d5cfa0190a49ad2c3df Mon Sep 17 00:00:00 2001 From: "jesus.abarca" Date: Mon, 21 Sep 2015 00:29:18 -0500 Subject: [PATCH 1/4] fix(loader): replace remove() with removeChild() to delete an existing script tag --- src/coffee/providers/map-loader.coffee | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/coffee/providers/map-loader.coffee b/src/coffee/providers/map-loader.coffee index f0db7e376..11c5fab4e 100644 --- a/src/coffee/providers/map-loader.coffee +++ b/src/coffee/providers/map-loader.coffee @@ -23,7 +23,10 @@ angular.module('uiGmapgoogle-maps.providers') query = _.map _.omit(options, omitOptions), (v, k) -> k + '=' + v - document.getElementById(scriptId).remove() if scriptId + if scriptId + scriptElem = document.getElementById(scriptId) + scriptElem.parentNode.removeChild(scriptElem) + query = query.join '&' script = document.createElement 'script' script.id = scriptId = "ui_gmap_map_load_#{uuid.generate()}" From f3ddd322fea7c9880fe1d5b895e86ce991692b8a Mon Sep 17 00:00:00 2001 From: "jesus.abarca" Date: Fri, 16 Oct 2015 00:40:09 -0500 Subject: [PATCH 2/4] fix(loader): check for window.navigator.connection until device is ready --- .../google-map-api-provider.spec.coffee | 34 +++++++++++++++++-- src/coffee/providers/map-loader.coffee | 24 ++++++++++--- 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/spec/coffee/providers/google-map-api-provider.spec.coffee b/spec/coffee/providers/google-map-api-provider.spec.coffee index 08b93d959..bdd6b356d 100644 --- a/spec/coffee/providers/google-map-api-provider.spec.coffee +++ b/spec/coffee/providers/google-map-api-provider.spec.coffee @@ -20,32 +20,60 @@ describe 'uiGmapGoogleMapApiProvider', -> it 'uses maps.google.cn when in china', -> options = { china: true, v: '3.17', libraries: '', language: 'en', sensor: 'false' } mapScriptLoader.load(options) + + loadEvent = document.createEvent 'CustomEvent' + loadEvent.initCustomEvent 'load', false, false, null + document.dispatchEvent loadEvent + lastScriptIndex = document.getElementsByTagName('script').length - 1 expect(document.getElementsByTagName('script')[lastScriptIndex].src).toContain('http://maps.google.cn/maps/api/js') describe 'on Cordova devices', -> beforeAll -> + window.cordova = {} window.navigator.connection = {} window.Connection = WIFI: 'wifi' NONE: 'none' - afterAll -> delete window.navigator.connection + afterAll -> + delete window.navigator.connection + delete window.cordova - it 'should include the script when the device is online', -> + it 'should wait for the deviceready event to include the script when the device is online', -> window.navigator.connection.type = window.Connection.WIFI options = { v: '3.17', libraries: '', language: 'en', sensor: 'false', device: 'online' } mapScriptLoader.load(options) + loadEvent = document.createEvent 'CustomEvent' + loadEvent.initCustomEvent 'load', false, false, null + document.dispatchEvent loadEvent + + lastScriptIndex = document.getElementsByTagName('script').length - 1 + expect(document.getElementsByTagName('script')[lastScriptIndex].src).not.toContain('device=online') + + readyEvent = document.createEvent 'CustomEvent' + readyEvent.initCustomEvent 'deviceready', false, false, null + document.dispatchEvent readyEvent + lastScriptIndex = document.getElementsByTagName('script').length - 1 expect(document.getElementsByTagName('script')[lastScriptIndex].src).toContain('device=online') - it 'should wait for the online event to include the script when the device is offline', -> + it 'should wait for the deviceready and online event to include the script when the device is offline', -> window.navigator.connection.type = window.Connection.NONE options = { v: '3.17', libraries: '', language: 'en', sensor: 'false', device: 'offline' } mapScriptLoader.load(options) + + loadEvent = document.createEvent 'CustomEvent' + loadEvent.initCustomEvent 'load', false, false, null + document.dispatchEvent loadEvent + + readyEvent = document.createEvent 'CustomEvent' + readyEvent.initCustomEvent 'deviceready', false, false, null + document.dispatchEvent readyEvent + lastScriptIndex = document.getElementsByTagName('script').length - 1 expect(document.getElementsByTagName('script')[lastScriptIndex].src).not.toContain('device=offline') diff --git a/src/coffee/providers/map-loader.coffee b/src/coffee/providers/map-loader.coffee index 11c5fab4e..5908390d4 100644 --- a/src/coffee/providers/map-loader.coffee +++ b/src/coffee/providers/map-loader.coffee @@ -37,6 +37,20 @@ angular.module('uiGmapgoogle-maps.providers') isGoogleMapsLoaded = -> angular.isDefined(window.google) and angular.isDefined(window.google.maps) + onWindowLoad = (options)-> + # if its a WebView + if !(!window.cordova && !window.PhoneGap && !window.phonegap && !window.forge) + document.addEventListener 'deviceready', -> + # Cordova specific https://github.com/apache/cordova-plugin-network-information/ + if window.navigator.connection && window.Connection && window.navigator.connection.type == window.Connection.NONE + document.addEventListener 'online', -> + includeScript options if !isGoogleMapsLoaded() + else + includeScript options + + else + includeScript options + load: (options)-> deferred = $q.defer() @@ -51,12 +65,12 @@ angular.module('uiGmapgoogle-maps.providers') deferred.resolve window.google.maps return - # Cordova specific https://github.com/apache/cordova-plugin-network-information/ - if window.navigator.connection && window.Connection && window.navigator.connection.type == window.Connection.NONE - document.addEventListener 'online', -> - includeScript options if !isGoogleMapsLoaded() + if document.readyState == 'complete' + onWindowLoad(options) else - includeScript options + window.addEventListener 'load', -> + window.removeEventListener 'load', onWindowLoad, false + onWindowLoad(options) # Return the promise deferred.promise From e3163fdc497de060664f97704fc9ce4b6e559a73 Mon Sep 17 00:00:00 2001 From: "jesus.abarca" Date: Fri, 16 Oct 2015 00:45:59 -0500 Subject: [PATCH 3/4] workaround for issue where network events are fired multiple times in a row --- src/coffee/providers/map-loader.coffee | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/coffee/providers/map-loader.coffee b/src/coffee/providers/map-loader.coffee index 5908390d4..1cc781909 100644 --- a/src/coffee/providers/map-loader.coffee +++ b/src/coffee/providers/map-loader.coffee @@ -2,6 +2,7 @@ angular.module('uiGmapgoogle-maps.providers') .factory('uiGmapMapScriptLoader', ['$q', 'uiGmapuuid', ($q, uuid) -> scriptId = undefined + lastNetworkStatus = undefined getScriptUrl = (options)-> #china doesn't allow https and has a special url @@ -44,7 +45,12 @@ angular.module('uiGmapgoogle-maps.providers') # Cordova specific https://github.com/apache/cordova-plugin-network-information/ if window.navigator.connection && window.Connection && window.navigator.connection.type == window.Connection.NONE document.addEventListener 'online', -> + if !lastNetworkStatus || lastNetworkStatus != 'online' + lastNetworkStatus = 'online'; includeScript options if !isGoogleMapsLoaded() + + document.addEventListener 'offline', -> + lastNetworkStatus = 'offline'; else includeScript options From 8080481383b265bddbbb2b87ce3188be8abd5f09 Mon Sep 17 00:00:00 2001 From: "jesus.abarca" Date: Sun, 1 Nov 2015 16:19:51 -0600 Subject: [PATCH 4/4] refactor(loader): use a function to check if app is within a webview and remove unnecessary checks for network api. --- src/coffee/providers/map-loader.coffee | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/coffee/providers/map-loader.coffee b/src/coffee/providers/map-loader.coffee index 1cc781909..6708cbb93 100644 --- a/src/coffee/providers/map-loader.coffee +++ b/src/coffee/providers/map-loader.coffee @@ -35,22 +35,27 @@ angular.module('uiGmapgoogle-maps.providers') script.src = getScriptUrl(options) + query document.body.appendChild script + isGoogleMapsLoaded = -> angular.isDefined(window.google) and angular.isDefined(window.google.maps) + isWebView = (obj = window) -> + !(!obj.cordova && !obj.PhoneGap && !obj.phonegap && !obj.forge) + onWindowLoad = (options)-> - # if its a WebView - if !(!window.cordova && !window.PhoneGap && !window.phonegap && !window.forge) + if isWebView() document.addEventListener 'deviceready', -> - # Cordova specific https://github.com/apache/cordova-plugin-network-information/ - if window.navigator.connection && window.Connection && window.navigator.connection.type == window.Connection.NONE + if window.navigator.connection.type == window.Connection.NONE document.addEventListener 'online', -> + # Workaround for issue where in Android, network events are fired multiple times in a row + # https://issues.apache.org/jira/browse/CB-7787 if !lastNetworkStatus || lastNetworkStatus != 'online' - lastNetworkStatus = 'online'; + lastNetworkStatus = 'online' includeScript options if !isGoogleMapsLoaded() document.addEventListener 'offline', -> - lastNetworkStatus = 'offline'; + lastNetworkStatus = 'offline' + else includeScript options