@geolonia/embed 4.0.0 → 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/.eslintrc.js +1 -2
  2. package/.github/workflows/build.yml +1 -1
  3. package/.mocharc.js +9 -0
  4. package/.tool-versions +1 -1
  5. package/PLUGIN.md +1 -1
  6. package/dist/embed +1 -1
  7. package/dist/embed-chunks/4472555f7ea90b733a8e.js +2 -0
  8. package/dist/embed-chunks/4b3a60cc98a6be867ce3.js +1 -0
  9. package/dist/embed.d.ts +62 -0
  10. package/dist/embed.js +1 -1
  11. package/dist/lib/CustomAttributionControl.d.ts +45 -0
  12. package/dist/lib/controls/geolonia-control.d.ts +7 -0
  13. package/dist/lib/geolonia-map.d.ts +22 -0
  14. package/dist/lib/geolonia-marker.d.ts +10 -0
  15. package/dist/lib/keyring.d.ts +29 -0
  16. package/dist/lib/keyring.test.d.ts +1 -0
  17. package/dist/lib/maplibre-util.d.ts +27 -0
  18. package/dist/lib/parse-atts.d.ts +6 -0
  19. package/dist/lib/parse-atts.test.d.ts +1 -0
  20. package/dist/lib/render.d.ts +5 -0
  21. package/dist/lib/simplestyle-vector.d.ts +26 -0
  22. package/dist/lib/simplestyle.d.ts +29 -0
  23. package/dist/lib/simplestyle.test.d.ts +1 -0
  24. package/dist/lib/util.d.ts +57 -0
  25. package/dist/lib/util.test.d.ts +1 -0
  26. package/dist/types-internal.d.ts +4 -0
  27. package/dist/version.d.ts +1 -0
  28. package/package.json +17 -22
  29. package/scripts/build-version.mjs +12 -0
  30. package/src/embed.ts +85 -0
  31. package/src/lib/{CustomAttributionControl.js → CustomAttributionControl.ts} +14 -2
  32. package/src/lib/controls/{geolonia-control.js → geolonia-control.ts} +6 -2
  33. package/src/lib/{geolonia-map.js → geolonia-map.ts} +44 -47
  34. package/src/lib/{geolonia-marker.js → geolonia-marker.ts} +10 -9
  35. package/src/lib/keyring.test.ts +66 -0
  36. package/src/lib/keyring.ts +86 -0
  37. package/src/lib/{maplibre-util.js → maplibre-util.ts} +1 -1
  38. package/src/lib/{parse-atts.test.js → parse-atts.test.ts} +5 -1
  39. package/src/lib/{parse-atts.js → parse-atts.ts} +14 -9
  40. package/src/lib/render.ts +100 -0
  41. package/src/lib/{simplestyle-vector.js → simplestyle-vector.ts} +4 -3
  42. package/src/lib/{simplestyle.test.js → simplestyle.test.ts} +26 -27
  43. package/src/lib/{simplestyle.js → simplestyle.ts} +9 -8
  44. package/src/lib/{util.test.js → util.test.ts} +51 -46
  45. package/src/lib/{util.js → util.ts} +15 -14
  46. package/src/types-internal.ts +4 -0
  47. package/tsconfig.json +28 -0
  48. package/webpack.config.js +7 -5
  49. package/.babelrc +0 -12
  50. package/dist/embed-chunks/3d467b2d527e4e5de1b8.js +0 -1
  51. package/dist/embed-chunks/eb491da30c0af7f50004.js +0 -2
  52. package/src/embed.d.ts +0 -58
  53. package/src/embed.js +0 -119
  54. package/src/lib/parse-api-key.js +0 -77
  55. package/src/lib/parse-api-key.test.js +0 -102
  56. package/src/lib/providers/amazon.js +0 -116
  57. /package/dist/embed-chunks/{eb491da30c0af7f50004.js.LICENSE.txt → 4472555f7ea90b733a8e.js.LICENSE.txt} +0 -0
