meshviewer/lib/map.js

222 lines
6.3 KiB
JavaScript
Raw Normal View History

define(['map/clientlayer', 'map/labellayer', 'map/button', 'leaflet'],
function (ClientLayer, LabelLayer, Button, L) {
'use strict';
2016-05-27 21:59:01 +00:00
var options = {
worldCopyJump: true,
zoomControl: true,
minZoom: 0
};
2015-04-04 17:08:28 +00:00
return function (config, linkScale, sidebar, router, buttons) {
var self = this;
var savedView;
2015-03-24 23:54:00 +00:00
var map;
var layerControl;
var baseLayers = {};
2015-04-04 17:08:28 +00:00
function saveView() {
savedView = {
center: map.getCenter(),
zoom: map.getZoom()
};
}
function contextMenuOpenLayerMenu() {
document.querySelector('.leaflet-control-layers').classList.add('leaflet-control-layers-expanded');
}
var el = document.createElement('div');
el.classList.add('map');
2015-03-24 23:54:00 +00:00
map = L.map(el, options);
var now = new Date();
config.mapLayers.forEach(function (item, i) {
2017-01-07 02:09:14 +00:00
if ((typeof item.config.start === 'number' && item.config.start <= now.getHours()) || (typeof item.config.end === 'number' && item.config.end > now.getHours())) {
item.config.order = item.config.start * -1;
} else {
item.config.order = i;
}
});
config.mapLayers = config.mapLayers.sort(function (a, b) {
return a.config.order - b.config.order;
});
2015-03-24 23:54:00 +00:00
var layers = config.mapLayers.map(function (d) {
2015-07-07 14:36:19 +00:00
return {
'name': d.name,
'layer': 'url' in d ? L.tileLayer(d.url.replace('{retina}', L.Browser.retina ? '@2x' : ''), d.config) : console.warn('Missing map url')
};
});
2015-07-07 14:36:19 +00:00
2016-12-28 03:21:55 +00:00
map.addLayer(layers[0].layer);
2015-07-07 14:36:19 +00:00
layers.forEach(function (d) {
baseLayers[d.name] = d.layer;
});
2015-03-24 23:54:00 +00:00
var button = new Button(config, map, router, buttons);
map.on('locationfound', button.locationFound);
map.on('locationerror', button.locationError);
map.on('dragend', saveView);
map.on('contextmenu', contextMenuOpenLayerMenu);
2015-04-04 17:08:28 +00:00
button.init();
2015-04-04 17:08:28 +00:00
layerControl = L.control.layers(baseLayers, [], { position: 'bottomright' });
layerControl.addTo(map);
2015-04-15 20:25:22 +00:00
2017-02-26 15:09:23 +00:00
map.zoomControl.setPosition('topright');
var clientLayer = new ClientLayer({ minZoom: config.clientZoom });
clientLayer.addTo(map);
clientLayer.setZIndex(5);
2015-04-16 23:02:56 +00:00
var labelLayer = new LabelLayer({ minZoom: config.labelZoom });
labelLayer.addTo(map);
labelLayer.setZIndex(6);
map.on('zoom', function () {
clientLayer.redraw();
labelLayer.redraw();
});
2015-04-17 21:54:19 +00:00
map.on('baselayerchange', function (e) {
map.options.maxZoom = e.layer.options.maxZoom;
clientLayer.options.maxZoom = map.options.maxZoom;
labelLayer.options.maxZoom = map.options.maxZoom;
if (map.getZoom() > map.options.maxZoom) {
map.setZoom(map.options.maxZoom);
}
2017-01-31 03:45:27 +00:00
var style = document.querySelector('.css-mode:not([media="not"])');
if (style && e.layer.options.mode !== '' && !style.classList.contains(e.layer.options.mode)) {
style.media = 'not';
labelLayer.updateLayer();
}
if (e.layer.options.mode) {
var newStyle = document.querySelector('.css-mode.' + e.layer.options.mode);
newStyle.media = '';
newStyle.appendChild(document.createTextNode(''));
labelLayer.updateLayer();
}
});
2017-10-21 00:06:42 +00:00
map.on('load', function () {
document.querySelectorAll('.leaflet-control-layers-selector').forEach(function (input) {
input.setAttribute('role', 'radiogroup');
input.setAttribute('aria-label', input.nextSibling.innerHTML.trim());
});
});
var nodeDict = {};
var linkDict = {};
var highlight;
2015-04-17 20:10:47 +00:00
function resetMarkerStyles(nodes, links) {
Object.keys(nodes).forEach(function (d) {
nodes[d].resetStyle();
});
Object.keys(links).forEach(function (d) {
links[d].resetStyle();
});
2015-04-17 20:10:47 +00:00
}
2017-03-05 11:29:21 +00:00
function setView(bounds, zoom) {
map.fitBounds(bounds, { paddingTopLeft: [sidebar(), 0], maxZoom: (zoom ? zoom : config.nodeZoom) });
2015-04-17 20:10:47 +00:00
}
2015-04-17 20:10:47 +00:00
function goto(m) {
var bounds;
if ('getBounds' in m) {
bounds = m.getBounds();
} else {
bounds = L.latLngBounds([m.getLatLng()]);
}
setView(bounds);
return m;
2015-04-17 20:10:47 +00:00
}
2015-04-17 20:10:47 +00:00
function updateView(nopanzoom) {
resetMarkerStyles(nodeDict, linkDict);
var m;
if (highlight !== undefined) {
if (highlight.type === 'node' && nodeDict[highlight.o.node_id]) {
m = nodeDict[highlight.o.node_id];
2017-02-12 23:31:31 +00:00
m.setStyle({ color: 'orange', weight: 20, fillOpacity: 1, opacity: 0.7, className: 'stroke-first' });
} else if (highlight.type === 'link' && linkDict[highlight.o.id]) {
m = linkDict[highlight.o.id];
2017-02-12 23:31:31 +00:00
m.setStyle({ weight: 4, opacity: 1, dashArray: '5, 10' });
2015-04-17 20:10:47 +00:00
}
}
2015-04-17 20:10:47 +00:00
if (!nopanzoom) {
if (m) {
goto(m);
} else if (savedView) {
map.setView(savedView.center, savedView.zoom);
} else {
2016-12-28 03:21:55 +00:00
setView(config.fixedCenter);
}
}
2015-04-17 20:10:47 +00:00
}
self.setData = function setData(data) {
nodeDict = {};
linkDict = {};
clientLayer.setData(data);
labelLayer.setData(data, map, nodeDict, linkDict, linkScale, router, config);
2015-03-29 14:14:10 +00:00
updateView(true);
};
2015-04-16 23:02:56 +00:00
self.resetView = function resetView() {
button.disableTracking();
highlight = undefined;
updateView();
};
2015-03-29 14:14:10 +00:00
2017-03-05 11:29:21 +00:00
self.gotoNode = function gotoNode(d) {
button.disableTracking();
highlight = { type: 'node', o: d };
2017-03-05 11:29:21 +00:00
updateView();
};
2015-03-25 00:16:15 +00:00
2017-03-05 11:29:21 +00:00
self.gotoLink = function gotoLink(d) {
button.disableTracking();
highlight = { type: 'link', o: d };
2017-03-05 11:29:21 +00:00
updateView();
};
2015-03-24 23:54:00 +00:00
2017-03-05 11:29:21 +00:00
self.gotoLocation = function gotoLocation(d) {
button.disableTracking();
2017-03-05 11:29:21 +00:00
map.setView([d.lat, d.lng], d.zoom);
};
self.destroy = function destroy() {
button.clearButtons();
map.remove();
2015-07-11 22:11:18 +00:00
if (el.parentNode) {
el.parentNode.removeChild(el);
}
};
2015-07-11 22:11:18 +00:00
self.render = function render(d) {
d.appendChild(el);
map.invalidateSize();
};
2015-03-24 23:54:00 +00:00
return self;
};
});