@geolonia/embed 2.0.0 → 2.0.2

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": "2.0.0",
3
+ "version": "2.0.2",
4
4
  "description": "Geolonia embed JS API",
5
5
  "main": "dist/embed.js",
6
6
  "types": "src/embed.d.ts",
@@ -5,6 +5,9 @@ import GeoloniaControl from '@geolonia/mbgl-geolonia-control';
5
5
  import GestureHandling from '@geolonia/mbgl-gesture-handling';
6
6
  import parseAtts from './parse-atts';
7
7
 
8
+ import SimpleStyle from './simplestyle';
9
+ import SimpleStyleVector from './simplestyle-vector';
10
+
8
11
  import * as util from './util';
9
12
 
10
13
  const isCssSelector = (string) => {
@@ -200,28 +203,27 @@ export default class GeoloniaMap extends maplibregl.Map {
200
203
 
201
204
  if (atts.simpleVector) {
202
205
  const simpleVectorAttributeValue = util.parseSimpleVector(atts.simpleVector);
203
- const { default: SimpleStyleVector } = await import('./simplestyle-vector');
204
206
  new SimpleStyleVector(simpleVectorAttributeValue).addTo(map);
205
207
  }
206
208
 
207
209
  if (atts.geojson) {
208
210
  const el = isCssSelector(atts.geojson);
209
- const { default: SimpleStyle } = await import('./simplestyle');
211
+ let json;
210
212
  if (el) {
211
- const json = JSON.parse(el.textContent);
212
- new SimpleStyle(json, {
213
- cluster: (atts.cluster === 'on'),
214
- clusterColor: atts.clusterColor,
215
- }).addTo(map);
213
+ json = JSON.parse(el.textContent);
216
214
  } else {
217
- fetch(atts.geojson).then((response) => {
218
- return response.json();
219
- }).then((json) => {
220
- new SimpleStyle(json, {
221
- cluster: (atts.cluster === 'on'),
222
- clusterColor: atts.clusterColor,
223
- }).addTo(map);
224
- });
215
+ const response = await fetch(atts.geojson);
216
+ json = await response.json();
217
+ }
218
+
219
+ const ss = new SimpleStyle(json, {
220
+ cluster: (atts.cluster === 'on'),
221
+ clusterColor: atts.clusterColor,
222
+ });
223
+ ss.addTo(map);
224
+
225
+ if (!container.dataset || (!container.dataset.lng && !container.dataset.lat)) {
226
+ ss.fitBounds();
225
227
  }
226
228
  }
227
229
 
@@ -2,7 +2,6 @@
2
2
 
3
3
  import maplibregl from 'maplibre-gl';
4
4
  import turfCenter from '@turf/center';
5
- import sanitizeHtml from 'sanitize-html';
6
5
 
7
6
  const textColor = '#000000';
8
7
  const textHaloColor = '#FFFFFF';
@@ -204,7 +203,8 @@ class SimpleStyleVector {
204
203
  this.setPopup(map, 'vt-circle-simple-style-points');
205
204
  }
206
205
 
207
- setPopup(map, source) {
206
+ async setPopup(map, source) {
207
+ const { default: sanitizeHtml } = await import('sanitize-html');
208
208
  map.on('click', source, (e) => {
209
209
  const center = turfCenter(e.features[0]).geometry.coordinates;
210
210
  const description = e.features[0].properties.description;
@@ -3,7 +3,6 @@
3
3
  import maplibregl from 'maplibre-gl';
4
4
  import geojsonExtent from '@mapbox/geojson-extent';
5
5
  import turfCenter from '@turf/center';
6
- import sanitizeHtml from 'sanitize-html';
7
6
 
8
7
  const textColor = '#000000';
9
8
  const textHaloColor = '#FFFFFF';
@@ -11,28 +10,46 @@ const backgroundColor = 'rgba(255, 0, 0, 0.4)';
11
10
  const strokeColor = '#FFFFFF';
12
11
 
13
12
  class SimpleStyle {
14
- constructor(json, options) {
15
- this.json = json;
13
+ constructor(geojson, options) {
14
+ this.geojson = geojson;
16
15
 
17
16
  this.options = {
18
17
  id: 'geolonia-simple-style',
19
18
  cluster: true,
20
- heatmap: false,
19
+ heatmap: false, // TODO: It should support heatmap.
21
20
  clusterColor: '#ff0000',
22
21
  ...options,
23
22
  };
24
23
  }
25
24
 
26
- getLayerId() {
27
- return this.options.id;
25
+ updateData(geojson) {
26
+ const features = geojson.features;
27
+ const polygonandlines = features.filter((feature) => (feature.geometry.type.toLowerCase() !== 'point'));
28
+ const points = features.filter((feature) => (feature.geometry.type.toLowerCase() === 'point'));
29
+
30
+ this.map.getSource(this.options.id).setData({
31
+ 'type': 'FeatureCollection',
32
+ 'features': polygonandlines,
33
+ });
34
+
35
+ this.map.getSource(`${this.options.id}-points`).setData({
36
+ 'type': 'FeatureCollection',
37
+ 'features': points,
38
+ });
39
+
40
+ this.geojson = geojson;
41
+
42
+ return this;
28
43
  }
29
44
 
30
45
  addTo(map) {
31
- const features = this.json.features;
46
+ this.map = map;
47
+
48
+ const features = this.geojson.features;
32
49
  const polygonandlines = features.filter((feature) => (feature.geometry.type.toLowerCase() !== 'point'));
33
50
  const points = features.filter((feature) => (feature.geometry.type.toLowerCase() === 'point'));
34
51
 
35
- map.addSource(this.options.id, {
52
+ this.map.addSource(this.options.id, {
36
53
  type: 'geojson',
37
54
  data: {
38
55
  type: 'FeatureCollection',
@@ -40,10 +57,10 @@ class SimpleStyle {
40
57
  },
41
58
  });
42
59
 
43
- this.setPolygonGeometries(map);
44
- this.setLineGeometries(map);
60
+ this.setPolygonGeometries();
61
+ this.setLineGeometries();
45
62
 
46
- map.addSource(`${this.options.id}-points`, {
63
+ this.map.addSource(`${this.options.id}-points`, {
47
64
  type: 'geojson',
48
65
  data: {
49
66
  type: 'FeatureCollection',
@@ -54,7 +71,7 @@ class SimpleStyle {
54
71
  clusterRadius: 50,
55
72
  });
56
73
 
57
- map.addLayer({
74
+ this.map.addLayer({
58
75
  id: `${this.options.id}-polygon-symbol`,
59
76
  type: 'symbol',
60
77
  source: this.options.id,
@@ -73,7 +90,7 @@ class SimpleStyle {
73
90
  },
74
91
  });
75
92
 
76
- map.addLayer({
93
+ this.map.addLayer({
77
94
  id: `${this.options.id}-linestring-symbol`,
78
95
  type: 'symbol',
79
96
  source: this.options.id,
@@ -93,27 +110,34 @@ class SimpleStyle {
93
110
  },
94
111
  });
95
112
 
96
- this.setPointGeometries(map);
97
- this.setCluster(map);
113
+ this.setPointGeometries();
114
+ this.setCluster();
98
115
 
99
- const container = map.getContainer();
116
+ return this;
117
+ }
100
118
 
101
- if (!container.dataset || (!container.dataset.lng && !container.dataset.lat)) {
102
- const bounds = geojsonExtent(this.json);
103
- map.fitBounds(bounds, {
104
- duration: 0,
105
- padding: 30,
119
+ fitBounds(options = {}) {
120
+ const _options = {
121
+ duration: 3000,
122
+ padding: 30,
123
+ ...options,
124
+ };
125
+
126
+ const bounds = geojsonExtent(this.geojson);
127
+ if (bounds) {
128
+ window.requestAnimationFrame(() => {
129
+ this.map.fitBounds(bounds, _options);
106
130
  });
107
131
  }
132
+
133
+ return this;
108
134
  }
109
135
 
110
136
  /**
111
137
  * Set polygon geometries.
112
- *
113
- * @param map
114
138
  */
115
- setPolygonGeometries(map) {
116
- map.addLayer({
139
+ setPolygonGeometries() {
140
+ this.map.addLayer({
117
141
  id: `${this.options.id}-polygon`,
118
142
  type: 'fill',
119
143
  source: this.options.id,
@@ -125,16 +149,14 @@ class SimpleStyle {
125
149
  },
126
150
  });
127
151
 
128
- this.setPopup(map, `${this.options.id}-polygon`);
152
+ this.setPopup(this.map, `${this.options.id}-polygon`);
129
153
  }
130
154
 
131
155
  /**
132
156
  * Set line geometries.
133
- *
134
- * @param map
135
157
  */
136
- setLineGeometries(map) {
137
- map.addLayer({
158
+ setLineGeometries() {
159
+ this.map.addLayer({
138
160
  id: `${this.options.id}-linestring`,
139
161
  type: 'line',
140
162
  source: this.options.id,
@@ -150,17 +172,15 @@ class SimpleStyle {
150
172
  },
151
173
  });
152
174
 
153
- this.setPopup(map, `${this.options.id}-linestring`);
175
+ this.setPopup(this.map, `${this.options.id}-linestring`);
154
176
  }
155
177
 
156
178
  /**
157
179
  * Setup point geometries.
158
- *
159
- * @param map
160
180
  */
161
- setPointGeometries(map) {
162
- map.addLayer({
163
- id: 'circle-simple-style-points',
181
+ setPointGeometries() {
182
+ this.map.addLayer({
183
+ id: `${this.options.id}-circle-points`,
164
184
  type: 'circle',
165
185
  source: `${this.options.id}-points`,
166
186
  filter: ['!', ['has', 'point_count']],
@@ -179,7 +199,7 @@ class SimpleStyle {
179
199
  },
180
200
  });
181
201
 
182
- map.addLayer({
202
+ this.map.addLayer({
183
203
  id: `${this.options.id}-symbol-points`,
184
204
  type: 'symbol',
185
205
  source: `${this.options.id}-points`,
@@ -210,10 +230,11 @@ class SimpleStyle {
210
230
  },
211
231
  });
212
232
 
213
- this.setPopup(map, 'circle-simple-style-points');
233
+ this.setPopup(this.map, `${this.options.id}-circle-points`);
214
234
  }
215
235
 
216
- setPopup(map, source) {
236
+ async setPopup(map, source) {
237
+ const { default: sanitizeHtml } = await import('sanitize-html');
217
238
  map.on('click', source, (e) => {
218
239
  const center = turfCenter(e.features[0]).geometry.coordinates;
219
240
  const description = e.features[0].properties.description;
@@ -236,11 +257,9 @@ class SimpleStyle {
236
257
 
237
258
  /**
238
259
  * Setup cluster markers
239
- *
240
- * @param map
241
260
  */
242
- setCluster(map) {
243
- map.addLayer({
261
+ setCluster() {
262
+ this.map.addLayer({
244
263
  id: `${this.options.id}-clusters`,
245
264
  type: 'circle',
246
265
  source: `${this.options.id}-points`,
@@ -252,7 +271,7 @@ class SimpleStyle {
252
271
  },
253
272
  });
254
273
 
255
- map.addLayer({
274
+ this.map.addLayer({
256
275
  id: `${this.options.id}-cluster-count`,
257
276
  type: 'symbol',
258
277
  source: `${this.options.id}-points`,
@@ -264,26 +283,26 @@ class SimpleStyle {
264
283
  },
265
284
  });
266
285
 
267
- map.on('click', `${this.options.id}-clusters`, (e) => {
268
- const features = map.queryRenderedFeatures(e.point, { layers: [`${this.options.id}-clusters`] });
286
+ this.map.on('click', `${this.options.id}-clusters`, (e) => {
287
+ const features = this.map.queryRenderedFeatures(e.point, { layers: [`${this.options.id}-clusters`] });
269
288
  const clusterId = features[0].properties.cluster_id;
270
- map.getSource('geolonia-simple-style-points').getClusterExpansionZoom(clusterId, (err, zoom) => {
289
+ this.map.getSource(`${this.options.id}-points`).getClusterExpansionZoom(clusterId, (err, zoom) => {
271
290
  if (err)
272
291
  return;
273
292
 
274
- map.easeTo({
293
+ this.map.easeTo({
275
294
  center: features[0].geometry.coordinates,
276
295
  zoom: zoom,
277
296
  });
278
297
  });
279
298
  });
280
299
 
281
- map.on('mouseenter', `${this.options.id}-clusters`, () => {
282
- map.getCanvas().style.cursor = 'pointer';
300
+ this.map.on('mouseenter', `${this.options.id}-clusters`, () => {
301
+ this.map.getCanvas().style.cursor = 'pointer';
283
302
  });
284
303
 
285
- map.on('mouseleave', `${this.options.id}-clusters`, () => {
286
- map.getCanvas().style.cursor = '';
304
+ this.map.on('mouseleave', `${this.options.id}-clusters`, () => {
305
+ this.map.getCanvas().style.cursor = '';
287
306
  });
288
307
  }
289
308
  }
@@ -0,0 +1,134 @@
1
+ 'use strict';
2
+
3
+ import assert from 'assert';
4
+
5
+ window.URL.createObjectURL = () => {}; // To prevent `TypeError: window.URL.createObjectURL is not a function`
6
+ window.requestAnimationFrame = (cb) => cb();
7
+
8
+ class Map {
9
+ constructor(json, options) {
10
+ this.json = json;
11
+ this.options = options;
12
+ this.sources = {};
13
+ this.layers = [];
14
+ this.bounds = false;
15
+ }
16
+
17
+ addSource(id, source) {
18
+ this.sources[id] = source;
19
+ }
20
+
21
+ addLayer(layer) {
22
+ this.layers.push(layer);
23
+ }
24
+
25
+ on() {
26
+
27
+ }
28
+
29
+ getSource(id) {
30
+ class getSource {
31
+ constructor(id, sources) {
32
+ this.id = id;
33
+ this.sources = sources;
34
+ }
35
+ setData(geojson) {
36
+ this.sources[this.id] = {
37
+ type: 'geojson',
38
+ data: geojson,
39
+ };
40
+ }
41
+ }
42
+
43
+ return new getSource(id, this.sources);
44
+ }
45
+
46
+ getContainer() {
47
+ return { dataset: true };
48
+ }
49
+
50
+ // It should not be fired if GeoJSON is empty.
51
+ fitBounds() {
52
+ this.bounds = true;
53
+ }
54
+ }
55
+
56
+ const geojson = {
57
+ 'type': 'FeatureCollection',
58
+ 'features': [
59
+ {
60
+ 'type': 'Feature',
61
+ 'properties': {},
62
+ 'geometry': {
63
+ 'type': 'Point',
64
+ 'coordinates': [
65
+ 139.77012634277344,
66
+ 35.68518697509636,
67
+ ],
68
+ },
69
+ },
70
+ ],
71
+ };
72
+
73
+ describe('Tests for simpleStyle()', () => {
74
+ it('should has sources and layers as expected', async () => {
75
+ const { default: simpleStyle } = await import('./simplestyle');
76
+
77
+ const map = new Map();
78
+ new simpleStyle(geojson).addTo(map).fitBounds();
79
+
80
+ assert.deepEqual([ 'geolonia-simple-style', 'geolonia-simple-style-points' ], Object.keys(map.sources));
81
+ assert.deepEqual(8, map.layers.length);
82
+ assert.deepEqual(true, map.bounds);
83
+ });
84
+
85
+ it('should has sources and layers as expected with custom IDs', async () => {
86
+ const { default: simpleStyle } = await import('./simplestyle');
87
+
88
+ const map = new Map();
89
+ new simpleStyle(geojson, {id: 'hello-world'}).addTo(map).fitBounds();
90
+
91
+ assert.deepEqual([ 'hello-world', 'hello-world-points' ], Object.keys(map.sources));
92
+ assert.deepEqual(8, map.layers.length);
93
+ assert.deepEqual(true, map.bounds);
94
+ });
95
+
96
+ it('should has sources and layers as expected with empty GeoJSON', async () => {
97
+ const { default: simpleStyle } = await import('./simplestyle');
98
+
99
+ const map = new Map();
100
+
101
+ const empty = {
102
+ 'type': 'FeatureCollection',
103
+ 'features': [],
104
+ };
105
+
106
+ new simpleStyle(empty, {id: 'hello-world'}).addTo(map).fitBounds();
107
+
108
+ assert.deepEqual([ 'hello-world', 'hello-world-points' ], Object.keys(map.sources));
109
+ assert.deepEqual(8, map.layers.length);
110
+ assert.deepEqual(false, map.bounds);
111
+ });
112
+
113
+ it('should update GeoJSON', async () => {
114
+ const { default: simpleStyle } = await import('./simplestyle');
115
+
116
+ const map = new Map();
117
+
118
+ const empty = {
119
+ 'type': 'FeatureCollection',
120
+ 'features': [],
121
+ };
122
+
123
+ const ss = new simpleStyle(empty).addTo(map).fitBounds();
124
+
125
+ assert.deepEqual([ 'geolonia-simple-style', 'geolonia-simple-style-points' ], Object.keys(map.sources));
126
+ assert.deepEqual(8, map.layers.length);
127
+ assert.deepEqual(false, map.bounds);
128
+ assert.deepEqual(0, map.sources['geolonia-simple-style-points'].data.features.length);
129
+
130
+ ss.updateData(geojson); // The GeoJSON is not empty.
131
+ assert.deepEqual(false, map.bounds); // `fitBounds()` doesn't fire.
132
+ assert.deepEqual(1, map.sources['geolonia-simple-style-points'].data.features.length);
133
+ });
134
+ });
@@ -1 +0,0 @@
1
- "use strict";(self.webpackChunk_geolonia_embed=self.webpackChunk_geolonia_embed||[]).push([[577],{7577:(e,t,o)=>{o.r(t),o.d(t,{default:()=>y}),o(2526),o(1817),o(9070);var i=o(2739),l=o.n(i),r=o(1235),s=o(8007),a=o.n(s);function n(e,t){for(var o=0;o<t.length;o++){var i=t[o];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}var c="#000000",u="#FFFFFF",g="rgba(255, 0, 0, 0.4)",p="#FFFFFF";const y=function(){function e(t){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.sourceName="vt-geolonia-simple-style",this.url=t}var t,o;return t=e,(o=[{key:"addTo",value:function(e){var t=this,o=e.getContainer();if(!o.dataset||!o.dataset.lng&&!o.dataset.lat){var i=!1;e.on("sourcedata",(function(o){if(o.sourceId===t.sourceName){var l=e.getSource(o.sourceId);!0===(l&&l.loaded())&&(i||(i=!0,e.fitBounds(l.bounds,{duration:0,padding:30})))}}))}e.addSource(this.sourceName,{type:"vector",url:this.url}),this.setPolygonGeometries(e),this.setLineGeometries(e),e.addLayer({id:"vt-geolonia-simple-style-polygon-symbol",type:"symbol",source:this.sourceName,"source-layer":"g-simplestyle-v1",filter:["==","$type","Polygon"],paint:{"text-color":["string",["get","text-color"],c],"text-halo-color":["string",["get","text-halo-color"],u],"text-halo-width":1},layout:{"text-field":["get","title"],"text-font":["Noto Sans Regular"],"text-size":12,"text-max-width":12,"text-allow-overlap":!1}}),e.addLayer({id:"vt-geolonia-simple-style-linestring-symbol",type:"symbol",source:this.sourceName,"source-layer":"g-simplestyle-v1",filter:["==","$type","LineString"],paint:{"text-color":["string",["get","text-color"],c],"text-halo-color":["string",["get","text-halo-color"],u],"text-halo-width":1},layout:{"symbol-placement":"line","text-field":["get","title"],"text-font":["Noto Sans Regular"],"text-size":12,"text-max-width":12,"text-allow-overlap":!1}}),this.setPointGeometries(e)}},{key:"setPolygonGeometries",value:function(e){e.addLayer({id:"vt-geolonia-simple-style-polygon",type:"fill",source:this.sourceName,"source-layer":"g-simplestyle-v1",filter:["==","$type","Polygon"],paint:{"fill-color":["string",["get","fill"],g],"fill-opacity":["number",["get","fill-opacity"],1],"fill-outline-color":["string",["get","stroke"],p]}}),this.setPopup(e,"vt-geolonia-simple-style-polygon")}},{key:"setLineGeometries",value:function(e){e.addLayer({id:"vt-geolonia-simple-style-linestring",type:"line",source:this.sourceName,"source-layer":"g-simplestyle-v1",filter:["==","$type","LineString"],paint:{"line-width":["number",["get","stroke-width"],2],"line-color":["string",["get","stroke"],g],"line-opacity":["number",["get","stroke-opacity"],1]},layout:{"line-cap":"round","line-join":"round"}}),this.setPopup(e,"vt-geolonia-simple-style-linestring")}},{key:"setPointGeometries",value:function(e){e.addLayer({id:"vt-circle-simple-style-points",type:"circle",source:this.sourceName,"source-layer":"g-simplestyle-v1",filter:["==","$type","Point"],paint:{"circle-radius":["case",["==","small",["get","marker-size"]],7,["==","large",["get","marker-size"]],13,9],"circle-color":["string",["get","marker-color"],g],"circle-opacity":["number",["get","fill-opacity"],1],"circle-stroke-width":["number",["get","stroke-width"],1],"circle-stroke-color":["string",["get","stroke"],p],"circle-stroke-opacity":["number",["get","stroke-opacity"],1]}}),e.addLayer({id:"vt-geolonia-simple-style-points",type:"symbol",source:this.sourceName,"source-layer":"g-simplestyle-v1",filter:["==","$type","Point"],paint:{"text-color":["string",["get","text-color"],c],"text-halo-color":["string",["get","text-halo-color"],u],"text-halo-width":1},layout:{"icon-image":["case",["==","large",["get","marker-size"]],["image",["concat",["get","marker-symbol"],"-15"]],["image",["concat",["get","marker-symbol"],"-11"]]],"text-field":["get","title"],"text-font":["Noto Sans Regular"],"text-size":12,"text-anchor":"top","text-max-width":12,"text-offset":["case",["==","small",["get","marker-size"]],["literal",[0,.6]],["==","large",["get","marker-size"]],["literal",[0,1.2]],["literal",[0,.8]]],"text-allow-overlap":!1}}),this.setPopup(e,"vt-circle-simple-style-points")}},{key:"setPopup",value:function(e,t){e.on("click",t,(function(t){var o=(0,r.Z)(t.features[0]).geometry.coordinates,i=t.features[0].properties.description;i&&(new(l().Popup)).setLngLat(o).setHTML(a()(i)).addTo(e)})),e.on("mouseenter",t,(function(t){t.features[0].properties.description&&(e.getCanvas().style.cursor="pointer")})),e.on("mouseleave",t,(function(){e.getCanvas().style.cursor=""}))}}])&&n(t.prototype,o),e}()}}]);