@@ -1,15 +1,22 @@
1
- import 'whatwg-fetch';
2
- import 'promise-polyfill/src/polyfill';
3
- import maplibregl from 'maplibre-gl';
1
+ import maplibregl, { FullscreenControl, GeolocateControl, NavigationControl, Popup, ScaleControl } from 'maplibre-gl';
2
+ import Marker from './geolonia-marker';
4
3
  import { GeoloniaControl } from './controls/geolonia-control';
5
4
  import CustomAttributionControl from './CustomAttributionControl';
6
5
  import GestureHandling from '@geolonia/mbgl-gesture-handling';
7
6
  import parseAtts from './parse-atts';
8
7
 
9
- import SimpleStyle from './simplestyle';
8
+ import { SimpleStyle } from './simplestyle';
10
9
  import SimpleStyleVector from './simplestyle-vector';
11
10
 
12
- import * as util from './util';
11
+ import { getContainer, getOptions, getSessionId, getStyle, handleRestrictedMode, isScrollable, parseControlOption, parseSimpleVector } from './util';
12
+
13
+ import type { MapOptions, PointLike, StyleOptions, StyleSpecification, StyleSwapOptions } from 'maplibre-gl';
14
+
15
+ export type GeoloniaMapOptions = Omit<MapOptions, 'style'> & { interactive?: boolean };
16
+
17
+ type Container = HTMLElement & {
18
+ geoloniaMap: GeoloniaMap;
19
+ };
13
20
 
