Add support local Maputnik

This commit is contained in:
Lukas Martinelli
2017-01-01 14:49:32 +01:00
parent 99acbd4d92
commit ce39ae723c
4 changed files with 42 additions and 14 deletions

View File

@@ -1,25 +1,49 @@
import request from 'request'
import style from './style.js'
import ReconnectingWebSocket from 'reconnecting-websocket'
const host = 'localhost'
const port = '8000'
const localUrl = `http://${host}:${port}`
const websocketUrl = `ws://${host}:${port}/ws`
console.log(localUrl, websocketUrl)
export class ApiStyleStore {
constructor(opts) {
if(opts.onLocalStyleChange) {
const connection = new ReconnectingWebSocket(websocketUrl)
connection.onmessage = function(e) {
if(!e.data) return
try {
console.log('Received style update from API')
const updatedStyle = style.ensureStyleValidity(JSON.parse(e.data))
opts.onLocalStyleChange(updatedStyle)
} catch(err) {
console.error('Cannot parse local file ' + e.data)
}
}
}
}
supported(cb) {
request('http://localhost:8000/styles', (error, response, body) => {
cb(error === undefined)
request(localUrl + '/styles', (error, response, body) => {
cb(error === null)
})
}
latestStyle(cb) {
if(this.latestStyleId) {
request('http://localhost:8000/styles/' + this.latestStyleId, (error, response, body) => {
request(localUrl + '/styles/' + this.latestStyleId, (error, response, body) => {
cb(JSON.parse(body))
})
} else {
request('http://localhost:8000/styles', (error, response, body) => {
request(localUrl + '/styles', (error, response, body) => {
if (!error && response.statusCode == 200) {
const styleIds = JSON.parse(body);
this.latestStyleId = styleIds[0];
request('http://localhost:8000/styles/' + this.latestStyleId, (error, response, body) => {
cb(style.fromJSON(JSON.parse(body)))
request(localUrl + '/styles/' + this.latestStyleId, (error, response, body) => {
cb(style.ensureStyleValidity(JSON.parse(body)))
})
}
})
@@ -28,11 +52,11 @@ export class ApiStyleStore {
// Save current style replacing previous version
save(mapStyle) {
const id = mapStyle.get('id')
const id = mapStyle.id
request.put({
url: 'http://localhost:8000/styles/' + id,
url: localUrl + '/styles/' + id,
json: true,
body: style.toJSON(mapStyle)
body: mapStyle
}, (error, response, body) => {
console.log('Saved style');
})