@geolonia/embed 1.17.3 → 1.19.1
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/.github/workflows/build.yml +1 -0
- package/dist/embed.js +4 -4
- package/package.json +1 -1
- package/snapshots/map.png.snapshot +0 -0
- package/src/embed.d.ts +0 -1
- package/src/embed.js +12 -4
- package/src/lib/geolonia-map.js +28 -46
- package/src/lib/parse-atts.js +1 -1
- package/src/lib/parse-atts.test.js +1 -1
- package/src/lib/util.js +20 -3
- package/src/lib/util.test.js +24 -0
- package/src/style.css +7 -0
- package/test/integration.test.js +2 -2
package/package.json
CHANGED
|
Binary file
|
package/src/embed.d.ts
CHANGED
package/src/embed.js
CHANGED
|
@@ -18,6 +18,7 @@ if ( util.checkPermission() ) {
|
|
|
18
18
|
let isDOMContentLoaded = false
|
|
19
19
|
const alreadyRenderedMaps = []
|
|
20
20
|
const plugins = []
|
|
21
|
+
const isRemoved = Symbol('map-is-removed')
|
|
21
22
|
|
|
22
23
|
/**
|
|
23
24
|
*
|
|
@@ -26,9 +27,14 @@ if ( util.checkPermission() ) {
|
|
|
26
27
|
const renderGeoloniaMap = target => {
|
|
27
28
|
const map = new GeoloniaMap(target)
|
|
28
29
|
|
|
30
|
+
// detect if the map removed manually
|
|
31
|
+
map.on('remove', () => {
|
|
32
|
+
map[isRemoved] = true
|
|
33
|
+
})
|
|
34
|
+
|
|
29
35
|
// plugin
|
|
30
36
|
const atts = parseAtts(target)
|
|
31
|
-
if (isDOMContentLoaded) {
|
|
37
|
+
if (isDOMContentLoaded && !map[isRemoved]) {
|
|
32
38
|
plugins.forEach(plugin => plugin(map, target, atts))
|
|
33
39
|
} else {
|
|
34
40
|
alreadyRenderedMaps.push({ map, target: target, atts })
|
|
@@ -37,9 +43,11 @@ if ( util.checkPermission() ) {
|
|
|
37
43
|
|
|
38
44
|
document.addEventListener('DOMContentLoaded', () => {
|
|
39
45
|
isDOMContentLoaded = true
|
|
40
|
-
alreadyRenderedMaps.forEach(({ map, target, atts }) =>
|
|
41
|
-
|
|
42
|
-
|
|
46
|
+
alreadyRenderedMaps.forEach(({ map, target, atts }) => {
|
|
47
|
+
if (!map[isRemoved]) {
|
|
48
|
+
plugins.forEach(plugin => plugin(map, target, atts))
|
|
49
|
+
}
|
|
50
|
+
})
|
|
43
51
|
// clear
|
|
44
52
|
alreadyRenderedMaps.splice(0, alreadyRenderedMaps.length)
|
|
45
53
|
})
|
package/src/lib/geolonia-map.js
CHANGED
|
@@ -63,14 +63,9 @@ export default class GeoloniaMap extends mapboxgl.Map {
|
|
|
63
63
|
|
|
64
64
|
const sessionId = util.getSessionId(40)
|
|
65
65
|
let sourcesUrl = new URL(`${atts.apiUrl}/sources`)
|
|
66
|
-
if (options.baseTilesVersion !== '') {
|
|
67
|
-
sourcesUrl = new URL(`${atts.apiUrl}/sources_v2`)
|
|
68
|
-
sourcesUrl.searchParams.set('ver', options.baseTilesVersion)
|
|
69
|
-
}
|
|
70
66
|
sourcesUrl.searchParams.set('key', atts.key)
|
|
71
67
|
sourcesUrl.searchParams.set('sessionId', sessionId)
|
|
72
68
|
|
|
73
|
-
let __insertRefreshedAuthParams
|
|
74
69
|
// Pass API key and requested tile version to `/sources` (tile json).
|
|
75
70
|
const _transformRequest = options.transformRequest
|
|
76
71
|
options.transformRequest = (url, resourceType) => {
|
|
@@ -79,8 +74,20 @@ export default class GeoloniaMap extends mapboxgl.Map {
|
|
|
79
74
|
url: sourcesUrl.toString(),
|
|
80
75
|
}
|
|
81
76
|
}
|
|
82
|
-
|
|
83
|
-
|
|
77
|
+
|
|
78
|
+
let transformedUrl = url
|
|
79
|
+
if (url.startsWith('geolonia://')) {
|
|
80
|
+
const tilesMatch = url.match(/^geolonia:\/\/tiles\/(?<username>.+)\/(?<customtileId>.+)/)
|
|
81
|
+
if (tilesMatch) {
|
|
82
|
+
transformedUrl = `https://tileserver.geolonia.com/customtiles/${tilesMatch.groups.customtileId}/tiles.json`
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (resourceType === 'Source' && transformedUrl.startsWith('https://tileserver.geolonia.com')) {
|
|
87
|
+
const tileserverSourcesUrl = new URL(transformedUrl)
|
|
88
|
+
if (atts.stage !== 'v1') {
|
|
89
|
+
tileserverSourcesUrl.hostname = `tileserver-${atts.stage}.geolonia.com`
|
|
90
|
+
}
|
|
84
91
|
tileserverSourcesUrl.searchParams.set('sessionId', sessionId)
|
|
85
92
|
tileserverSourcesUrl.searchParams.set('key', atts.key)
|
|
86
93
|
return {
|
|
@@ -91,11 +98,7 @@ export default class GeoloniaMap extends mapboxgl.Map {
|
|
|
91
98
|
let request
|
|
92
99
|
// Additional transformation
|
|
93
100
|
if (typeof _transformRequest === 'function') {
|
|
94
|
-
request = _transformRequest(
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
if (typeof __insertRefreshedAuthParams !== 'undefined') {
|
|
98
|
-
request = __insertRefreshedAuthParams(request, url)
|
|
101
|
+
request = _transformRequest(transformedUrl, resourceType)
|
|
99
102
|
}
|
|
100
103
|
|
|
101
104
|
return request
|
|
@@ -107,22 +110,6 @@ export default class GeoloniaMap extends mapboxgl.Map {
|
|
|
107
110
|
this.geoloniaSourcesUrl = sourcesUrl
|
|
108
111
|
this.__styleExtensionLoadRequired = true
|
|
109
112
|
|
|
110
|
-
// this function requires `this`, which is not set before `super`
|
|
111
|
-
__insertRefreshedAuthParams = (request, url) => {
|
|
112
|
-
if (typeof this._currentAuthParams !== 'undefined') {
|
|
113
|
-
const purl = new URL((request && request.url) || url)
|
|
114
|
-
const q = purl.searchParams
|
|
115
|
-
if (q.get('key') && q.get('expires') && q.get('Policy') && q.get('Key-Pair-Id')) {
|
|
116
|
-
purl.search = this._currentAuthParams
|
|
117
|
-
|
|
118
|
-
return {
|
|
119
|
-
url: purl.toString(),
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
return request
|
|
124
|
-
}
|
|
125
|
-
|
|
126
113
|
// Note: GeoloniaControl should be placed before another controls.
|
|
127
114
|
// Because this control should be "very" bottom-left(default) or the attributed position.
|
|
128
115
|
const { position: geoloniaControlPosition } = util.parseControlOption(atts.geoloniaControl)
|
|
@@ -180,7 +167,7 @@ export default class GeoloniaMap extends mapboxgl.Map {
|
|
|
180
167
|
|
|
181
168
|
if (atts.lat && atts.lng && 'on' === atts.marker) {
|
|
182
169
|
if (content) {
|
|
183
|
-
const popup = new window.geolonia.Popup().setHTML(content)
|
|
170
|
+
const popup = new window.geolonia.Popup({ offset: [0, -25] }).setHTML(content)
|
|
184
171
|
let marker
|
|
185
172
|
if (atts.customMarker) {
|
|
186
173
|
const offset = atts.customMarkerOffset.split(/,/).map(n => {
|
|
@@ -203,11 +190,6 @@ export default class GeoloniaMap extends mapboxgl.Map {
|
|
|
203
190
|
new window.geolonia.Marker({ color: atts.markerColor }).setLngLat(options.center).addTo(map)
|
|
204
191
|
}
|
|
205
192
|
}
|
|
206
|
-
|
|
207
|
-
if (options.baseTilesVersion !== '') {
|
|
208
|
-
// トークンが60分で失効するので毎20分で更新する
|
|
209
|
-
map._tileUrlRefreshIntervalId = setInterval(map.refreshTileUrls, 1800000)
|
|
210
|
-
}
|
|
211
193
|
})
|
|
212
194
|
|
|
213
195
|
map.on('styledata', event => {
|
|
@@ -219,7 +201,8 @@ export default class GeoloniaMap extends mapboxgl.Map {
|
|
|
219
201
|
this.__styleExtensionLoadRequired = false
|
|
220
202
|
|
|
221
203
|
if (atts.simpleVector) {
|
|
222
|
-
|
|
204
|
+
const simpleVectorAttributeValue = util.parseSimpleVector(atts.simpleVector)
|
|
205
|
+
new SimpleStyleVector(simpleVectorAttributeValue).addTo(map)
|
|
223
206
|
}
|
|
224
207
|
|
|
225
208
|
if (atts.geojson) {
|
|
@@ -258,22 +241,21 @@ export default class GeoloniaMap extends mapboxgl.Map {
|
|
|
258
241
|
}
|
|
259
242
|
})
|
|
260
243
|
|
|
244
|
+
// handle Geolonia Server errors
|
|
245
|
+
map.on('error', async error => {
|
|
246
|
+
if (
|
|
247
|
+
error.error &&
|
|
248
|
+
error.error.status === 402
|
|
249
|
+
) {
|
|
250
|
+
util.handleRestrictedMode(map)
|
|
251
|
+
}
|
|
252
|
+
})
|
|
253
|
+
|
|
261
254
|
container.geoloniaMap = map
|
|
262
255
|
|
|
263
256
|
return map
|
|
264
257
|
}
|
|
265
258
|
|
|
266
|
-
async refreshTileUrls() {
|
|
267
|
-
const resp = await fetch(this.geoloniaSourcesUrl)
|
|
268
|
-
if (!resp.ok) {
|
|
269
|
-
// Error
|
|
270
|
-
return
|
|
271
|
-
}
|
|
272
|
-
const sources = await resp.json()
|
|
273
|
-
const url = new URL(sources.tiles[0])
|
|
274
|
-
this._currentAuthParams = url.searchParams
|
|
275
|
-
}
|
|
276
|
-
|
|
277
259
|
/**
|
|
278
260
|
*
|
|
279
261
|
* @param {string|null} style style identity or `null` when map.remove()
|
package/src/lib/parse-atts.js
CHANGED
|
@@ -48,10 +48,10 @@ export default (container, params = {}) => {
|
|
|
48
48
|
plugin: 'off',
|
|
49
49
|
key: key,
|
|
50
50
|
apiUrl: `https://api.geolonia.com/${stage}`,
|
|
51
|
+
stage,
|
|
51
52
|
loader: 'on',
|
|
52
53
|
minZoom: '',
|
|
53
54
|
maxZoom: 20,
|
|
54
|
-
baseTilesVersion: '',
|
|
55
55
|
'3d': '',
|
|
56
56
|
...container.dataset,
|
|
57
57
|
}
|
package/src/lib/util.js
CHANGED
|
@@ -198,7 +198,6 @@ export function getOptions(container, params, atts) {
|
|
|
198
198
|
hash: ('on' === atts.hash),
|
|
199
199
|
localIdeographFontFamily: 'sans-serif',
|
|
200
200
|
attributionControl: true,
|
|
201
|
-
baseTilesVersion: params.baseTilesVersion || atts.baseTilesVersion,
|
|
202
201
|
}
|
|
203
202
|
|
|
204
203
|
if ('' !== atts.minZoom && (0 === Number(atts.minZoom) || Number(atts.minZoom))) {
|
|
@@ -215,7 +214,7 @@ export function getOptions(container, params, atts) {
|
|
|
215
214
|
}
|
|
216
215
|
|
|
217
216
|
/**
|
|
218
|
-
*
|
|
217
|
+
*
|
|
219
218
|
* @param {string} an data-*-control Embed attribute
|
|
220
219
|
* @returns { enabled: bolean, position: 'top-right' | 'bottom-right' | 'bottom-left' | 'top-left' | void }
|
|
221
220
|
*/
|
|
@@ -236,7 +235,7 @@ export function parseControlOption(att) {
|
|
|
236
235
|
let sessionId = ''
|
|
237
236
|
|
|
238
237
|
/**
|
|
239
|
-
*
|
|
238
|
+
*
|
|
240
239
|
* @param {number} digits for session
|
|
241
240
|
* @returns sessionId
|
|
242
241
|
*/
|
|
@@ -253,3 +252,21 @@ export const getSessionId = digit => {
|
|
|
253
252
|
return value
|
|
254
253
|
}
|
|
255
254
|
}
|
|
255
|
+
|
|
256
|
+
export const parseSimpleVector = attributeValue => {
|
|
257
|
+
if (/^(https?|geolonia):\/\//.test(attributeValue)) {
|
|
258
|
+
return attributeValue
|
|
259
|
+
} else {
|
|
260
|
+
return `geolonia://tiles/custom/${attributeValue}`
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export const handleRestrictedMode = map => {
|
|
265
|
+
if (!map._geolonia_restricted_mode_handled) {
|
|
266
|
+
map._geolonia_restricted_mode_handled = true
|
|
267
|
+
const container = map.getContainer()
|
|
268
|
+
map.remove()
|
|
269
|
+
container.innerHTML = ''
|
|
270
|
+
container.classList.add('geolonia__restricted-mode-image-container')
|
|
271
|
+
}
|
|
272
|
+
}
|
package/src/lib/util.test.js
CHANGED
|
@@ -171,4 +171,28 @@ describe('Tests for util.js', () => {
|
|
|
171
171
|
assert.strictEqual(false, enabled)
|
|
172
172
|
assert.strictEqual(void 0, position)
|
|
173
173
|
})
|
|
174
|
+
|
|
175
|
+
it('should parse simple vector value with http.', () => {
|
|
176
|
+
const attributeValue = 'https://example.com/path/to/tile.json'
|
|
177
|
+
assert.strictEqual(
|
|
178
|
+
attributeValue,
|
|
179
|
+
util.parseSimpleVector(attributeValue),
|
|
180
|
+
)
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
it('should parse simple vector value with geolonia schema.', () => {
|
|
184
|
+
const attributeValue = 'geolonia://tiles/username/ct_123'
|
|
185
|
+
assert.strictEqual(
|
|
186
|
+
attributeValue,
|
|
187
|
+
util.parseSimpleVector(attributeValue),
|
|
188
|
+
)
|
|
189
|
+
})
|
|
190
|
+
|
|
191
|
+
it('should parse simple vector value with customtile ID', () => {
|
|
192
|
+
const attributeValue = 'ct_123'
|
|
193
|
+
assert.strictEqual(
|
|
194
|
+
'geolonia://tiles/custom/ct_123',
|
|
195
|
+
util.parseSimpleVector(attributeValue),
|
|
196
|
+
)
|
|
197
|
+
})
|
|
174
198
|
})
|
package/src/style.css
CHANGED
|
@@ -102,3 +102,10 @@
|
|
|
102
102
|
opacity: 0.5;
|
|
103
103
|
}
|
|
104
104
|
}
|
|
105
|
+
|
|
106
|
+
/* Style for restricted mode */
|
|
107
|
+
.geolonia__restricted-mode-image-container {
|
|
108
|
+
background-image: url("https://geolonia.github.io/embed/restricted.jpg");
|
|
109
|
+
background-position: center center;
|
|
110
|
+
background-size: cover;
|
|
111
|
+
}
|
package/test/integration.test.js
CHANGED
|
@@ -35,11 +35,11 @@ describe('Tests for Maps.', () => {
|
|
|
35
35
|
).toString()
|
|
36
36
|
const content = template
|
|
37
37
|
.replace(/%ORIGIN%/g, 'http://127.0.0.1:8080')
|
|
38
|
-
.replace(/%API_KEY%/g, process.env.GEOLONIA_API_KEY)
|
|
38
|
+
.replace(/%API_KEY%/g, process.env.GEOLONIA_API_KEY || 'YOUR-API-KEY')
|
|
39
39
|
await page.goto('https://geolonia.com')
|
|
40
40
|
page.on('pageerror', error => capturedErrors.push(error))
|
|
41
41
|
await page.setContent(content)
|
|
42
|
-
await sleep(
|
|
42
|
+
await sleep(15000)
|
|
43
43
|
})
|
|
44
44
|
|
|
45
45
|
after(async () => {
|