maputnik/src/libs/style.js

118 lines
2.6 KiB
JavaScript
Raw Normal View History

2016-09-08 21:42:18 +02:00
import React from 'react';
import deref from '@mapbox/mapbox-gl-style-spec/deref'
import tokens from '../config/tokens.json'
2016-09-09 18:53:57 +02: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({
version: 8,
sources: {},
layers: [],
2016-12-20 16:08:49 +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
}
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,
layers: deref(style.layers)
2016-12-26 12:21:41 +01:00
}
return derefedStyle
}
function ensureStyleValidity(style) {
return ensureHasNoInteractive(ensureHasNoRefs(ensureHasId(style)))
2016-09-20 16:49:02 +02: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"
}
const metadata = mapStyle.metadata || {}
2018-08-23 21:52:12 +02:00
let accessToken = metadata[`maputnik:${sourceName}_access_token`]
if(opts.allowFallback && !accessToken) {
2018-08-23 21:52:12 +02:00
accessToken = tokens[sourceName]
}
return accessToken;
}
2018-08-23 21:52:12 +02:00
function replaceSourceAccessToken(mapStyle, sourceName, opts={}) {
const source = mapStyle.sources[sourceName]
if(!source) return mapStyle
if(!source.hasOwnProperty("url")) return mapStyle
2018-08-23 21:52:12 +02:00
const accessToken = getAccessToken(sourceName, mapStyle, opts)
if(!accessToken) {
// Early exit.
return mapStyle;
}
const changedSources = {
...mapStyle.sources,
2018-08-23 21:52:12 +02:00
[sourceName]: {
...source,
url: source.url.replace('{key}', accessToken)
}
}
const changedStyle = {
...mapStyle,
sources: changedSources
}
2018-08-23 21:52:12 +02:00
if (mapStyle.glyphs) {
changedStyle.glyphs = changedStyle.glyphs.replace('{key}', accessToken)
}
return changedStyle
}
function replaceAccessTokens(mapStyle, opts={}) {
2018-08-23 21:52:12 +02:00
let changedStyle = mapStyle
2018-08-23 21:52:12 +02:00
Object.keys(mapStyle.sources).forEach((sourceName) => {
changedStyle = replaceSourceAccessToken(changedStyle, sourceName, opts);
})
return changedStyle
}
export default {
2016-12-26 12:21:41 +01:00
ensureStyleValidity,
emptyStyle,
indexOfLayer,
2016-12-22 18:08:42 +01:00
generateId,
replaceAccessTokens,
}