@geolonia/embed 3.1.2 → 3.1.4

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@geolonia/embed",
3
- "version": "3.1.2",
3
+ "version": "3.1.4",
4
4
  "description": "Geolonia embed JS API",
5
5
  "main": "dist/embed.js",
6
6
  "types": "src/embed.d.ts",
package/src/embed.d.ts CHANGED
@@ -1,7 +1,10 @@
1
1
  import * as maplibregl from 'maplibre-gl';
2
2
  import SimpleStyle from './lib/simplestyle'
3
3
 
4
- export class Map extends maplibregl.Map {}
4
+ export type GeoloniaMapOptions = Omit<maplibregl.MapOptions, 'style'> & { interactive?: boolean }
5
+ export class Map extends maplibregl.Map {
6
+ constructor(geoloniaMapOptions: string | GeoloniaMapOptions);
7
+ }
5
8
  export class Marker extends maplibregl.Marker {}
6
9
  export class Popup extends maplibregl.Popup {}
7
10
 
package/src/embed.js CHANGED
@@ -10,7 +10,7 @@ import GeoloniaMarker from './lib/geolonia-marker';
10
10
  import { AmazonLocationServiceMapProvider } from './lib/providers/amazon';
11
11
  import * as util from './lib/util';
12
12
  import parseAtts from './lib/parse-atts';
13
- import parseApiKey from './lib/parse-api-key';
13
+ import { parseApiKey } from './lib/parse-api-key';
14
14
  import pkg from '../package.json';
15
15
  import SimpleStyle from './lib/simplestyle';
16
16
 
@@ -20,6 +20,9 @@ if ( util.checkPermission() ) {
20
20
  const plugins = [];
21
21
  const isRemoved = Symbol('map-is-removed');
22
22
 
23
+ // Create the initial window.geolonia object if it doesn't exist.
24
+ parseApiKey(document);
25
+
23
26
  /**
24
27
  *
25
28
  * @param {HTMLElement} target
@@ -78,12 +81,12 @@ if ( util.checkPermission() ) {
78
81
  window.geolonia =
79
82
  window.maplibregl =
80
83
  window.mapboxgl = // Embed API backward compatibility
81
- maplibregl;
84
+ Object.assign(window.geolonia, maplibregl);
82
85
 
83
86
  // This is required for correct initialization! Don't delete!
84
87
  const { key } = parseApiKey(document);
85
88
  if (key === 'no-api-key') {
86
- console.error('Missing API key.') // eslint-disable-line
89
+ console.error('[Geolonia] Missing API key.') // eslint-disable-line
87
90
  }
88
91
 
89
92
  window.geolonia.Map = GeoloniaMap;
@@ -43,7 +43,7 @@ export default class GeoloniaMap extends maplibregl.Map {
43
43
 
44
44
  if (container.clientHeight === 0) {
45
45
  // eslint-disable-next-line no-console
46
- 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.');
46
+ 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.');
47
47
  }
48
48
 
49
49
  const atts = parseAtts(container, params);
@@ -1,18 +1,26 @@
1
1
  import urlParse from 'url-parse';
2
2
 
3
- export default (document) => {
3
+ function getParsedApiKey() {
4
+ const geolonia = window.geolonia || {};
5
+ if (geolonia._apiKey && geolonia._stage) {
6
+ return {
7
+ key: geolonia._apiKey,
8
+ stage: geolonia._stage,
9
+ };
10
+ }
11
+ return null;
12
+ }
13
+
14
+ function initApiKey(document) {
4
15
  if (typeof window.geolonia === 'undefined') {
5
16
  window.geolonia = {};
6
17
  }
7
18
 
8
- if (window.geolonia._apiKey && window.geolonia._stage) {
9
- return {
10
- key: window.geolonia._apiKey,
11
- stage: window.geolonia._stage,
12
- };
13
- }
19
+ const scripts = document.currentScript ?
20
+ [document.currentScript]
21
+ :
22
+ document.getElementsByTagName('script');
14
23
 
15
- const scripts = document.getElementsByTagName('script');
16
24
  const params = {
17
25
  key: 'no-api-key',
18
26
  stage: 'dev',
@@ -34,8 +42,36 @@ export default (document) => {
34
42
  }
35
43
  }
36
44
 
37
- window.geolonia._apiKey = params.key;
38
- window.geolonia._stage = params.stage;
45
+ window.geolonia._apiKey ||= params.key;
46
+ window.geolonia._stage ||= params.stage;
47
+
48
+ return {
49
+ key: window.geolonia._apiKey,
50
+ stage: window.geolonia._stage,
51
+ };
52
+ }
53
+
54
+ /**
55
+ * Parses the API key and stage from the initial window.geolonia object, or from
56
+ * the URL of the current script tag.
57
+ *
58
+ * This function will create `window.geolonia` if it doesn't already exist.
59
+ * If window.geolonia._apiKey and window.geolonia._stage don't exist,
60
+ * this function will set them to the detected values.
61
+ * If window.geolonia._apiKey and window.geolonia._stage do exist,
62
+ * this function will return them immediately.
63
+ * If either window.geolonia._apiKey or window.geolonia._stage exist,
64
+ * that value will override the detected value.
65
+ *
66
+ * The default values are: {key: 'no-api-key', stage: 'dev'}
67
+ * @param {Document} document
68
+ * @returns { key: string, stage: string }
69
+ */
70
+ export function parseApiKey(document) {
71
+ const apiKey = getParsedApiKey();
72
+ if (apiKey) {
73
+ return apiKey;
74
+ }
39
75
 
40
- return params;
41
- };
76
+ return initApiKey(document);
77
+ }
@@ -1,8 +1,12 @@
1
- import parseApiKey from './parse-api-key';
1
+ import { parseApiKey } from './parse-api-key';
2
2
  import { JSDOM } from 'jsdom';
