mapserver/public/js/components/Map.js

62 lines
1.3 KiB
JavaScript
Raw Permalink Normal View History

2019-09-18 12:53:58 +03:00
import layerManager from '../LayerManager.js';
import { createMap } from '../map/MapFactory.js';
2019-12-01 22:00:51 +03:00
function setupMap(vnode, id){
2019-09-18 12:53:58 +03:00
const map = createMap(
2019-12-01 22:00:51 +03:00
id,
2019-09-18 12:53:58 +03:00
layerManager.getCurrentLayer().id,
+vnode.attrs.zoom,
+vnode.attrs.lat,
+vnode.attrs.lon
);
2019-09-18 12:53:58 +03:00
vnode.state.map = map;
2019-09-18 12:53:58 +03:00
function updateHash(){
const center = map.getCenter();
const layerId = layerManager.getCurrentLayer().id;
2019-09-18 12:53:58 +03:00
m.route.set(`/map/${layerId}/${map.getZoom()}/` +
`${Math.floor(center.lng)}/${Math.floor(center.lat)}`);
}
2019-09-18 12:53:58 +03:00
map.on('zoomend', updateHash);
map.on('moveend', updateHash);
2019-11-26 10:42:00 +03:00
return map;
2019-09-18 12:53:58 +03:00
}
2019-09-18 12:53:58 +03:00
export default {
2019-12-01 22:00:51 +03:00
oninit(){
this.id = "map_" + Math.floor(Math.random() * 10000);
},
2019-09-18 12:53:58 +03:00
view(){
2019-12-01 22:00:51 +03:00
return m("div", { class: "full-screen", id: this.id });
2019-09-18 12:53:58 +03:00
},
2019-09-18 12:53:58 +03:00
oncreate(vnode){
2019-12-01 22:00:51 +03:00
this.map = setupMap(vnode, this.id);
},
onupdate(vnode){
2019-09-18 12:53:58 +03:00
if (vnode.attrs.layerId != layerManager.getCurrentLayer().id){
//layer changed, recreate map
2019-11-26 10:42:00 +03:00
this.map.remove();
2019-09-18 12:53:58 +03:00
layerManager.setLayerId(vnode.attrs.layerId);
2019-12-01 22:00:51 +03:00
this.map = setupMap(vnode, this.id);
2019-09-18 12:53:58 +03:00
} else {
//position/zoom change
2019-11-26 10:42:00 +03:00
//this.map.setView([+vnode.attrs.lat, +vnode.attrs.lon], +vnode.attrs.zoom);
2019-09-18 12:53:58 +03:00
}
2019-12-01 20:44:36 +03:00
return false;
},
2019-11-26 13:05:24 +03:00
onremove(){
2019-11-26 10:42:00 +03:00
this.map.remove();
}
2019-09-01 13:06:47 +03:00
};