2017-04-10 14:29:57 +02:00
|
|
|
import deref from '@mapbox/mapbox-gl-style-spec/deref'
|
2017-01-13 15:31:28 +01:00
|
|
|
import tokens from '../config/tokens.json'
|
2016-09-09 18:53:57 +02:00
|
|
|
|
2016-12-16 14:49:25 +01:00
|
|
|
// Empty style is always used if no style could be restored or fetched
|
2016-12-26 12:21:41 +01:00
|
|
|
const emptyStyle = ensureStyleValidity({
|
2016-12-16 14:49:25 +01:00
|
|
|
version: 8,
|
|
|
|
sources: {},
|
|
|
|
layers: [],
|
2016-12-20 16:08:49 +01:00
|
|
|
})
|
2016-12-16 14:49:25 +01:00
|
|
|
|
2016-12-22 18:08:42 +01:00
|
|
|
function generateId() {
|
|
|
|
return Math.random().toString(36).substr(2, 9)
|
|
|
|
}
|
|
|
|
|
2016-09-20 16:49:02 +02:00
|
|
|
function ensureHasId(style) {
|
2016-12-20 16:08:49 +01:00
|
|
|
if('id' in style) return style
|
2016-12-22 18:08:42 +01:00
|
|
|
style.id = generateId()
|
2016-12-20 16:08:49 +01:00
|
|
|
return style
|
2016-09-20 16:49:02 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 15:31:08 +01:00
|
|
|
function ensureHasNoInteractive(style) {
|
|
|
|
const changedLayers = style.layers.map(layer => {
|
|
|
|
const changedLayer = { ...layer }
|
|
|
|
delete changedLayer.interactive
|
|
|
|
return changedLayer
|
|
|
|
})
|
|
|
|
|
|
|
|
const nonInteractiveStyle = {
|
|
|
|
...style,
|
|
|
|
layers: changedLayers
|
|
|
|
}
|
|
|
|
return nonInteractiveStyle
|
|
|
|
}
|
|
|
|
|
2016-12-26 12:21:41 +01:00
|
|
|
function ensureHasNoRefs(style) {
|
|
|
|
const derefedStyle = {
|
|
|
|
...style,
|
2017-04-10 14:29:57 +02:00
|
|
|
layers: deref(style.layers)
|
2016-12-26 12:21:41 +01:00
|
|
|
}
|
|
|
|
return derefedStyle
|
|
|
|
}
|
|
|
|
|
|
|
|
function ensureStyleValidity(style) {
|
2017-01-13 15:31:08 +01:00
|
|
|
return ensureHasNoInteractive(ensureHasNoRefs(ensureHasId(style)))
|
2016-09-20 16:49:02 +02:00
|
|
|
}
|
|
|
|
|
2016-12-20 20:21:35 +01:00
|
|
|
function indexOfLayer(layers, layerId) {
|
|
|
|
for (let i = 0; i < layers.length; i++) {
|
|
|
|
if(layers[i].id === layerId) {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2018-08-23 21:52:12 +02:00
|
|
|
function getAccessToken(sourceName, mapStyle, opts) {
|
|
|
|
if(sourceName === "thunderforest_transport" || sourceName === "thunderforest_outdoors") {
|
|
|
|
sourceName = "thunderforest"
|
2018-07-27 17:25:53 +02:00
|
|
|
}
|
|
|
|
|
2017-01-13 15:31:28 +01:00
|
|
|
const metadata = mapStyle.metadata || {}
|
2018-08-23 21:52:12 +02:00
|
|
|
let accessToken = metadata[`maputnik:${sourceName}_access_token`]
|
2018-02-02 16:33:15 +01:00
|
|
|
|
|
|
|
if(opts.allowFallback && !accessToken) {
|
2018-08-23 21:52:12 +02:00
|
|
|
accessToken = tokens[sourceName]
|
2018-02-02 16:33:15 +01:00
|
|
|
}
|
|
|
|
|
2018-04-11 16:53:40 +02:00
|
|
|
return accessToken;
|
|
|
|
}
|
|
|
|
|
2018-08-23 21:52:12 +02:00
|
|
|
function replaceSourceAccessToken(mapStyle, sourceName, opts={}) {
|
|
|
|
const source = mapStyle.sources[sourceName]
|
2018-04-11 16:53:40 +02:00
|
|
|
if(!source) return mapStyle
|
|
|
|
if(!source.hasOwnProperty("url")) return mapStyle
|
|
|
|
|
2018-08-23 21:52:12 +02:00
|
|
|
const accessToken = getAccessToken(sourceName, mapStyle, opts)
|
2018-04-11 16:53:40 +02:00
|
|
|
|
2018-02-02 16:33:15 +01:00
|
|
|
if(!accessToken) {
|
|
|
|
// Early exit.
|
|
|
|
return mapStyle;
|
|
|
|
}
|
|
|
|
|
2017-01-13 15:31:28 +01:00
|
|
|
const changedSources = {
|
|
|
|
...mapStyle.sources,
|
2018-08-23 21:52:12 +02:00
|
|
|
[sourceName]: {
|
2018-04-11 16:53:40 +02:00
|
|
|
...source,
|
|
|
|
url: source.url.replace('{key}', accessToken)
|
2017-01-13 15:31:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
const changedStyle = {
|
|
|
|
...mapStyle,
|
|
|
|
sources: changedSources
|
|
|
|
}
|
|
|
|
return changedStyle
|
|
|
|
}
|
|
|
|
|
2018-04-11 16:53:40 +02:00
|
|
|
function replaceAccessTokens(mapStyle, opts={}) {
|
2018-08-23 21:52:12 +02:00
|
|
|
let changedStyle = mapStyle
|
2018-04-11 16:53:40 +02:00
|
|
|
|
2018-08-23 21:52:12 +02:00
|
|
|
Object.keys(mapStyle.sources).forEach((sourceName) => {
|
|
|
|
changedStyle = replaceSourceAccessToken(changedStyle, sourceName, opts);
|
2018-04-11 16:53:40 +02:00
|
|
|
})
|
|
|
|
|
2019-04-06 12:00:48 +02:00
|
|
|
if (mapStyle.glyphs && (mapStyle.glyphs.match(/\.tilehosting\.com/) || mapStyle.glyphs.match(/\.maptiler\.com/))) {
|
2018-09-22 14:04:12 +02:00
|
|
|
const newAccessToken = getAccessToken("openmaptiles", mapStyle, opts);
|
|
|
|
if (newAccessToken) {
|
|
|
|
changedStyle = {
|
|
|
|
...changedStyle,
|
|
|
|
glyphs: mapStyle.glyphs.replace('{key}', newAccessToken)
|
|
|
|
}
|
2018-09-03 22:02:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-11 16:53:40 +02:00
|
|
|
return changedStyle
|
|
|
|
}
|
|
|
|
|
2020-01-19 21:05:11 +01:00
|
|
|
function stripAccessTokens(mapStyle) {
|
|
|
|
const changedMetadata = {
|
|
|
|
...mapStyle.metadata
|
|
|
|
};
|
|
|
|
delete changedMetadata['maputnik:mapbox_access_token'];
|
|
|
|
delete changedMetadata['maputnik:openmaptiles_access_token'];
|
|
|
|
return {
|
|
|
|
...mapStyle,
|
|
|
|
metadata: changedMetadata
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-09-10 21:29:18 +02:00
|
|
|
export default {
|
2016-12-26 12:21:41 +01:00
|
|
|
ensureStyleValidity,
|
2016-12-16 14:49:25 +01:00
|
|
|
emptyStyle,
|
2016-12-20 20:21:35 +01:00
|
|
|
indexOfLayer,
|
2016-12-22 18:08:42 +01:00
|
|
|
generateId,
|
2018-11-27 13:29:47 +01:00
|
|
|
getAccessToken,
|
2018-04-11 16:53:40 +02:00
|
|
|
replaceAccessTokens,
|
2020-01-19 21:05:11 +01:00
|
|
|
stripAccessTokens,
|
2016-09-10 21:29:18 +02:00
|
|
|
}
|