@@ -5,6 +5,74 @@ const GoogleMapEdit = I => class extends I {
55 setup ( ) {
66 super . setup ( ) ;
77 this . canSpecifyKey = true ;
8+ this . websiteEditService = this . services . website_edit ;
9+ this . websiteMapService = this . services . website_map ;
10+ }
11+
12+ async willStart ( ) {
13+ const isLoaded =
14+ ( typeof google === "object" &&
15+ typeof google . maps === "object" &&
16+ ! this . websiteEditService . callShared (
17+ "googleMapsOption" ,
18+ "shouldRefetchApiKey"
19+ ) ) ||
20+ ( await this . loadGoogleMaps ( false ) ) ;
21+ if ( isLoaded ) {
22+ this . canStart = await this . websiteEditService . callShared (
23+ "googleMapsOption" ,
24+ "initializeGoogleMaps" ,
25+ [ this . el , google . maps ]
26+ ) ;
27+ }
28+ }
29+
30+ /**
31+ * Get the stored API key if any (or open a dialog to ask the user for one),
32+ * load and configure the Google Maps API.
33+ *
34+ * @param {boolean } [forceReconfigure=false]
35+ * @returns {Promise<void> }
36+ */
37+ async loadGoogleMaps ( forceReconfigure = false ) {
38+ /** @type {string | undefined } */
39+ const apiKey = await this . websiteMapService . getGMapAPIKey ( true ) ;
40+ const apiKeyValidation = await this . websiteMapService . validateGMapApiKey ( apiKey ) ;
41+ const shouldReconfigure = forceReconfigure || ! apiKeyValidation . isValid ;
42+ let didReconfigure = false ;
43+ if ( shouldReconfigure ) {
44+ didReconfigure = await this . websiteEditService . callShared (
45+ "googleMapsOption" ,
46+ "configureGMapsAPI" ,
47+ apiKey
48+ ) ;
49+ if ( ! didReconfigure ) {
50+ this . websiteEditService . callShared ( "remove" , "removeElement" , this . el ) ;
51+ }
52+ }
53+ if ( ! shouldReconfigure || didReconfigure ) {
54+ const shouldRefetch = this . websiteEditService . callShared (
55+ "googleMapsOption" ,
56+ "shouldRefetchApiKey"
57+ ) ;
58+ return ! ! ( await this . loadGoogleMapsAPIFromService ( shouldRefetch || didReconfigure ) ) ;
59+ } else {
60+ return false ;
61+ }
62+ }
63+
64+ /**
65+ * Load the Google Maps API from the Google Map Service.
66+ * This method is set apart so it can be overridden for testing.
67+ *
68+ * @param {boolean } [shouldRefetch]
69+ * @returns {Promise<string|undefined> } A promise that resolves to an API
70+ * key if found.
71+ */
72+ async loadGoogleMapsAPIFromService ( shouldRefetch ) {
73+ const apiKey = await this . websiteMapService . loadGMapAPI ( true , shouldRefetch ) ;
74+ this . websiteEditService . callShared ( "googleMapsOption" , "shouldNotRefetchApiKey" ) ;
75+ return ! ! apiKey ;
876 }
977}
1078
0 commit comments