@@ -4,20 +4,21 @@ var shared = require('./shared');
4
4
var debug = require ( 'debug' ) ( 'scout:minicharts:geo' ) ;
5
5
var mapStyle = require ( './mapstyle' ) ;
6
6
// var async = require('async');
7
- // var xor = require('xor-it');
8
7
var app = require ( 'ampersand-app' ) ;
9
8
var format = require ( 'util' ) . format ;
10
-
9
+ var remote = window . require ( 'remote' ) ;
10
+ var dialog = remote . require ( 'dialog' ) ;
11
+ var BrowserWindow = remote . require ( 'browser-window' ) ;
11
12
12
13
var SHIFTKEY = 16 ;
13
14
var 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
- // }
20
15
16
+ var FATAL_GOOGLE_ERROR_CODES = [
17
+ 'InvalidKeyOrUnauthorizedURLMapError' ,
18
+ 'NotLoadingAPIFromGoogleMapError' ,
19
+ 'TOSViolationMapError' ,
20
+ 'UnauthorizedURLForClientIdMapError'
21
+ ] ;
21
22
// From: http://davidbcalhoun.com/2014/async.parallel-with-a-simple-timeout-node-js/
22
23
// async.parallel with optional timeout (options.timeoutMS)
23
24
// function parallel(options, tasks, cb) {
@@ -59,10 +60,12 @@ var minicharts_d3fns_geo = function() {
59
60
60
61
var margin = shared . margin ;
61
62
62
- function disableMapsFeature ( ) {
63
+ function disableMapsFeature ( permanent ) {
63
64
// disable both in feature flag (for this run) and localStorage
64
65
app . setFeature ( 'Google Map Minicharts' , false ) ;
65
- localStorage . disableGoogleMaps = true ;
66
+ if ( permanent ) {
67
+ localStorage . disableGoogleMaps = true ;
68
+ }
66
69
delete window . google ;
67
70
options . view . parent . render ( ) ;
68
71
}
@@ -77,8 +80,38 @@ var minicharts_d3fns_geo = function() {
77
80
if ( match ) {
78
81
errorCode = match [ 1 ] . trim ( ) ;
79
82
}
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
82
115
} ;
83
116
84
117
var script = document . createElement ( 'script' ) ;
@@ -216,7 +249,8 @@ var minicharts_d3fns_geo = function() {
216
249
. style ( {
217
250
width : ( width - margin . left - margin . right ) + 'px' ,
218
251
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;'
220
254
} ) ;
221
255
222
256
if ( ! window . google ) {
0 commit comments