Skip to content

Commit 3b3c696

Browse files
bso-odoorobinlej
authored andcommitted
google map edit interaction becomes the general one
1 parent 0f4d366 commit 3b3c696

File tree

3 files changed

+68
-87
lines changed

3 files changed

+68
-87
lines changed

addons/html_builder/__manifest__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,6 @@
5959
'html_builder.assets_edit_frontend': [
6060
('include', 'website.assets_edit_frontend'),
6161
'html_builder/static/src/interactions/**/*.edit.*',
62-
# the google map edit interaction was reimplemented locally to replace this
63-
('remove', 'website/static/src/snippets/s_google_map/google_map.edit.js'),
6462
],
6563
'html_builder.iframe_add_dialog': [
6664
('include', 'web.assets_frontend'),

addons/html_builder/static/src/interactions/google_map.edit.js

Lines changed: 0 additions & 85 deletions
This file was deleted.

addons/website/static/src/snippets/s_google_map/google_map.edit.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)