14
21
  const isCssSelector = (string) => {
15
22
  if (/^https?:\/\//.test(string)) {
@@ -35,8 +42,11 @@ const isCssSelector = (string) => {
35
42
  * @param container
36
43
  */
37
44
  export default class GeoloniaMap extends maplibregl.Map {
38
- constructor(params) {
39
- const container = util.getContainer(params);
45
+ private geoloniaSourcesUrl: URL;
46
+ private __styleExtensionLoadRequired: boolean;
47
+
48
+ constructor(params: string | GeoloniaMapOptions) {
49
+ const container = getContainer(params) as Container | false;
40
50
 
41
51
  if (!container) {
42
52
  if ( typeof params === 'string') {
@@ -55,8 +65,8 @@ export default class GeoloniaMap extends maplibregl.Map {
55
65
  console.warn('[Geolonia] Embed API failed to render the map because the container has no height. Please set the CSS property `height` to the container.');
56
66
  }
57
67
 
58
- const atts = parseAtts(container, params);
59
- const options = util.getOptions(container, params, atts);
68
+ const atts = parseAtts(container, { interactive: typeof params === 'object' ? params.interactive : true });
69
+ const options = getOptions(container, params, atts);
60
70
 
61
71
  // Getting content should be fire just before initialize the map.
62
72
  const content = container.innerHTML.trim();
@@ -77,7 +87,7 @@ export default class GeoloniaMap extends maplibregl.Map {
77
87
  container.appendChild(loading);
78
88
  }
79
89
 
80
- const sessionId = util.getSessionId(40);
90
+ const sessionId = getSessionId(40);
81
91
  const sourcesUrl = new URL(`${atts.apiUrl}/sources`);
82
92
  sourcesUrl.searchParams.set('key', atts.key);
83
93
  sourcesUrl.searchParams.set('sessionId', sessionId);
@@ -140,44 +150,29 @@ export default class GeoloniaMap extends maplibregl.Map {
140
150
 
141
151
  // Note: GeoloniaControl should be placed before another controls.
142
152
  // Because this control should be "very" bottom-left(default) or the attributed position.
143
- const { position: geoloniaControlPosition } = util.parseControlOption(atts.geoloniaControl);
153
+ const { position: geoloniaControlPosition } = parseControlOption(atts.geoloniaControl);
144
154
  map.addControl(new GeoloniaControl(), geoloniaControlPosition);
145
155
 
146
156
  map.addControl(new CustomAttributionControl(), 'bottom-right');
147
157
 
148
- const { enabled: fullscreenControlEnabled, position: fullscreenControlPosition } = util.parseControlOption(atts.fullscreenControl);
158
+ const { enabled: fullscreenControlEnabled, position: fullscreenControlPosition } = parseControlOption(atts.fullscreenControl);
149
159
  if (fullscreenControlEnabled) {
150
- // IE patch for fullscreen mode
151
- if (!container.classList.contains('geolonia')) {
152
- document.onmsfullscreenchange = () => {
153
- const isFullscreen = document.msFullscreenElement === container;
154
- if (isFullscreen) {
155
- map._beforeFullscreenWidth = container.style.width;
156
- map._beforeFullscreenHeight = container.style.height;
157
- container.style.width = '100%';
158
- container.style.height = '100%';
159
- } else {
160
- container.style.width = map._beforeFullscreenWidth;
161
- container.style.height = map._beforeFullscreenHeight;
162
- }
163
- };
164
- }
165
- map.addControl(new window.geolonia.FullscreenControl(), fullscreenControlPosition);
160
+ map.addControl(new FullscreenControl(), fullscreenControlPosition);
166
161
  }
167
162
 
168
- const { enabled: navigationControlEnabled, position: navigationControlPosition } = util.parseControlOption(atts.navigationControl);
163
+ const { enabled: navigationControlEnabled, position: navigationControlPosition } = parseControlOption(atts.navigationControl);
169
164
  if (navigationControlEnabled) {
170
- map.addControl(new window.geolonia.NavigationControl(), navigationControlPosition);
165
+ map.addControl(new NavigationControl(), navigationControlPosition);
171
166
  }
172
167
 
173
- const { enabled: geolocateControlEnabled, position: geolocateControlPosition } = util.parseControlOption(atts.geolocateControl);
168
+ const { enabled: geolocateControlEnabled, position: geolocateControlPosition } = parseControlOption(atts.geolocateControl);
174
169
  if (geolocateControlEnabled) {
175
- map.addControl(new window.geolonia.GeolocateControl(), geolocateControlPosition);
170
+ map.addControl(new GeolocateControl({}), geolocateControlPosition);
176
171
  }
177
172
 
178
- const { enabled: scaleControlEnabled, position: scaleControlPosition } = util.parseControlOption(atts.scaleControl);
173
+ const { enabled: scaleControlEnabled, position: scaleControlPosition } = parseControlOption(atts.scaleControl);
179
174
  if (scaleControlEnabled) {
180
- map.addControl(new window.geolonia.ScaleControl(), scaleControlPosition);
175
+ map.addControl(new ScaleControl({}), scaleControlPosition);
181
176
  }
182
177
 
183
178
  map.on('load', (event) => {
@@ -191,33 +186,33 @@ export default class GeoloniaMap extends maplibregl.Map {
191
186
  }
192
187
  }
193
188
 
194
- if (atts.gestureHandling !== 'off' && util.isScrollable()) {
189
+ if (atts.gestureHandling !== 'off' && isScrollable()) {
195
190
  new GestureHandling({ lang: atts.lang }).addTo(map);
196
191
  }
197
192
 
198
193
  if (atts.lat && atts.lng && atts.marker === 'on') {
199
194
  if (content) {
200
- const popup = new window.geolonia.Popup({ offset: [0, -25] }).setHTML(content);
195
+ const popup = new Popup({ offset: [0, -25] }).setHTML(content);
201
196
  let marker;
202
197
  if (atts.customMarker) {
203
198
  const offset = atts.customMarkerOffset.split(/,/).map((n) => {
204
199
  return Number(n.trim());
205
200
  });
206
- const container = document.querySelector(atts.customMarker);
201
+ const container: HTMLElement = document.querySelector(atts.customMarker);
207
202
  container.style.display = 'block';
208
- marker = new window.geolonia.Marker({
203
+ marker = new Marker({
209
204
  element: container,
210
- offset: offset,
205
+ offset: offset as PointLike,
211
206
  }).setLngLat(options.center).addTo(map).setPopup(popup);
212
207
  } else {
213
- marker = new window.geolonia.Marker({ color: atts.markerColor }).setLngLat(options.center).addTo(map).setPopup(popup);
208
+ marker = new Marker({ color: atts.markerColor }).setLngLat(options.center).addTo(map).setPopup(popup);
214
209
  }
215
210
  if (atts.openPopup === 'on') {
216
211
  marker.togglePopup();
217
212
  }
218
213
  marker.getElement().classList.add('geolonia-clickable-marker');
219
214
  } else {
220
- new window.geolonia.Marker({ color: atts.markerColor }).setLngLat(options.center).addTo(map);
215
+ new Marker({ color: atts.markerColor }).setLngLat(options.center).addTo(map);
221
216
  }
222
217
  }
223
218
  });
@@ -231,7 +226,7 @@ export default class GeoloniaMap extends maplibregl.Map {
231
226
  this.__styleExtensionLoadRequired = false;
232
227
 
233
228
  if (atts.simpleVector) {
234
- const simpleVectorAttributeValue = util.parseSimpleVector(atts.simpleVector);
229
+ const simpleVectorAttributeValue = parseSimpleVector(atts.simpleVector);
235
230
  new SimpleStyleVector(simpleVectorAttributeValue).addTo(map);
236
231
  }
237
232
 
@@ -277,7 +272,7 @@ export default class GeoloniaMap extends maplibregl.Map {
277
272
  error.error &&
278
273
  error.error.status === 402
279
274
  ) {
280
- util.handleRestrictedMode(map);
275
+ handleRestrictedMode(map);
281
276
  }
282
277
  });
283
278
 
@@ -291,7 +286,7 @@ export default class GeoloniaMap extends maplibregl.Map {
291
286
  * @param {string|null} style style identity or `null` when map.remove()
292
287
  * @param {*} options
293
288
  */
294
- setStyle(style, options = {}) {
289
+ setStyle(style: string | StyleSpecification, options: StyleSwapOptions & StyleOptions = {}): this {
295
290
  if (style !== null) {
296
291
  // It can't access `this` because `setStyle()` will be called with `super()`.
297
292
  // So, we need to run `parseAtts()` again(?)
@@ -300,7 +295,7 @@ export default class GeoloniaMap extends maplibregl.Map {
300
295
 
301
296
  // If style is object, it must be passed as it.
302
297
  if (typeof style === 'string') {
303
- style = util.getStyle(style, atts);
298
+ style = getStyle(style, atts);
304
299
  }
305
300
  }
306
301
 
@@ -308,11 +303,13 @@ export default class GeoloniaMap extends maplibregl.Map {
308
303
  this.__styleExtensionLoadRequired = true;
309
304
  // Calls `maplibregl.Map.setStyle()`.
310
305
  super.setStyle.call(this, style, options);
306
+
307
+ return this;
311
308
  }
312
309
 
313
- remove() {
310
+ remove(): void {
314
311
  const container = this.getContainer();
315
312
  super.remove.call(this);
316
- delete container.geoloniaMap;
313
+ delete (container as HTMLElement & { geoloniaMap: GeoloniaMap }).geoloniaMap;
317
314
  }
318
315
  }
@@ -3,7 +3,8 @@
3
3
  import maplibregl from 'maplibre-gl';
4
4
  import tinycolor from 'tinycolor2';
5
5
  import markerSVG from './marker.svg';
6
- import * as util from './util';
6
+ import { handleMarkerOptions } from './util';
7
+ import type { MarkerOptions } from 'maplibre-gl';
7
8
 
8
9
  /**
9
10
  * Geolonia default marker
@@ -11,8 +12,8 @@ import * as util from './util';
11
12
  * @param container
12
13
  */
13
14
  export default class GeoloniaMarker extends maplibregl.Marker {
14
- constructor(options = {}, legacyOptions = {}) {
15
- options = util.handleMarkerOptions(options, legacyOptions);
15
+ constructor(options: MarkerOptions = {}, legacyOptions: MarkerOptions = {}) {
16
+ options = handleMarkerOptions(options, legacyOptions);
16
17
 
17
18
  if (!options || !options.element) {
18
19
  const markerElement = document.createElement('div');
@@ -20,8 +21,8 @@ export default class GeoloniaMarker extends maplibregl.Marker {
20
21
  markerElement.innerHTML = markerSVG;
21
22
 
22
23
  // Following shoud follow the dimention of marker.svg 52:67.
23
- markerElement.style.margin = 0;
24
- markerElement.style.padding = 0;
24
+ markerElement.style.margin = '0';
25
+ markerElement.style.padding = '0';
25
26
  markerElement.style.width = '26px';
26
27
  markerElement.style.height = '34px';
27
28
 
@@ -32,12 +33,12 @@ export default class GeoloniaMarker extends maplibregl.Marker {
32
33
  options.element = markerElement;
33
34
 
34
35
  if (options.color) {
35
- markerElement.querySelector('.left').style.fill = options.color;
36
- markerElement.querySelector('.right').style.fill = tinycolor(options.color).darken().toString();
36
+ (markerElement.querySelector('.left') as HTMLElement).style.fill = options.color;
37
+ (markerElement.querySelector('.right') as HTMLElement).style.fill = tinycolor(options.color).darken().toString();
37
38
  } else {
38
39
  const defaultColor = '#E4402F';
39
- markerElement.querySelector('.left').style.fill = defaultColor;
40
- markerElement.querySelector('.right').style.fill = tinycolor(defaultColor).darken().toString();
40
+ (markerElement.querySelector('.left') as HTMLElement).style.fill = defaultColor;
41
+ (markerElement.querySelector('.right') as HTMLElement).style.fill = tinycolor(defaultColor).darken().toString();
41
42
  }
42
43
 
43
44
  options.offset = [0, -15];
@@ -0,0 +1,66 @@
1
+ import { keyring } from './keyring';
2
+ import { JSDOM } from 'jsdom';
3
+ import assert from 'assert';
4
+
5
+ describe('parse api key from dom', () => {
6
+ beforeEach(() => {
7
+ keyring.reset();
8
+ });
9
+ after(() => {
10
+ keyring.reset();
11
+ });
12
+
13
+ it('should parse with geolonia flag', () => {
14
+ const { document: mocDocument } = new JSDOM(`<html><body>
15
+ <script src="https://external.example.com/?geolonia-api-key=abc"></script>
16
+ </body></html>`).window;
17
+
18
+ keyring.parse(mocDocument);
19
+ assert.deepEqual('abc', keyring.apiKey);
20
+ assert.deepEqual('dev', keyring.stage);
21
+ });
22
+
23
+ it('should parse with geolonia flag', () => {
24
+ const { document: mocDocument } = new JSDOM(`<html><body>
25
+ <script src="https://external.example.com/jquery.js"></script>
26
+ <script src="https://external.example.com/?geolonia-api-key=def"></script>
27
+ </body></html>`).window;
28
+
29
+ keyring.parse(mocDocument);
30
+ assert.deepEqual('def', keyring.apiKey);
31
+ assert.deepEqual('dev', keyring.stage);
32
+ });
33
+
34
+ it('should be "YOUR-API-KEY" and "dev"', () => {
35
+ const { document: mocDocument } = new JSDOM(`<html><body>
36
+ <script src="https://external.example.com/jquery.js"></script>
37
+ <script type="text/javascript" src="https://api.geolonia.com/dev/embed?geolonia-api-key=YOUR-API-KEY"></script>
38
+ </body></html>`).window;
39
+
40
+ keyring.parse(mocDocument);
41
+ assert.deepEqual('YOUR-API-KEY', keyring.apiKey);
42
+ assert.deepEqual('dev', keyring.stage);
43
+ });
44
+
45
+ it('should be "YOUR-API-KEY" and "v1"', () => {
46
+ const { document: mocDocument } = new JSDOM(`<html><body>
47
+ <script src="https://external.example.com/jquery.js"></script>
48
+ <script type="text/javascript" src="https://api.geolonia.com/v1/embed?geolonia-api-key=YOUR-API-KEY"></script>
49
+ </body></html>`).window;
50
+
51
+ keyring.parse(mocDocument);
52
+ assert.deepEqual('YOUR-API-KEY', keyring.apiKey);
53
+ assert.deepEqual('v1', keyring.stage);
54
+ });
55
+
56
+ it('should be "YOUR-API-KEY" and "v123.4"', () => {
57
+ const { document: mocDocument } = new JSDOM(`<html><body>
58
+ <script src="https://external.example.com/jquery.js"></script>
59
+ <script type="text/javascript" src="https://api.geolonia.com/v123.4/embed?geolonia-api-key=YOUR-API-KEY"></script>
60
+ </body></html>`).window;
61
+
62
+ keyring.parse(mocDocument);
63
+ assert.deepEqual('YOUR-API-KEY', keyring.apiKey);
64
+ assert.deepEqual('v123.4', keyring.stage);
65
+ });
66
+ });
@@ -0,0 +1,86 @@
1
+ class Keyring {
2
+ #apiKey: string;
3
+ #stage: string;
4
+
5
+ /**
6
+ * @returns API key
7
+ */
8
+ get apiKey() {
9
+ if (!this.#apiKey) {
10
+ this.parse();
11
+ }
12
+ return this.#apiKey;
13
+ }
14
+ /**
15
+ * @param key - API Key to set
16
+ */
17
+ set apiKey(key: string) {
18
+ this.#apiKey = key;
19
+ }
20
+ /**
21
+ * @returns Stage name
22
+ */
23
+ get stage() {
24
+ if (!this.#stage) {
25
+ this.parse();
26
+ }
27
+ return this.#stage;
28
+ }
29
+ /**
30
+ * @param stage - Stage name to set
31
+ */
32
+ set stage(stage: string) {
33
+ this.#stage = stage;
34
+ }
35
+
36
+ /**
37
+ * Parses the API key and stage from the URL of the current script tag.
38
+ *
39
+ * @param {Document} document
40
+ */
41
+ public parse(doc: Document = document) {
42
+ const scripts: HTMLScriptElement[] | HTMLCollectionOf<HTMLScriptElement> = doc.currentScript ?
43
+ [doc.currentScript as HTMLScriptElement]
44
+ :
45
+ doc.getElementsByTagName('script');
46
+
47
+ for (const script of scripts) {
48
+ const url = new URL(
49
+ (
50
+ script.src.startsWith('https://') ||
51
+ script.src.startsWith('http://') ||
52
+ script.src.startsWith('//')
53
+ ) ? script.src : `https://${location.host}/${script.src}`);
54
+ const apiKey = url.searchParams.get('geolonia-api-key');
55
+
56
+ if (apiKey) {
57
+ this.#apiKey = apiKey;
58
+
59
+ const res = url.pathname.match( /^\/(v[0-9.]+)\/embed/ );
60
+ if (res && res[1]) {
61
+ this.#stage = res[1];
62
+ }
63
+
64
+ break;
65
+ }
66
+ }
67
+
68
+ if (!this.#stage) {
69
+ this.#stage = 'dev';
70
+ }
71
+
72
+ if (!this.#apiKey) {
73
+ throw new Error('Cannot load API key.');
74
+ }
75
+ }
76
+
77
+ /** Reset API key and stage for testing purpose */
78
+ public reset() {
79
+ this.#apiKey = undefined;
80
+ this.#stage = undefined;
81
+ }
82
+ }
83
+
84
+ const keyring = new Keyring(); // Singleton
85
+
86
+ export { keyring };
@@ -23,7 +23,7 @@ export class DOM {
23
23
  return props[0];
24
24
  }
25
25
 
26
- static create(tagName, className, container) {
26
+ static create(tagName, className?, container?) {
27
27
  const el = window.document.createElement(tagName);
28
28
  if (className !== undefined) el.className = className;
29
29
  if (container) container.appendChild(el);
@@ -1,6 +1,7 @@
1
1
  import parseAtts from './parse-atts';
2
2
  import assert from 'assert';
3
3
  import { JSDOM } from 'jsdom';
4
+ import { keyring } from './keyring';
4
5
 
5
6
  describe('tests for parse Attributes', () => {
6
7
 
@@ -8,9 +9,11 @@ describe('tests for parse Attributes', () => {
8
9
 
9
10
  beforeEach(() => {
10
11
  global.window = {
11
- geolonia: { config: {} },
12
+ // @ts-ignore forcefully assigning values to readonly properties
12
13
  navigator: { languages: ['ja'] },
13
14
  };
15
+
16
+ keyring.reset();
14
17
  });
15
18
 
16
19
  it('should parse attribute', () => {
@@ -56,5 +59,6 @@ describe('tests for parse Attributes', () => {
56
59
 
57
60
  afterEach(() => {
58
61
  global.window = prevWindow;
62
+ keyring.reset();
59
63
  });
60
64
  });
@@ -1,25 +1,30 @@
1
1
  'use strict';
2
2
 
3
- import { parseApiKey } from './parse-api-key';
4
- import * as util from './util';
3
+ import { keyring } from './keyring';
4
+ import { getLang } from './util';
5
+ import type { EmbedAttributes } from '../embed';
5
6
 
6
- export default (container, params = {}) => {
7
+ type ParseAttsParams = {
8
+ interactive?: boolean;
9
+ };
10
+
11
+ export default (container, params: ParseAttsParams = {}): EmbedAttributes => {
7
12
  if (!container.dataset) {
8
13
  container.dataset = {};
9
14
  }
10
15
 
11
16
  let lang = 'auto';
12
17
  if (container.dataset.lang && container.dataset.lang === 'auto') {
13
- lang = util.getLang();
18
+ lang = getLang();
14
19
  } else if (container.dataset.lang && container.dataset.lang === 'ja') {
15
20
  lang = 'ja';
16
21
  } else if (container.dataset.lang && container.dataset.lang !== 'ja') {
17
22
  lang = 'en';
18
23
  } else {
19
- lang = util.getLang();
24
+ lang = getLang();
20
25
  }
21
26
 
22
- const { key, stage } = parseApiKey(container);
27
+ keyring.parse(container);
23
28
 
24
29
  return {
25
30
  lat: 0,
@@ -46,9 +51,9 @@ export default (container, params = {}) => {
46
51
  style: 'geolonia/basic-v1',
47
52
  lang: lang,
48
53
  plugin: 'off',
49
- key: key,
50
- apiUrl: `https://api.geolonia.com/${stage}`,
51
- stage,
54
+ key: keyring.apiKey,
55
+ apiUrl: `https://api.geolonia.com/${keyring.stage}`,
56
+ stage: keyring.stage,
52
57
  loader: 'on',
53
58
  minZoom: '',
54
59
  maxZoom: 20,
@@ -0,0 +1,100 @@
1
+ import maplibregl from 'maplibre-gl';
2
+ import 'maplibre-gl/dist/maplibre-gl.css';
3
+ import '../style.css';
4
+ import GeoloniaMap from './geolonia-map';
5
+ import { checkPermission } from './util';
6
+ import parseAtts from './parse-atts';
7
+ import { keyring } from './keyring';
8
+ import { Protocol } from 'pmtiles';
9
+
10
+ const plugins = [];
11
+
12
+ export const renderGeoloniaMap = () => {
13
+ const protocol = new Protocol();
14
+ maplibregl.addProtocol('pmtiles', protocol.tile);
15
+
16
+ if ( checkPermission() ) {
17
+ let isDOMContentLoaded = false;
18
+ const alreadyRenderedMaps = [];
19
+ const isRemoved = Symbol('map-is-removed');
20
+
21
+ keyring.parse();
22
+
23
+ /**
24
+ *
25
+ * @param {HTMLElement} target
26
+ */
27
+ const renderSingleMap = (target) => {
28
+ const map = new GeoloniaMap(target);
29
+
30
+ // detect if the map removed manually
31
+ map.on('remove', () => {
32
+ map[isRemoved] = true;
33
+ });
34
+
35
+ // remove map instance automatically if the container removed.
36
+ // prevent memory leak
37
+ const observer = new MutationObserver((mutationRecords) => {
38
+ const removed = mutationRecords.some((record) => [...record.removedNodes].some((node) => node === target));
39
+ if (removed && !map[isRemoved]) {
40
+ map.remove();
41
+ }
42
+ });
43
+ observer.observe(target.parentNode, { childList: true });
44
+
45
+ // plugin
46
+ const atts = parseAtts(target);
47
+ if (isDOMContentLoaded && !map[isRemoved]) {
48
+ plugins.forEach((plugin) => plugin(map, target, atts));
49
+ } else {
50
+ alreadyRenderedMaps.push({ map, target: target, atts });
51
+ }
52
+ };
53
+
54
+ document.addEventListener('DOMContentLoaded', () => {
55
+ isDOMContentLoaded = true;
56
+ alreadyRenderedMaps.forEach(({ map, target, atts }) => {
57
+ if (!map[isRemoved]) {
58
+ plugins.forEach((plugin) => plugin(map, target, atts));
59
+ }
60
+ });
61
+ // clear
62
+ alreadyRenderedMaps.splice(0, alreadyRenderedMaps.length);
63
+ });
64
+
65
+ const observer = new IntersectionObserver((entries) => {
66
+ entries.forEach((item) => {
67
+ if (!item.isIntersecting) {
68
+ return;
69
+ }
70
+ renderSingleMap(item.target);
71
+ observer.unobserve(item.target);
72
+ });
73
+ });
74
+
75
+ const containers = document.querySelectorAll('.geolonia[data-lazy-loading="off"]');
76
+ const lazyContainers = document.querySelectorAll('.geolonia:not([data-lazy-loading="off"])');
77
+
78
+ // This is required for correct initialization! Don't delete!
79
+ if (!keyring.apiKey) {
80
+ console.error('[Geolonia] Missing API key.') // eslint-disable-line
81
+ }
82
+
83
+ // render Map immediately
84
+ for (let i = 0; i < containers.length; i++) {
85
+ renderSingleMap(containers[i]);
86
+ }
87
+
88
+ // set intersection observer
89
+ for (let i = 0; i < lazyContainers.length; i++) {
90
+ observer.observe(lazyContainers[i]);
91
+ }
92
+ } else {
93
+ console.error( '[Geolonia] We are very sorry, but we can\'t display our map in iframe.' ) // eslint-disable-line
94
+ }
95
+ };
96
+
97
+ export const registerPlugin = (plugin: (map: GeoloniaMap, target: HTMLElement, atts) => void): void => {
98
+ plugins.push(plugin);
99
+ return void 0;
100
+ };
@@ -10,9 +10,10 @@ const backgroundColor = 'rgba(255, 0, 0, 0.4)';
10
10
  const strokeColor = '#FFFFFF';
11
11
 
12
12
  class SimpleStyleVector {
13
- constructor(url) {
13
+ private sourceName: string;
14
+
15
+ constructor(private url) {
14
16
  this.sourceName = 'vt-geolonia-simple-style';
15
- this.url = url;
16
17
  }
17
18
 
18
19
  addTo(map) {
@@ -203,7 +204,7 @@ class SimpleStyleVector {
203
204
 
204
205
  async setPopup(map, source) {
205
206
  map.on('click', source, async (e) => {
206
- const center = turfCenter(e.features[0]).geometry.coordinates;
207
+ const center: [ number, number ] = turfCenter(e.features[0]).geometry.coordinates as [ number, number ];
207
208
  const description = e.features[0].properties.description;
208
209
 
209
210
  if (description) {