@@ -4,20 +4,21 @@ var shared = require('./shared');
44var debug = require ( 'debug' ) ( 'scout:minicharts:geo' ) ;
55var mapStyle = require ( './mapstyle' ) ;
66// var async = require('async');
7- // var xor = require('xor-it');
87var app = require ( 'ampersand-app' ) ;
98var format = require ( 'util' ) . format ;
10-
9+ var remote = window . require ( 'remote' ) ;
10+ var dialog = remote . require ( 'dialog' ) ;
11+ var BrowserWindow = remote . require ( 'browser-window' ) ;
1112
1213var SHIFTKEY = 16 ;
1314var APIKEY = 'AIzaSyDrhE1qbcnNIh4sK3t7GEcbLRdCNKWjlt0' ;
14- // var APIKEY = 'AIzaSyDrhE1qbcnNIh4sZp47GEcbLRdCNKWjlt4'; // wrong one
15- // function produceKey(text) {
16- // var key = 'Error: Google map could not be loaded, disabling feature';
17- // var res = xor(key, text);
18- // return res;
19- // }
2015
16+ var FATAL_GOOGLE_ERROR_CODES = [
17+ 'InvalidKeyOrUnauthorizedURLMapError' ,
18+ 'NotLoadingAPIFromGoogleMapError' ,
19+ 'TOSViolationMapError' ,
20+ 'UnauthorizedURLForClientIdMapError'
21+ ] ;
2122// From: http://davidbcalhoun.com/2014/async.parallel-with-a-simple-timeout-node-js/
2223// async.parallel with optional timeout (options.timeoutMS)
2324// function parallel(options, tasks, cb) {
@@ -59,10 +60,12 @@ var minicharts_d3fns_geo = function() {
5960
6061 var margin = shared . margin ;
6162
62- function disableMapsFeature ( ) {
63+ function disableMapsFeature ( permanent ) {
6364 // disable both in feature flag (for this run) and localStorage
6465 app . setFeature ( 'Google Map Minicharts' , false ) ;
65- localStorage . disableGoogleMaps = true ;
66+ if ( permanent ) {
67+ localStorage . disableGoogleMaps = true ;
68+ }
6669 delete window . google ;
6770 options . view . parent . render ( ) ;
6871 }
@@ -77,8 +80,38 @@ var minicharts_d3fns_geo = function() {
7780 if ( match ) {
7881 errorCode = match [ 1 ] . trim ( ) ;
7982 }
80- debug ( 'Error with code "%s" while loading Google Maps. Disabling Geo Querybuilder feature.' , errorCode ) ;
81- disableMapsFeature ( ) ;
83+ var message ;
84+ var detail ;
85+ if ( FATAL_GOOGLE_ERROR_CODES . indexOf ( errorCode ) !== - 1 ) {
86+ // maps API key is not valid, we may have had to deactivate it
87+ message = 'The Google Maps API key used in Compass is no longer '
88+ + 'valid.' ;
89+ detail = 'Compass will disable the Google Map feature permanently and '
90+ + 'replace the map with a simplified coordinate chart. Please check '
91+ + 'for an update to Compass to re-enable this feature.' ;
92+ disableMapsFeature ( true ) ;
93+ debug ( 'Error with code "%s" while loading Google Maps. Disabling Geo '
94+ + 'Querybuilder feature permanently.' , errorCode ) ;
95+ } else {
96+ message = 'There was a problem loading the Google Map.' ;
97+ detail = 'Compass will disable the Google Map feature temporarily '
98+ + 'and replace the map with a simplified coordinate chart. Compass '
99+ + 'will try to load a Google Map again next time you use the '
100+ + 'application.' ;
101+ disableMapsFeature ( false ) ;
102+ debug ( 'Error with code "%s" while loading Google Maps. Disabling Geo '
103+ + 'Querybuilder feature temporarily.' , errorCode ) ;
104+ }
105+ // show error dialog
106+ dialog . showMessageBox ( BrowserWindow . getFocusedWindow ( ) , {
107+ type : 'error' ,
108+ title : 'Error loading Google Maps' ,
109+ message : message ,
110+ detail : detail ,
111+ buttons : [ 'OK' ]
112+ } ) ;
113+
114+ // @todo thomasr/imlucas: add call to metrics.error here with errror code
82115 } ;
83116
84117 var script = document . createElement ( 'script' ) ;
@@ -216,7 +249,8 @@ var minicharts_d3fns_geo = function() {
216249 . style ( {
217250 width : ( width - margin . left - margin . right ) + 'px' ,
218251 height : ( height - margin . top - margin . bottom ) + 'px' ,
219- padding : margin . top + 'px ' + margin . right + 'px ' + margin . bottom + 'px ' + margin . left + 'px;'
252+ padding : margin . top + 'px ' + margin . right + 'px ' + margin . bottom
253+ + 'px ' + margin . left + 'px;'
220254 } ) ;
221255
222256 if ( ! window . google ) {
0 commit comments