2016-09-08 21:42:18 +02:00
|
|
|
import React from 'react';
|
2016-09-10 21:29:18 +02:00
|
|
|
import spec from 'mapbox-gl-style-spec/reference/latest.min.js'
|
2016-12-26 12:21:41 +01:00
|
|
|
import derefLayers from 'mapbox-gl-style-spec/lib/deref'
|
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,
|
|
|
|
layers: derefLayers(style.layers)
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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,
|
2016-09-10 21:29:18 +02:00
|
|
|
}
|