1
0
forked from MTSR/mapserver

globals refactoring

This commit is contained in:
NatureFreshMilk 2019-09-18 11:53:58 +02:00
parent 68114bc460
commit cb3d8bea54
36 changed files with 268 additions and 316 deletions

34
static/js/LayerManager.js Normal file
View File

@ -0,0 +1,34 @@
import RealtimeTileLayer from './map/RealtimeTileLayer.js';
class LayerManager {
setup(layers){
this.layers = layers;
this.currentLayer = this.layers[0];
}
setLayerId(layerId){
var self = this;
this.layers.forEach(function(layer){
if (layer.id == layerId){
self.currentLayer = layer;
return;
}
});
if (layerId != this.currentLayer.id){
// layer not found
this.currentLayer = this.layers[0];
}
}
getLayerByY(y){
return this.layers.find(layer => (y >= (layer.from*16) && y <= (layer.to*16)));
}
getCurrentLayer(){
return this.currentLayer;
}
}
export default new LayerManager();

View File

@ -0,0 +1,22 @@
import LayerManager from '../LayerManager.js';
function onchange(e){
const params = m.route.param();
params.layerId = e.target.value;
m.route.set("/map/:layerId/:zoom/:lon/:lat", params);
}
export default {
view: function(){
const layers = LayerManager.layers.map(layer => m(
"option",
{ value: layer.id, selected: layer.id == LayerManager.getCurrentLayer().id },
layer.name
))
return m("select", { class: "form-control", onchange: onchange },layers);
}
};

View File

