2016-09-08 21:42:18 +02:00
|
|
|
import React from 'react';
|
2016-09-09 18:53:57 +02:00
|
|
|
import randomColor from 'randomcolor'
|
|
|
|
|
|
|
|
function assignColorsToLayers(layers) {
|
|
|
|
return layers.map(layer => {
|
|
|
|
if(!layer.metadata) {
|
|
|
|
layer.metadata = {}
|
|
|
|
}
|
|
|
|
if(!"mapolo:color" in layer.metadata) {
|
|
|
|
layer.metadata["mapolo:color"] = randomColor()
|
|
|
|
}
|
|
|
|
return layer
|
|
|
|
})
|
|
|
|
}
|
2016-09-08 21:42:18 +02:00
|
|
|
|
2016-09-09 15:49:23 +02:00
|
|
|
// A wrapper around Mapbox GL style to publish
|
|
|
|
// and subscribe to map changes
|
|
|
|
export class StyleManager {
|
|
|
|
constructor(mapStyle) {
|
|
|
|
this.commandHistory = [];
|
|
|
|
this.subscribers = [];
|
|
|
|
this.mapStyle = mapStyle;
|
2016-09-09 18:53:57 +02:00
|
|
|
|
|
|
|
if(this.mapStyle) {
|
|
|
|
this.mapStyle.layers = assignColorsToLayers(this.mapStyle.layers)
|
|
|
|
}
|
2016-09-08 21:42:18 +02:00
|
|
|
}
|
2016-09-09 00:10:54 +02:00
|
|
|
|
2016-09-09 15:49:23 +02:00
|
|
|
onStyleChange(cb) {
|
|
|
|
this.subscribers.push(cb);
|
2016-09-08 21:42:18 +02:00
|
|
|
}
|
|
|
|
|
2016-09-09 18:53:57 +02:00
|
|
|
changeStyle(change) {
|
|
|
|
this.commandHistory.push(change)
|
|
|
|
this.subscribers.forEach(f => f(change))
|
|
|
|
console.log(change)
|
2016-09-08 21:42:18 +02:00
|
|
|
}
|
|
|
|
|
2016-09-09 16:58:48 +02:00
|
|
|
exportStyle() {
|
|
|
|
return JSON.stringify(this.mapStyle, null, 4)
|
|
|
|
}
|
|
|
|
|
2016-09-09 18:53:57 +02:00
|
|
|
settings() {
|
|
|
|
const { name, sprite, glyphs, owner } = this.mapStyle
|
|
|
|
return { name, sprite, glyphs, owner }
|
|
|
|
}
|
|
|
|
|
|
|
|
set name(val) {
|
|
|
|
this.mapStyle.name = val
|
|
|
|
}
|
|
|
|
|
|
|
|
set owner(val) {
|
|
|
|
this.mapStyle.owner = val
|
|
|
|
}
|
|
|
|
|
|
|
|
set glyphs(val) {
|
|
|
|
this.mapStyle.glyphs = val
|
|
|
|
this.changeStyle({
|
|
|
|
command: 'setStyle',
|
|
|
|
args: [this.mapStyle]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
set sprite(val) {
|
|
|
|
this.mapStyle.sprite = val
|
|
|
|
this.changeStyle({
|
|
|
|
command: 'setStyle',
|
|
|
|
args: [this.mapStyle]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-09-09 15:49:23 +02:00
|
|
|
layer(layerId) {
|
|
|
|
return this.mapStyle.layers[layerId]
|
2016-09-08 21:42:18 +02:00
|
|
|
}
|
|
|
|
|
2016-09-09 00:10:54 +02:00
|
|
|
layers() {
|
2016-09-09 15:49:23 +02:00
|
|
|
if(this.mapStyle) return this.mapStyle.layers
|
|
|
|
return []
|
2016-09-08 21:42:18 +02:00
|
|
|
}
|
|
|
|
}
|