3
3
  import assert from 'assert';
4
4
 
5
5
  describe('parse api key from dom', () => {
6
+ beforeEach(() => {
7
+ delete window.geolonia;
8
+ });
9
+
6
10
  it('should parse with geolonia flag', () => {
7
11
  const { document: mocDocument } = new JSDOM(`<html><body>
8
12
  <script src="https://external.example.com/?geolonia-api-key=abc"></script>
@@ -15,6 +19,35 @@ describe('parse api key from dom', () => {
15
19
  assert.deepEqual('dev', params.stage);
16
20
  });
17
21
 
22
+ it('should override API key and stage with pre-initialized window.geolonia', () => {
23
+ const { document: mocDocument } = new JSDOM(`<html><body>
24
+ <script src="https://external.example.com/?geolonia-api-key=abc"></script>
25
+ </body></html>`).window;
26
+
27
+ window.geolonia = {
28
+ _stage: 'testStage',
29
+ _apiKey: 'testApiKey',
30
+ };
31
+
32
+ const params = parseApiKey(mocDocument);
33
+ assert.deepEqual('testApiKey', params.key);
34
+ assert.deepEqual('testStage', params.stage);
35
+ });
36
+
37
+ it('should override only stage with pre-initialized window.geolonia', () => {
38
+ const { document: mocDocument } = new JSDOM(`<html><body>
39
+ <script src="https://external.example.com/?geolonia-api-key=abc"></script>
40
+ </body></html>`).window;
41
+
42
+ window.geolonia = {
43
+ _stage: 'testStage',
44
+ };
45
+
46
+ const params = parseApiKey(mocDocument);
47
+ assert.deepEqual('abc', params.key);
48
+ assert.deepEqual('testStage', params.stage);
49
+ });
50
+
18
51
  it('should parse with geolonia flag', () => {
19
52
  const { document: mocDocument } = new JSDOM(`<html><body>
20
53
  <script src="https://external.example.com/jquery.js"></script>
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- import parseApiKey from './parse-api-key';
3
+ import { parseApiKey } from './parse-api-key';
4
4
  import * as util from './util';
5
5
 
6
6
  export default (container, params = {}) => {
@@ -235,8 +235,15 @@ class SimpleStyle {
235
235
  ['==', 'large', ['get', 'marker-size']], ['literal', [0, 1.2]],
236
236
  ['literal', [0, 0.8]],
237
237
  ],
238
- 'text-allow-overlap': false,
238
+ 'text-allow-overlap': true,
239
239
  'icon-allow-overlap': true,
240
+ 'text-variable-anchor': ['top', 'bottom', 'left', 'right'],
241
+ 'text-radial-offset': [
242
+ 'case',
243
+ ['==', 'small', ['get', 'marker-size']], 0.6,
244
+ ['==', 'large', ['get', 'marker-size']], 1.3,
245
+ 0.8,
246
+ ],
240
247
  },
241
248
  });
242
249
 
@@ -341,7 +348,7 @@ class SimpleStyle {
341
348
 
342
349
  } catch (error) {
343
350
 
344
- console.error('Failed to load GeoJSON:', error); // eslint-disable-line no-console
351
+ console.error('[Geolonia] Failed to load GeoJSON:', error); // eslint-disable-line no-console
345
352
  }
346
353
 
347
354
  };
@@ -84,6 +84,7 @@ describe('Tests for simpleStyle()', () => {
84
84
  assert.deepEqual(8, map.layers.length);
85
85
  assert.deepEqual(true, map.bounds);
86
86
  assert.deepEqual(true, map.layers.find((layer) => layer.id === 'geolonia-simple-style-symbol-points').layout['icon-allow-overlap']);
87
+ assert.deepEqual(true, map.layers.find((layer) => layer.id === 'geolonia-simple-style-symbol-points').layout['text-allow-overlap']);
87
88
  });
88
89
 
89
90
  it('should has sources and layers as expected with custom IDs', async () => {
package/src/lib/util.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- import parseApiKey from './parse-api-key';
3
+ import { parseApiKey } from './parse-api-key';
4
4
 
5
5
  /**
6
6
  *
@@ -14,7 +14,7 @@ export function isURL(str) {
14
14
  try {
15
15
  return new URL(str, location.href).href;
16
16
  } catch (error) {
17
- console.error(error) // eslint-disable-line
17
+ console.error('[Geolonia]', error) // eslint-disable-line
18
18
  return false;
19
19
  }
20
20
  }
@@ -183,6 +183,8 @@ export function getStyle(style, atts) {
183
183
  }
184
184
  }
185
185
 
186
+ // params are the parameters that have been passed to new geolonia.Map(params)
187
+ // atts are the data-XYZ attributes that are on the container
186
188
  export function getOptions(container, params, atts) {
187
189
  if (params.container) {
188
190
  delete params.container; // Don't overwrite container.