@ -1,33 +1,36 @@
import layerManager from '../map/LayerManager.js'; import layerManager from '../LayerManager.js';
import { createMap } from '../map/MapFactory.js'; import { createMap } from '../map/MapFactory.js';
function setupMap(vnode){
const map = createMap(
vnode.dom,
layerManager.getCurrentLayer().id,
+vnode.attrs.zoom,
+vnode.attrs.lat,
+vnode.attrs.lon
);
vnode.state.map = map;
function updateHash(){
const center = map.getCenter();
const layerId = layerManager.getCurrentLayer().id;
m.route.set(`/map/${layerId}/${map.getZoom()}/` +
`${Math.floor(center.lng)}/${Math.floor(center.lat)}`);
}
map.on('zoomend', updateHash);
map.on('moveend', updateHash);
}
export default { export default {
view(){ view(){
return m("div", { class: "full-screen" }); return m("div", { class: "full-screen" });
}, },
oncreate(vnode){ oncreate(vnode){
setupMap(vnode);
const map = createMap(
vnode.dom,
+vnode.attrs.layerId,
+vnode.attrs.zoom,
+vnode.attrs.lat,
+vnode.attrs.lon
);
vnode.state.map = map;
function updateHash(){
const center = map.getCenter();
const layerId = layerManager.getCurrentLayer().id;
m.route.set(`/map/${layerId}/${map.getZoom()}/${center.lng}/${center.lat}`);
}
map.on('zoomend', updateHash);
map.on('moveend', updateHash);
map.on('baselayerchange', updateHash);
}, },
onbeforeupdate(newVnode) { onbeforeupdate(newVnode) {
@ -36,13 +39,22 @@ export default {
return newAattrs.layerId != layerManager.getCurrentLayer().id || return newAattrs.layerId != layerManager.getCurrentLayer().id ||
newAattrs.zoom != newVnode.state.map.getZoom() || newAattrs.zoom != newVnode.state.map.getZoom() ||
Math.abs(newAattrs.lat - center.lat) > 0.1 || Math.abs(newAattrs.lat - center.lat) > 2 ||
Math.abs(newAattrs.lat - center.lat) > 0.1; Math.abs(newAattrs.lat - center.lat) > 2;
}, },
onupdate(vnode){ onupdate(vnode){
layerManager.switchLayer(+vnode.attrs.layerId); if (vnode.attrs.layerId != layerManager.getCurrentLayer().id){
vnode.state.map.setView([+vnode.attrs.lat, +vnode.attrs.lon], +vnode.attrs.zoom); //layer changed, recreate map
vnode.state.map.remove();
layerManager.setLayerId(vnode.attrs.layerId);
setupMap(vnode);
} else {
//position/zoom change
vnode.state.map.setView([+vnode.attrs.lat, +vnode.attrs.lon], +vnode.attrs.zoom);
}
}, },
onremove(vnode){ onremove(vnode){

View File

@ -1,4 +1,4 @@
import layerMgr from '../map/LayerManager.js'; import layerMgr from '../LayerManager.js';
export default { export default {
view: function(vnode){ view: function(vnode){

View File

@ -4,7 +4,7 @@ import routes from './routes.js';
import wsChannel from './WebSocketChannel.js'; import wsChannel from './WebSocketChannel.js';
import config from './config.js'; import config from './config.js';
import { hashCompat } from './compat.js'; import { hashCompat } from './compat.js';
import layerManager from './map/LayerManager.js'; import layerManager from './LayerManager.js';
// hash route compat // hash route compat
hashCompat(); hashCompat();

View File

@ -1,20 +0,0 @@
const Component = {
view: function(){
return m("select", {
class: "form-control"
},[
m("option", { value: "Ground" }, "Ground"),
m("option", { value: "Sky" }, "Sky")
]
);
}
};
export default L.Control.extend({
onAdd: function() {
var div = L.DomUtil.create('div');
m.mount(div, Component);
return div;
}
});

View File

@ -1,81 +0,0 @@
import RealtimeTileLayer from './RealtimeTileLayer.js';
class LayerManager {
setup(layers){
this.listeners = [];
this.layers = layers;
this.currentLayer = this.layers[0];
}
setupMap(wsChannel, map, currentLayerId){
this.map = map;
this.layerObjects = {};
var self = this;
//All layers
this.layers.forEach(function(layer){
var tileLayer = new RealtimeTileLayer(wsChannel, layer.id, map);
self.layerObjects[layer.name] = tileLayer;
if (layer.id == currentLayerId){
tileLayer.addTo(map);
self.currentLayer = layer;
}
});
map.on('baselayerchange', function (e) {
self.setLayerId(e.layer.layerId);
});
}
switchLayer(layerId){
var self = this;
Object.keys(this.layerObjects).forEach(function(key){
var layerObj = self.layerObjects[key];
if (self.map.hasLayer(layerObj)){
self.map.removeLayer(layerObj);
}
});
Object.keys(this.layerObjects).forEach(function(key){
var layerObj = self.layerObjects[key];
if (layerObj.layerId == layerId){
self.map.addLayer(layerObj);
}
});
}
setLayerId(layerId){
var self = this;
this.layers.forEach(function(layer){
if (layer.id == layerId){
self.currentLayer = layer;
self.listeners.forEach(function(listener){
listener(layer);
});
return;
}
});
}
getLayerByY(y){
return this.layers.find(layer => (y >= (layer.from*16) && y <= (layer.to*16)));
}
addListener(listener){
this.listeners.push(listener);
}
removeListener(listener){
this.listeners = this.listeners.filter(function(el){
return el != listener;
});
}
getCurrentLayer(){
return this.currentLayer;
}
}
export default new LayerManager();

View File

@ -2,11 +2,11 @@ import wsChannel from '../WebSocketChannel.js';
import SimpleCRS from './SimpleCRS.js'; import SimpleCRS from './SimpleCRS.js';
import CoordinatesDisplay from './CoordinatesDisplay.js'; import CoordinatesDisplay from './CoordinatesDisplay.js';
import WorldInfoDisplay from './WorldInfoDisplay.js'; import WorldInfoDisplay from './WorldInfoDisplay.js';
import SearchControl from './SearchControl.js'; import TopRightControl from './TopRightControl.js';
import LayerControl from './LayerControl.js';
import Overlaysetup from './Overlaysetup.js'; import Overlaysetup from './Overlaysetup.js';
import CustomOverlay from './CustomOverlay.js'; import CustomOverlay from './CustomOverlay.js';
import layerManager from './LayerManager.js'; import RealtimeTileLayer from './RealtimeTileLayer.js';
import config from '../config.js'; import config from '../config.js';
@ -24,25 +24,20 @@ export function createMap(node, layerId, zoom, lat, lon){
map.attributionControl.addAttribution('<a href="https://github.com/minetest-tools/mapserver">Minetest Mapserver</a>'); map.attributionControl.addAttribution('<a href="https://github.com/minetest-tools/mapserver">Minetest Mapserver</a>');
var overlays = {}; var tileLayer = new RealtimeTileLayer(wsChannel, layerId, map);
tileLayer.addTo(map);
layerManager.setupMap(wsChannel, map, layerId);
//All overlays //All overlays
Overlaysetup(cfg, map, overlays, wsChannel, layerManager); var overlays = {};
Overlaysetup(cfg, map, overlays);
CustomOverlay(map, overlays); CustomOverlay(map, overlays);
new CoordinatesDisplay({ position: 'bottomleft' }).addTo(map); new CoordinatesDisplay({ position: 'bottomleft' }).addTo(map);
new WorldInfoDisplay(wsChannel, { position: 'bottomright' }).addTo(map); new WorldInfoDisplay(wsChannel, { position: 'bottomright' }).addTo(map);
new TopRightControl({ position: 'topright' }).addTo(map);
if (cfg.enablesearch){
new SearchControl(wsChannel, { position: 'topright' }).addTo(map);
}
new LayerControl({ position: 'topright' }).addTo(map);
//layer control //layer control
L.control.layers(layerManager.layerObjects, overlays, { position: "topright" }).addTo(map); L.control.layers({}, overlays, { position: "topright" }).addTo(map);
return map; return map;
} }

View File

@ -22,140 +22,140 @@ import BorderOverlay from './overlays/BorderOverlay.js';
import TrainOverlay from './overlays/TrainOverlay.js'; import TrainOverlay from './overlays/TrainOverlay.js';
import TrainsignalOverlay from './overlays/TrainsignalOverlay.js'; import TrainsignalOverlay from './overlays/TrainsignalOverlay.js';
export default function(cfg, map, overlays, wsChannel, layerMgr){ export default function(cfg, map, overlays, wsChannel){
function isDefault(key){ function isDefault(key){
return cfg.defaultoverlays.indexOf(key) >= 0; return cfg.defaultoverlays.indexOf(key) >= 0;
} }
if (cfg.mapobjects.mapserver_player) { if (cfg.mapobjects.mapserver_player) {
overlays.Player = new PlayerOverlay(wsChannel, layerMgr); overlays.Player = new PlayerOverlay();
if (isDefault("mapserver_player")) { if (isDefault("mapserver_player")) {
map.addLayer(overlays.Player); map.addLayer(overlays.Player);
} }
} }
if (cfg.mapobjects.mapserver_poi) { if (cfg.mapobjects.mapserver_poi) {
overlays.POI = new PoiOverlay(wsChannel, layerMgr); overlays.POI = new PoiOverlay(wsChannel);
if (isDefault("mapserver_poi")) { if (isDefault("mapserver_poi")) {
map.addLayer(overlays.POI); map.addLayer(overlays.POI);
} }
} }
if (cfg.mapobjects.smartshop || cfg.mapobjects.fancyvend) { if (cfg.mapobjects.smartshop || cfg.mapobjects.fancyvend) {
overlays.Shop = new ShopOverlay(wsChannel, layerMgr); overlays.Shop = new ShopOverlay();
if (isDefault("smartshop") || isDefault("fancyvend")) { if (isDefault("smartshop") || isDefault("fancyvend")) {
map.addLayer(overlays.Shop); map.addLayer(overlays.Shop);
} }
} }
if (cfg.mapobjects.mapserver_label) { if (cfg.mapobjects.mapserver_label) {
overlays.Label = new LabelOverlay(wsChannel, layerMgr); overlays.Label = new LabelOverlay();
if (isDefault("mapserver_label")) { if (isDefault("mapserver_label")) {
map.addLayer(overlays.Label); map.addLayer(overlays.Label);
} }
} }
if (cfg.mapobjects.mapserver_trainline) { if (cfg.mapobjects.mapserver_trainline) {
overlays.Trainlines = new TrainlineOverlay(wsChannel, layerMgr); overlays.Trainlines = new TrainlineOverlay();
if (isDefault("mapserver_trainline")) { if (isDefault("mapserver_trainline")) {
map.addLayer(overlays.Trainlines); map.addLayer(overlays.Trainlines);
} }
} }
if (cfg.mapobjects.mapserver_border) { if (cfg.mapobjects.mapserver_border) {
overlays.Border = new BorderOverlay(wsChannel, layerMgr); overlays.Border = new BorderOverlay();
if (isDefault("mapserver_border")) { if (isDefault("mapserver_border")) {
map.addLayer(overlays.Border); map.addLayer(overlays.Border);
} }
} }
if (cfg.mapobjects.travelnet) { if (cfg.mapobjects.travelnet) {
overlays.Travelnet = new TravelnetOverlay(wsChannel, layerMgr); overlays.Travelnet = new TravelnetOverlay();
if (isDefault("travelnet")) { if (isDefault("travelnet")) {
map.addLayer(overlays.Travelnet); map.addLayer(overlays.Travelnet);
} }
} }
if (cfg.mapobjects.bones) { if (cfg.mapobjects.bones) {
overlays.Bones = new BonesOverlay(wsChannel, layerMgr); overlays.Bones = new BonesOverlay();
if (isDefault("bones")) { if (isDefault("bones")) {
map.addLayer(overlays.Bones); map.addLayer(overlays.Bones);
} }
} }
if (cfg.mapobjects.digilines) { if (cfg.mapobjects.digilines) {
overlays["Digilines LCD"] = new LcdOverlay(wsChannel, layerMgr); overlays["Digilines LCD"] = new LcdOverlay();
if (isDefault("digilines")) { if (isDefault("digilines")) {
map.addLayer(overlays["Digilines LCD"]); map.addLayer(overlays["Digilines LCD"]);
} }
} }
if (cfg.mapobjects.digiterms) { if (cfg.mapobjects.digiterms) {
overlays.Digiterms = new DigitermOverlay(wsChannel, layerMgr); overlays.Digiterms = new DigitermOverlay();
if (isDefault("digiterms")) { if (isDefault("digiterms")) {
map.addLayer(overlays.Digiterms); map.addLayer(overlays.Digiterms);
} }
} }
if (cfg.mapobjects.luacontroller) { if (cfg.mapobjects.luacontroller) {
overlays["Lua Controller"] = new LuacontrollerOverlay(wsChannel, layerMgr); overlays["Lua Controller"] = new LuacontrollerOverlay();
if (isDefault("luacontroller")) { if (isDefault("luacontroller")) {
map.addLayer(overlays["Lua Controller"]); map.addLayer(overlays["Lua Controller"]);
} }
} }
if (cfg.mapobjects.technic_anchor) { if (cfg.mapobjects.technic_anchor) {
overlays["Technic Anchor"] = new TechnicAnchorOverlay(wsChannel, layerMgr); overlays["Technic Anchor"] = new TechnicAnchorOverlay();
if (isDefault("technic_anchor")) { if (isDefault("technic_anchor")) {
map.addLayer(overlays["Technic Anchor"]); map.addLayer(overlays["Technic Anchor"]);
} }
} }
if (cfg.mapobjects.technic_quarry) { if (cfg.mapobjects.technic_quarry) {
overlays["Technic Quarry"] = new TechnicQuarryOverlay(wsChannel, layerMgr); overlays["Technic Quarry"] = new TechnicQuarryOverlay();
if (isDefault("technic_quarry")) { if (isDefault("technic_quarry")) {
map.addLayer(overlays["Technic Quarry"]); map.addLayer(overlays["Technic Quarry"]);
} }
} }
if (cfg.mapobjects.technic_switch) { if (cfg.mapobjects.technic_switch) {
overlays["Technic Switching station"] = new TechnicSwitchOverlay(wsChannel, layerMgr); overlays["Technic Switching station"] = new TechnicSwitchOverlay();
if (isDefault("technic_switch")) { if (isDefault("technic_switch")) {
map.addLayer(overlays["Technic Switching station"]); map.addLayer(overlays["Technic Switching station"]);
} }
} }
if (cfg.mapobjects.protector) { if (cfg.mapobjects.protector) {
overlays.Protector = new ProtectorOverlay(wsChannel, layerMgr); overlays.Protector = new ProtectorOverlay();
if (isDefault("protector")) { if (isDefault("protector")) {
map.addLayer(overlays.Protector); map.addLayer(overlays.Protector);
} }
} }
if (cfg.mapobjects.xpprotector) { if (cfg.mapobjects.xpprotector) {
overlays["XP Protector"] = new XPProtectorOverlay(wsChannel, layerMgr); overlays["XP Protector"] = new XPProtectorOverlay();
if (isDefault("xpprotector")) { if (isDefault("xpprotector")) {
map.addLayer(overlays["XP Protector"]); map.addLayer(overlays["XP Protector"]);
} }
} }
if (cfg.mapobjects.privprotector) { if (cfg.mapobjects.privprotector) {
overlays["Priv Protector"] = new PrivProtectorOverlay(wsChannel, layerMgr); overlays["Priv Protector"] = new PrivProtectorOverlay();
if (isDefault("privprotector")) { if (isDefault("privprotector")) {
map.addLayer(overlays["Priv Protector"]); map.addLayer(overlays["Priv Protector"]);
} }
} }
if (cfg.mapobjects.mission) { if (cfg.mapobjects.mission) {
overlays.Missions = new MissionOverlay(wsChannel, layerMgr); overlays.Missions = new MissionOverlay();
if (isDefault("mission")) { if (isDefault("mission")) {
map.addLayer(overlays.Missions); map.addLayer(overlays.Missions);
} }
} }
if (cfg.mapobjects.train) { if (cfg.mapobjects.train) {
overlays.Trains = new TrainOverlay(wsChannel, layerMgr); overlays.Trains = new TrainOverlay();
if (isDefault("train")) { if (isDefault("train")) {
map.addLayer(overlays.Trains); map.addLayer(overlays.Trains);
@ -163,7 +163,7 @@ export default function(cfg, map, overlays, wsChannel, layerMgr){
} }
if (cfg.mapobjects.trainsignal) { if (cfg.mapobjects.trainsignal) {
overlays.Trainsignals = new TrainsignalOverlay(wsChannel, layerMgr); overlays.Trainsignals = new TrainsignalOverlay();
if (isDefault("trainsignal")) { if (isDefault("trainsignal")) {
map.addLayer(overlays.Trainsignals); map.addLayer(overlays.Trainsignals);
@ -171,21 +171,21 @@ export default function(cfg, map, overlays, wsChannel, layerMgr){
} }
if (cfg.mapobjects.minecart) { if (cfg.mapobjects.minecart) {
overlays.Minecart = new MinecartOverlay(wsChannel, layerMgr); overlays.Minecart = new MinecartOverlay();
if (isDefault("minecart")) { if (isDefault("minecart")) {
map.addLayer(overlays.Minecart); map.addLayer(overlays.Minecart);
} }
} }
if (cfg.mapobjects.atm) { if (cfg.mapobjects.atm) {
overlays.ATM = new ATMOverlay(wsChannel, layerMgr); overlays.ATM = new ATMOverlay();
if (isDefault("atm")) { if (isDefault("atm")) {
map.addLayer(overlays.ATM); map.addLayer(overlays.ATM);
} }
} }
if (cfg.mapobjects.locator) { if (cfg.mapobjects.locator) {
overlays.Locator = new LocatorOverlay(wsChannel, layerMgr); overlays.Locator = new LocatorOverlay();
if (isDefault("locator")) { if (isDefault("locator")) {
map.addLayer(overlays.Locator); map.addLayer(overlays.Locator);
} }

View File

@ -1,13 +0,0 @@
import SearchInput from '../components/SearchInput.js';
export default L.Control.extend({
initialize: function(wsChannel, opts) {
L.Control.prototype.initialize.call(this, opts);
},
onAdd: function() {
var div = L.DomUtil.create('div');
m.mount(div, SearchInput);
return div;
}
});

View File

@ -0,0 +1,27 @@
import SearchInput from '../components/SearchInput.js';
import LayerSelector from '../components/LayerSelector.js';
import config from '../config.js';
const Component = {
view: function(){
const cfg = config.get();
return m("div", [
cfg.enablesearch ? m(SearchInput) : null,
m(LayerSelector)
])
}
}
export default L.Control.extend({
initialize: function(wsChannel, opts) {
L.Control.prototype.initialize.call(this, opts);
},
onAdd: function() {
var div = L.DomUtil.create('div');
m.mount(div, Component);
return div;
}
});

View File

@ -1,8 +1,8 @@
import AbstractIconOverlay from './AbstractIconOverlay.js'; import AbstractIconOverlay from './AbstractIconOverlay.js';
export default AbstractIconOverlay.extend({ export default AbstractIconOverlay.extend({
initialize: function(wsChannel, layerMgr) { initialize: function() {
AbstractIconOverlay.prototype.initialize.call(this, wsChannel, layerMgr, "atm"); AbstractIconOverlay.prototype.initialize.call(this, "atm");
}, },
getMaxDisplayedZoom: function(){ getMaxDisplayedZoom: function(){

View File

@ -1,21 +1,17 @@
import debounce from '../../util/debounce.js'; import debounce from '../../util/debounce.js';
import wsChannel from '../../WebSocketChannel.js';
import layerMgr from '../../LayerManager.js';
import { getMapObjects } from '../../api.js'; import { getMapObjects } from '../../api.js';
export default L.LayerGroup.extend({ export default L.LayerGroup.extend({
initialize: function(wsChannel, layerMgr, type) { initialize: function(type) {
L.LayerGroup.prototype.initialize.call(this); L.LayerGroup.prototype.initialize.call(this);
this.layerMgr = layerMgr;
this.wsChannel = wsChannel;
this.type = type; this.type = type;
this.onLayerChange = this.onLayerChange.bind(this);
this.onMapMove = debounce(this.onMapMove.bind(this), 50); this.onMapMove = debounce(this.onMapMove.bind(this), 50);
}, },
onLayerChange: function(){
this.reDraw();
},
onMapMove: function(){ onMapMove: function(){
this.reDraw(); this.reDraw();
@ -61,7 +57,7 @@ export default L.LayerGroup.extend({
return; return;
} }
var mapLayer = this.layerMgr.getCurrentLayer(); var mapLayer = layerMgr.getCurrentLayer();
var min = this._map.getBounds().getSouthWest(); var min = this._map.getBounds().getSouthWest();
var max = this._map.getBounds().getNorthEast(); var max = this._map.getBounds().getNorthEast();
@ -90,7 +86,6 @@ export default L.LayerGroup.extend({
this.map = map; this.map = map;
map.on("zoomend", this.onMapMove); map.on("zoomend", this.onMapMove);
map.on("moveend", this.onMapMove); map.on("moveend", this.onMapMove);
this.layerMgr.addListener(this.onLayerChange);
this.reDraw(true); this.reDraw(true);
}, },
@ -98,7 +93,6 @@ export default L.LayerGroup.extend({
this.clearLayers(); this.clearLayers();
map.off("zoomend", this.onMapMove); map.off("zoomend", this.onMapMove);
map.off("moveend", this.onMapMove); map.off("moveend", this.onMapMove);
this.layerMgr.removeListener(this.onLayerChange);
} }
}); });

View File

@ -1,12 +1,13 @@
import debounce from '../../util/debounce.js'; import debounce from '../../util/debounce.js';
import wsChannel from '../../WebSocketChannel.js';
import layerMgr from '../../LayerManager.js';
import { getMapObjects } from '../../api.js'; import { getMapObjects } from '../../api.js';
export default L.LayerGroup.extend({ export default L.LayerGroup.extend({
initialize: function(wsChannel, layerMgr, type, icon) { initialize: function(type, icon) {
L.LayerGroup.prototype.initialize.call(this); L.LayerGroup.prototype.initialize.call(this);
this.layerMgr = layerMgr;
this.wsChannel = wsChannel;
this.type = type; this.type = type;
this.icon = icon; this.icon = icon;
@ -66,7 +67,7 @@ export default L.LayerGroup.extend({
this.currentObjects = {}; this.currentObjects = {};
} }
var mapLayer = this.layerMgr.getCurrentLayer(); var mapLayer = layerMgr.getCurrentLayer();
var min = this.map.getBounds().getSouthWest(); var min = this.map.getBounds().getSouthWest();
var max = this.map.getBounds().getNorthEast(); var max = this.map.getBounds().getNorthEast();
@ -134,8 +135,7 @@ export default L.LayerGroup.extend({
this.map = map; this.map = map;
map.on("zoomend", this.onMapMove); map.on("zoomend", this.onMapMove);
map.on("moveend", this.onMapMove); map.on("moveend", this.onMapMove);
this.layerMgr.addListener(this.onLayerChange); wsChannel.addListener("mapobject-created", this.onMapObjectUpdated);
this.wsChannel.addListener("mapobject-created", this.onMapObjectUpdated);
this.reDraw(true); this.reDraw(true);
}, },
@ -143,8 +143,7 @@ export default L.LayerGroup.extend({
this.clearLayers(); this.clearLayers();
map.off("zoomend", this.onMapMove); map.off("zoomend", this.onMapMove);
map.off("moveend", this.onMapMove); map.off("moveend", this.onMapMove);
this.layerMgr.removeListener(this.onLayerChange); wsChannel.removeListener("mapobject-created", this.onMapObjectUpdated);
this.wsChannel.removeListener("mapobject-created", this.onMapObjectUpdated);
} }
}); });

View File

@ -9,8 +9,8 @@ var BonesIcon = L.icon({
}); });
export default AbstractIconOverlay.extend({ export default AbstractIconOverlay.extend({
initialize: function(wsChannel, layerMgr) { initialize: function() {
AbstractIconOverlay.prototype.initialize.call(this, wsChannel, layerMgr, "bones", BonesIcon); AbstractIconOverlay.prototype.initialize.call(this, "bones", BonesIcon);
}, },
createPopup: function(bones){ createPopup: function(bones){

View File

@ -1,8 +1,8 @@
import AbstractGeoJsonOverlay from './AbstractGeoJsonOverlay.js'; import AbstractGeoJsonOverlay from './AbstractGeoJsonOverlay.js';
export default AbstractGeoJsonOverlay.extend({ export default AbstractGeoJsonOverlay.extend({
initialize: function(wsChannel, layerMgr) { initialize: function() {
AbstractGeoJsonOverlay.prototype.initialize.call(this, wsChannel, layerMgr, "border"); AbstractGeoJsonOverlay.prototype.initialize.call(this, "border");
}, },
getMaxDisplayedZoom: function(){ getMaxDisplayedZoom: function(){

View File

@ -9,8 +9,8 @@ var DigitermIcon = L.icon({
}); });
export default AbstractIconOverlay.extend({ export default AbstractIconOverlay.extend({
initialize: function(wsChannel, layerMgr) { initialize: function() {
AbstractIconOverlay.prototype.initialize.call(this, wsChannel, layerMgr, "digiterm", DigitermIcon); AbstractIconOverlay.prototype.initialize.call(this, "digiterm", DigitermIcon);
}, },
createPopup: function(lcd){ createPopup: function(lcd){

View File

@ -1,8 +1,8 @@
import AbstractIconOverlay from './AbstractIconOverlay.js'; import AbstractIconOverlay from './AbstractIconOverlay.js';
export default AbstractIconOverlay.extend({ export default AbstractIconOverlay.extend({
initialize: function(wsChannel, layerMgr) { initialize: function() {
AbstractIconOverlay.prototype.initialize.call(this, wsChannel, layerMgr, "label"); AbstractIconOverlay.prototype.initialize.call(this, "label");
}, },
getMaxDisplayedZoom: function(){ getMaxDisplayedZoom: function(){

View File

@ -9,8 +9,8 @@ var LcdIcon = L.icon({
}); });
export default AbstractIconOverlay.extend({ export default AbstractIconOverlay.extend({
initialize: function(wsChannel, layerMgr) { initialize: function() {
AbstractIconOverlay.prototype.initialize.call(this, wsChannel, layerMgr, "digilinelcd", LcdIcon); AbstractIconOverlay.prototype.initialize.call(this, "digilinelcd", LcdIcon);
}, },
createPopup: function(lcd){ createPopup: function(lcd){

View File

@ -1,8 +1,8 @@
import AbstractIconOverlay from './AbstractIconOverlay.js'; import AbstractIconOverlay from './AbstractIconOverlay.js';
export default AbstractIconOverlay.extend({ export default AbstractIconOverlay.extend({
initialize: function(wsChannel, layerMgr) { initialize: function() {
AbstractIconOverlay.prototype.initialize.call(this, wsChannel, layerMgr, "locator"); AbstractIconOverlay.prototype.initialize.call(this, "locator");
}, },
getMaxDisplayedZoom: function(){ getMaxDisplayedZoom: function(){

View File

@ -17,8 +17,8 @@ var LuacontrollerBurntIcon = L.icon({
}); });
export default AbstractIconOverlay.extend({ export default AbstractIconOverlay.extend({
initialize: function(wsChannel, layerMgr) { initialize: function() {
AbstractIconOverlay.prototype.initialize.call(this, wsChannel, layerMgr, "luacontroller"); AbstractIconOverlay.prototype.initialize.call(this, "luacontroller");
}, },
getIcon: function(ctrl){ getIcon: function(ctrl){

View File

@ -1,21 +1,22 @@
import wsChannel from '../../WebSocketChannel.js';
import layerMgr from '../../LayerManager.js';
let minecarts = [];
//update minecarts all the time
wsChannel.addListener("minetest-info", function(info){
minecarts = info.minecarts || [];
});
export default L.LayerGroup.extend({ export default L.LayerGroup.extend({
initialize: function(wsChannel, layerMgr) { initialize: function() {
L.LayerGroup.prototype.initialize.call(this); L.LayerGroup.prototype.initialize.call(this);
this.layerMgr = layerMgr;
this.wsChannel = wsChannel;
this.currentObjects = {}; // name => marker this.currentObjects = {}; // name => marker
this.minecarts = [];
this.reDraw = this.reDraw.bind(this); this.reDraw = this.reDraw.bind(this);
this.onMinetestUpdate = this.onMinetestUpdate.bind(this); this.onMinetestUpdate = this.onMinetestUpdate.bind(this);
//update players all the time
this.wsChannel.addListener("minetest-info", function(info){
this.minecarts = info.minecarts || [];
}.bind(this));
}, },
createMarker: function(cart){ createMarker: function(cart){
@ -38,7 +39,7 @@ export default L.LayerGroup.extend({
}, },
isCartInCurrentLayer: function(cart){ isCartInCurrentLayer: function(cart){
var mapLayer = this.layerMgr.getCurrentLayer(); var mapLayer = layerMgr.getCurrentLayer();
return (cart.pos.y >= (mapLayer.from*16) && cart.pos.y <= (mapLayer.to*16)); return (cart.pos.y >= (mapLayer.from*16) && cart.pos.y <= (mapLayer.to*16));
}, },
@ -47,7 +48,7 @@ export default L.LayerGroup.extend({
onMinetestUpdate: function(/*info*/){ onMinetestUpdate: function(/*info*/){
var self = this; var self = this;
this.minecarts.forEach(function(cart){ minecarts.forEach(function(cart){
var isInLayer = self.isCartInCurrentLayer(cart); var isInLayer = self.isCartInCurrentLayer(cart);
if (!isInLayer){ if (!isInLayer){
@ -76,7 +77,7 @@ export default L.LayerGroup.extend({
}); });
Object.keys(self.currentObjects).forEach(function(existingId){ Object.keys(self.currentObjects).forEach(function(existingId){
var cartIsActive = self.minecarts.find(function(t){ var cartIsActive = minecarts.find(function(t){
return t.id == existingId; return t.id == existingId;
}); });
@ -92,9 +93,7 @@ export default L.LayerGroup.extend({
this.currentObjects = {}; this.currentObjects = {};
this.clearLayers(); this.clearLayers();
var mapLayer = this.layerMgr.getCurrentLayer(); minecarts.forEach(function(cart){
this.minecarts.forEach(function(cart){
if (!self.isCartInCurrentLayer(cart)){ if (!self.isCartInCurrentLayer(cart)){
//not in current layer //not in current layer
return; return;
@ -108,14 +107,12 @@ export default L.LayerGroup.extend({
}, },
onAdd: function(/*map*/) { onAdd: function(/*map*/) {
this.layerMgr.addListener(this.reDraw); wsChannel.addListener("minetest-info", this.onMinetestUpdate);
this.wsChannel.addListener("minetest-info", this.onMinetestUpdate);
this.reDraw(); this.reDraw();
}, },
onRemove: function(/*map*/) { onRemove: function(/*map*/) {
this.clearLayers(); this.clearLayers();
this.layerMgr.removeListener(this.reDraw); wsChannel.removeListener("minetest-info", this.onMinetestUpdate);
this.wsChannel.removeListener("minetest-info", this.onMinetestUpdate);
} }
}); });

View File

@ -9,8 +9,8 @@ var MissionIcon = L.icon({
}); });
export default AbstractIconOverlay.extend({ export default AbstractIconOverlay.extend({
initialize: function(wsChannel, layerMgr) { initialize: function() {
AbstractIconOverlay.prototype.initialize.call(this, wsChannel, layerMgr, "mission", MissionIcon); AbstractIconOverlay.prototype.initialize.call(this, "mission", MissionIcon);
}, },
createPopup: function(mission){ createPopup: function(mission){

View File

@ -1,3 +1,12 @@
import wsChannel from '../../WebSocketChannel.js';
import layerMgr from '../../LayerManager.js';
let players = [];
//update players all the time
wsChannel.addListener("minetest-info", function(info){
players = info.players || [];
});
var PlayerIcon = L.icon({ var PlayerIcon = L.icon({
iconUrl: 'pics/sam.png', iconUrl: 'pics/sam.png',
@ -8,22 +17,13 @@ var PlayerIcon = L.icon({
}); });
export default L.LayerGroup.extend({ export default L.LayerGroup.extend({
initialize: function(wsChannel, layerMgr) { initialize: function() {
L.LayerGroup.prototype.initialize.call(this); L.LayerGroup.prototype.initialize.call(this);
this.layerMgr = layerMgr;
this.wsChannel = wsChannel;
this.currentObjects = {}; // name => marker this.currentObjects = {}; // name => marker
this.players = [];
this.reDraw = this.reDraw.bind(this); this.reDraw = this.reDraw.bind(this);
this.onMinetestUpdate = this.onMinetestUpdate.bind(this); this.onMinetestUpdate = this.onMinetestUpdate.bind(this);
//update players all the time
this.wsChannel.addListener("minetest-info", function(info){
this.players = info.players || [];
}.bind(this));
}, },
createPopup: function(player){ createPopup: function(player){
@ -62,14 +62,14 @@ export default L.LayerGroup.extend({
}, },
isPlayerInCurrentLayer: function(player){ isPlayerInCurrentLayer: function(player){
var mapLayer = this.layerMgr.getCurrentLayer(); var mapLayer = layerMgr.getCurrentLayer();
return (player.pos.y >= (mapLayer.from*16) && player.pos.y <= (mapLayer.to*16)); return (player.pos.y >= (mapLayer.from*16) && player.pos.y <= (mapLayer.to*16));
}, },
onMinetestUpdate: function(/*info*/){ onMinetestUpdate: function(/*info*/){
this.players.forEach(player => { players.forEach(player => {
var isInLayer = this.isPlayerInCurrentLayer(player); var isInLayer = this.isPlayerInCurrentLayer(player);
if (!isInLayer){ if (!isInLayer){
@ -99,7 +99,7 @@ export default L.LayerGroup.extend({
}); });
Object.keys(this.currentObjects).forEach(existingName => { Object.keys(this.currentObjects).forEach(existingName => {
var playerIsActive = this.players.find(function(p){ var playerIsActive = players.find(function(p){
return p.name == existingName; return p.name == existingName;
}); });
@ -115,9 +115,7 @@ export default L.LayerGroup.extend({
this.currentObjects = {}; this.currentObjects = {};
this.clearLayers(); this.clearLayers();
var mapLayer = this.layerMgr.getCurrentLayer(); players.forEach(player => {
this.players.forEach(player => {
if (!this.isPlayerInCurrentLayer(player)){ if (!this.isPlayerInCurrentLayer(player)){
//not in current layer //not in current layer
return; return;
@ -131,14 +129,12 @@ export default L.LayerGroup.extend({
}, },
onAdd: function(/*map*/) { onAdd: function(/*map*/) {
this.layerMgr.addListener(this.reDraw); wsChannel.addListener("minetest-info", this.onMinetestUpdate);
this.wsChannel.addListener("minetest-info", this.onMinetestUpdate);
this.reDraw(); this.reDraw();
}, },
onRemove: function(/*map*/) { onRemove: function(/*map*/) {
this.clearLayers(); this.clearLayers();
this.layerMgr.removeListener(this.reDraw); wsChannel.removeListener("minetest-info", this.onMinetestUpdate);
this.wsChannel.removeListener("minetest-info", this.onMinetestUpdate);
} }
}); });

View File

@ -1,8 +1,8 @@
import AbstractIconOverlay from './AbstractIconOverlay.js'; import AbstractIconOverlay from './AbstractIconOverlay.js';
export default AbstractIconOverlay.extend({ export default AbstractIconOverlay.extend({
initialize: function(wsChannel, layerMgr) { initialize: function() {
AbstractIconOverlay.prototype.initialize.call(this, wsChannel, layerMgr, "poi"); AbstractIconOverlay.prototype.initialize.call(this, "poi");
}, },
getIcon: function(obj){ getIcon: function(obj){

View File

@ -1,8 +1,8 @@
import AbstractGeoJsonOverlay from './AbstractGeoJsonOverlay.js'; import AbstractGeoJsonOverlay from './AbstractGeoJsonOverlay.js';
export default AbstractGeoJsonOverlay.extend({ export default AbstractGeoJsonOverlay.extend({
initialize: function(wsChannel, layerMgr) { initialize: function() {
AbstractGeoJsonOverlay.prototype.initialize.call(this, wsChannel, layerMgr, "privprotector"); AbstractGeoJsonOverlay.prototype.initialize.call(this, "privprotector");
}, },
createFeature: function(protector){ createFeature: function(protector){

View File

@ -1,8 +1,8 @@
import AbstractGeoJsonOverlay from './AbstractGeoJsonOverlay.js'; import AbstractGeoJsonOverlay from './AbstractGeoJsonOverlay.js';
export default AbstractGeoJsonOverlay.extend({ export default AbstractGeoJsonOverlay.extend({
initialize: function(wsChannel, layerMgr) { initialize: function() {
AbstractGeoJsonOverlay.prototype.initialize.call(this, wsChannel, layerMgr, "protector"); AbstractGeoJsonOverlay.prototype.initialize.call(this, "protector");
}, },
getMaxDisplayedZoom: function(){ getMaxDisplayedZoom: function(){

View File

@ -16,8 +16,8 @@ var ShopEmptyIcon = L.icon({
export default AbstractIconOverlay.extend({ export default AbstractIconOverlay.extend({
initialize: function(wsChannel, layerMgr) { initialize: function() {
AbstractIconOverlay.prototype.initialize.call(this, wsChannel, layerMgr, "shop"); AbstractIconOverlay.prototype.initialize.call(this, "shop");
}, },
getMaxDisplayedZoom: function(){ getMaxDisplayedZoom: function(){

View File

@ -9,8 +9,8 @@ var TechnicAnchorIcon = L.icon({
}); });
export default AbstractIconOverlay.extend({ export default AbstractIconOverlay.extend({
initialize: function(wsChannel, layerMgr) { initialize: function() {
AbstractIconOverlay.prototype.initialize.call(this, wsChannel, layerMgr, "technicanchor", TechnicAnchorIcon); AbstractIconOverlay.prototype.initialize.call(this, "technicanchor", TechnicAnchorIcon);
}, },
createPopup: function(lcd){ createPopup: function(lcd){

View File

@ -9,8 +9,8 @@ var TechnicQuarryIcon = L.icon({
}); });
export default AbstractIconOverlay.extend({ export default AbstractIconOverlay.extend({
initialize: function(wsChannel, layerMgr) { initialize: function() {
AbstractIconOverlay.prototype.initialize.call(this, wsChannel, layerMgr, "technicquarry", TechnicQuarryIcon); AbstractIconOverlay.prototype.initialize.call(this, "technicquarry", TechnicQuarryIcon);
}, },
createPopup: function(quarry){ createPopup: function(quarry){

View File

@ -9,8 +9,8 @@ var TechnicSwitchIcon = L.icon({
}); });
export default AbstractIconOverlay.extend({ export default AbstractIconOverlay.extend({
initialize: function(wsChannel, layerMgr) { initialize: function() {
AbstractIconOverlay.prototype.initialize.call(this, wsChannel, layerMgr, "technicswitch", TechnicSwitchIcon); AbstractIconOverlay.prototype.initialize.call(this, "technicswitch", TechnicSwitchIcon);
}, },
createPopup: function(sw){ createPopup: function(sw){

View File

@ -1,3 +1,5 @@
import wsChannel from '../../WebSocketChannel.js';
import layerMgr from '../../LayerManager.js';
function getTrainImageUrlForType(type){ function getTrainImageUrlForType(type){
switch(type){ switch(type){
@ -29,23 +31,20 @@ function getTrainImageUrlForType(type){
} }
} }
let trains = [];
//update trains all the time
wsChannel.addListener("minetest-info", function(info){
trains = info.trains || [];
});
export default L.LayerGroup.extend({ export default L.LayerGroup.extend({
initialize: function(wsChannel, layerMgr) { initialize: function() {
L.LayerGroup.prototype.initialize.call(this); L.LayerGroup.prototype.initialize.call(this);
this.layerMgr = layerMgr;
this.wsChannel = wsChannel;
this.currentObjects = {}; // name => marker this.currentObjects = {}; // name => marker
this.trains = [];
this.reDraw = this.reDraw.bind(this); this.reDraw = this.reDraw.bind(this);
this.onMinetestUpdate = this.onMinetestUpdate.bind(this);
//update players all the time
this.wsChannel.addListener("minetest-info", function(info){
this.trains = info.trains || [];
}.bind(this));
}, },
createPopup: function(train){ createPopup: function(train){
@ -100,7 +99,7 @@ export default L.LayerGroup.extend({
}, },
isTrainInCurrentLayer: function(train){ isTrainInCurrentLayer: function(train){
var mapLayer = this.layerMgr.getCurrentLayer(); var mapLayer = layerMgr.getCurrentLayer();
return (train.pos.y >= (mapLayer.from*16) && train.pos.y <= (mapLayer.to*16)); return (train.pos.y >= (mapLayer.from*16) && train.pos.y <= (mapLayer.to*16));
}, },
@ -114,7 +113,7 @@ export default L.LayerGroup.extend({
return; return;
} }
this.trains.forEach(train => { trains.forEach(train => {
var isInLayer = this.isTrainInCurrentLayer(train); var isInLayer = this.isTrainInCurrentLayer(train);
if (!isInLayer){ if (!isInLayer){
@ -144,7 +143,7 @@ export default L.LayerGroup.extend({
}); });
Object.keys(this.currentObjects).forEach(existingId => { Object.keys(this.currentObjects).forEach(existingId => {
var trainIsActive = this.trains.find(function(t){ var trainIsActive = trains.find(function(t){
return t.id == existingId; return t.id == existingId;
}); });
@ -164,9 +163,9 @@ export default L.LayerGroup.extend({
return; return;
} }
var mapLayer = this.layerMgr.getCurrentLayer(); var mapLayer = layerMgr.getCurrentLayer();
this.trains.forEach(train => { trains.forEach(train => {
if (!this.isTrainInCurrentLayer(train)){ if (!this.isTrainInCurrentLayer(train)){
//not in current layer //not in current layer
return; return;
@ -181,14 +180,12 @@ export default L.LayerGroup.extend({
onAdd: function(map) { onAdd: function(map) {
this.map = map; this.map = map;
this.layerMgr.addListener(() => this.reDraw()); wsChannel.addListener("minetest-info", () => this.onMinetestUpdate());
this.wsChannel.addListener("minetest-info", () => this.onMinetestUpdate());
this.reDraw(); this.reDraw();
}, },
onRemove: function(/*map*/) { onRemove: function(/*map*/) {
this.clearLayers(); this.clearLayers();
this.layerMgr.removeListener(() => this.reDraw()); wsChannel.removeListener("minetest-info", () => this.onMinetestUpdate());
this.wsChannel.removeListener("minetest-info", () => this.onMinetestUpdate());
} }
}); });

View File

@ -1,8 +1,8 @@
import AbstractGeoJsonOverlay from './AbstractGeoJsonOverlay.js'; import AbstractGeoJsonOverlay from './AbstractGeoJsonOverlay.js';
export default AbstractGeoJsonOverlay.extend({ export default AbstractGeoJsonOverlay.extend({
initialize: function(wsChannel, layerMgr) { initialize: function() {
AbstractGeoJsonOverlay.prototype.initialize.call(this, wsChannel, layerMgr, "train"); AbstractGeoJsonOverlay.prototype.initialize.call(this, "train");
}, },
createGeoJson: function(objects){ createGeoJson: function(objects){

View File

@ -1,3 +1,5 @@
import wsChannel from '../../WebSocketChannel.js';
import layerMgr from '../../LayerManager.js';
var IconOn = L.icon({ var IconOn = L.icon({
iconUrl: "pics/advtrains/advtrains_signal_on.png", iconUrl: "pics/advtrains/advtrains_signal_on.png",
@ -13,25 +15,18 @@ var IconOff = L.icon({
popupAnchor: [0, -16] popupAnchor: [0, -16]
}); });
let signals = [];
//update signals all the time
wsChannel.addListener("minetest-info", function(info){
signals = info.signals || [];
});
export default L.LayerGroup.extend({ export default L.LayerGroup.extend({
initialize: function(wsChannel, layerMgr) { initialize: function() {
L.LayerGroup.prototype.initialize.call(this); L.LayerGroup.prototype.initialize.call(this);
this.layerMgr = layerMgr;
this.wsChannel = wsChannel;
this.currentObjects = {}; // name => marker this.currentObjects = {}; // name => marker
this.signals = [];
this.reDraw = this.reDraw.bind(this);
this.onMinetestUpdate = this.onMinetestUpdate.bind(this);
//update players all the time
this.wsChannel.addListener("minetest-info", function(info){
this.signals = info.signals || [];
}.bind(this));
}, },
createPopup: function(signal){ createPopup: function(signal){
@ -61,7 +56,7 @@ export default L.LayerGroup.extend({
}, },
isSignalInCurrentLayer: function(signal){ isSignalInCurrentLayer: function(signal){
var mapLayer = this.layerMgr.getCurrentLayer(); var mapLayer = layerMgr.getCurrentLayer();
return (signal.pos.y >= (mapLayer.from*16) && signal.pos.y <= (mapLayer.to*16)); return (signal.pos.y >= (mapLayer.from*16) && signal.pos.y <= (mapLayer.to*16));
}, },
@ -127,7 +122,7 @@ export default L.LayerGroup.extend({
return; return;
} }
var mapLayer = this.layerMgr.getCurrentLayer(); var mapLayer = layerMgr.getCurrentLayer();
this.signals.forEach(signal => { this.signals.forEach(signal => {
if (!this.isSignalInCurrentLayer(signal)){ if (!this.isSignalInCurrentLayer(signal)){
@ -145,14 +140,12 @@ export default L.LayerGroup.extend({
onAdd: function(map) { onAdd: function(map) {
this.map = map; this.map = map;
this.layerMgr.addListener(() => this.reDraw()); wsChannel.addListener("minetest-info", () => this.onMinetestUpdate());
this.wsChannel.addListener("minetest-info", () => this.onMinetestUpdate());
this.reDraw(); this.reDraw();
}, },
onRemove: function(/*map*/) { onRemove: function(/*map*/) {
this.clearLayers(); this.clearLayers();
this.layerMgr.removeListener(() => this.reDraw()); wsChannel.removeListener("minetest-info", () => this.onMinetestUpdate());
this.wsChannel.removeListener("minetest-info", () => this.onMinetestUpdate());
} }
}); });

View File

@ -9,8 +9,8 @@ var TravelnetIcon = L.icon({
}); });
export default AbstractIconOverlay.extend({ export default AbstractIconOverlay.extend({
initialize: function(wsChannel, layerMgr) { initialize: function() {
AbstractIconOverlay.prototype.initialize.call(this, wsChannel, layerMgr, "travelnet", TravelnetIcon); AbstractIconOverlay.prototype.initialize.call(this, "travelnet", TravelnetIcon);
}, },
createPopup: function(travelnet){ createPopup: function(travelnet){

View File

@ -1,8 +1,8 @@
import AbstractGeoJsonOverlay from './AbstractGeoJsonOverlay.js'; import AbstractGeoJsonOverlay from './AbstractGeoJsonOverlay.js';
export default AbstractGeoJsonOverlay.extend({ export default AbstractGeoJsonOverlay.extend({
initialize: function(wsChannel, layerMgr) { initialize: function() {
AbstractGeoJsonOverlay.prototype.initialize.call(this, wsChannel, layerMgr, "xpprotector"); AbstractGeoJsonOverlay.prototype.initialize.call(this, "xpprotector");
}, },
createFeature: function(protector){ createFeature: function(protector){