[TASK] Migrate to leaflet client & labellayer

This commit is contained in:
Martin Geno 2017-03-14 19:25:11 +01:00 committed by Xaver Maierhofer
parent 2a9a1bca5b
commit 5379f7f573
No known key found for this signature in database
GPG Key ID: 7FDCE23FD2EC9FE8
6 changed files with 48 additions and 30 deletions

View File

@ -9,6 +9,7 @@ A web-app to visualize nodes and links on a map for Freifunk open mesh network.
#### Main differences to https://github.com/ffnord/meshviewer #### Main differences to https://github.com/ffnord/meshviewer
_Some similar features might have been implemented/merged_ _Some similar features might have been implemented/merged_
- Leaflet upgraded to v1 - faster on mobile
- Forcegraph rewrite with d3.js v4 - Forcegraph rewrite with d3.js v4
- Map layer modes (Allow to set a default layer based on time combined with a stylesheet) - Map layer modes (Allow to set a default layer based on time combined with a stylesheet)
- Automatic updates for selected node or list (incl. image stats cache-breaker) - Automatic updates for selected node or list (incl. image stats cache-breaker)
@ -23,13 +24,12 @@ _Some similar features might have been implemented/merged_
- css and some js moved inline - css and some js moved inline
- Yarn/npm in favour of bower - Yarn/npm in favour of bower
- Load only moment.js without languages (Languages are included in translations) - Load only moment.js without languages (Languages are included in translations)
- unneeded components removed (es6-shim, tablesort, numeraljs, leaflet-providers, jshashes, chroma-js) - unneeded components removed (es6-shim, tablesort, numeraljs, leaflet-providers, leaflet-label jshashes, chroma-js)
- RBush v2 - performance boost in last versions (positions, labels and clients on the map) - RBush v2 - performance boost in last versions (positions, labels and clients on the map)
- Ruby dependency removed - Ruby dependency removed
- FixedCenter is required - FixedCenter is required
- Sass-lint, scss and variables rewritten for easy customizations/adjustments - Sass-lint, scss and variables rewritten for easy customizations/adjustments
- Cross browser/device support improved (THX@BrowserStack) - Cross browser/device support improved (THX@BrowserStack)
- Leaflet fork with a patch to avoid IE/Edge crashes
- Yarn package manager in favour of npm (npm still works) - Yarn package manager in favour of npm (npm still works)
- Configurable reverse geocoding server - Configurable reverse geocoding server
- [A lot more in the commit history](https://github.com/ffrgb/meshviewer/commits/develop) - [A lot more in the commit history](https://github.com/ffrgb/meshviewer/commits/develop)

2
app.js
View File

@ -58,7 +58,6 @@ require.config({
paths: { paths: {
'polyglot': '../node_modules/node-polyglot/build/polyglot', 'polyglot': '../node_modules/node-polyglot/build/polyglot',
'leaflet': '../node_modules/leaflet/dist/leaflet', 'leaflet': '../node_modules/leaflet/dist/leaflet',
'leaflet.label': '../node_modules/leaflet-label/dist/leaflet.label',
'moment': '../node_modules/moment/moment', 'moment': '../node_modules/moment/moment',
// d3 modules indirect dependencies // d3 modules indirect dependencies
// by d3-zoom: d3-drag // by d3-zoom: d3-drag
@ -83,7 +82,6 @@ require.config({
'language': 'utils/language' 'language': 'utils/language'
}, },
shim: { shim: {
'leaflet.label': ['leaflet'],
'd3-drag': ['d3-selection'], 'd3-drag': ['d3-selection'],
'd3-force': ['d3-collection', 'd3-dispatch', 'd3-quadtree', 'd3-timer'], 'd3-force': ['d3-collection', 'd3-dispatch', 'd3-quadtree', 'd3-timer'],
'd3-interpolate': ['d3-color'], 'd3-interpolate': ['d3-color'],

View File

@ -1,10 +1,11 @@
define(['map/clientlayer', 'map/labelslayer', 'leaflet', 'moment', 'locationmarker', 'rbush', 'helper'], define(['map/clientlayer', 'map/labellayer', 'leaflet', 'moment', 'map/locationmarker', 'rbush', 'helper'],
function (ClientLayer, LabelsLayer, L, moment, LocationMarker, rbush, helper) { function (ClientLayer, LabelLayer, L, moment, LocationMarker, rbush, helper) {
'use strict'; 'use strict';
var options = { var options = {
worldCopyJump: true, worldCopyJump: true,
zoomControl: true zoomControl: true,
minZoom: 0
}; };
var ButtonBase = L.Control.extend({ var ButtonBase = L.Control.extend({
@ -296,18 +297,23 @@ define(['map/clientlayer', 'map/labelslayer', 'leaflet', 'moment', 'locationmark
map.zoomControl.setPosition('topright'); map.zoomControl.setPosition('topright');
var clientLayer = new ClientLayer('', { minZoom: config.clientZoom }); var clientLayer = new ClientLayer({ minZoom: config.clientZoom });
clientLayer.addTo(map); clientLayer.addTo(map);
clientLayer.setZIndex(5); clientLayer.setZIndex(5);
var labelsLayer = new LabelsLayer('', { minZoom: config.labelZoom }); var labelLayer = new LabelLayer({ minZoom: config.labelZoom });
labelsLayer.addTo(map); labelLayer.addTo(map);
labelsLayer.setZIndex(6); labelLayer.setZIndex(6);
map.on('zoom', function () {
clientLayer.redraw();
labelLayer.redraw();
});
map.on('baselayerchange', function (e) { map.on('baselayerchange', function (e) {
map.options.maxZoom = e.layer.options.maxZoom; map.options.maxZoom = e.layer.options.maxZoom;
clientLayer.options.maxZoom = map.options.maxZoom; clientLayer.options.maxZoom = map.options.maxZoom;
labelsLayer.options.maxZoom = map.options.maxZoom; labelLayer.options.maxZoom = map.options.maxZoom;
if (map.getZoom() > map.options.maxZoom) { if (map.getZoom() > map.options.maxZoom) {
map.setZoom(map.options.maxZoom); map.setZoom(map.options.maxZoom);
} }
@ -315,13 +321,13 @@ define(['map/clientlayer', 'map/labelslayer', 'leaflet', 'moment', 'locationmark
var style = document.querySelector('.css-mode:not([media="not"])'); var style = document.querySelector('.css-mode:not([media="not"])');
if (style && e.layer.options.mode !== '' && !style.classList.contains(e.layer.options.mode)) { if (style && e.layer.options.mode !== '' && !style.classList.contains(e.layer.options.mode)) {
style.media = 'not'; style.media = 'not';
labelsLayer.updateLayer(); labelLayer.updateLayer();
} }
if (e.layer.options.mode) { if (e.layer.options.mode) {
var newStyle = document.querySelector('.css-mode.' + e.layer.options.mode); var newStyle = document.querySelector('.css-mode.' + e.layer.options.mode);
newStyle.media = ''; newStyle.media = '';
newStyle.appendChild(document.createTextNode('')); newStyle.appendChild(document.createTextNode(''));
labelsLayer.updateLayer(); labelLayer.updateLayer();
} }
}); });
@ -457,7 +463,7 @@ define(['map/clientlayer', 'map/labelslayer', 'leaflet', 'moment', 'locationmark
rtreeOnlineAll.load(data.nodes.all.filter(helper.online).filter(helper.hasLocation).map(mapRTree)); rtreeOnlineAll.load(data.nodes.all.filter(helper.online).filter(helper.hasLocation).map(mapRTree));
clientLayer.setData(rtreeOnlineAll); clientLayer.setData(rtreeOnlineAll);
labelsLayer.setData({ labelLayer.setData({
online: nodesOnline.filter(helper.hasLocation), online: nodesOnline.filter(helper.hasLocation),
offline: nodesOffline.filter(helper.hasLocation), offline: nodesOffline.filter(helper.hasLocation),
new: data.nodes.new.filter(helper.hasLocation), new: data.nodes.new.filter(helper.hasLocation),

View File

@ -2,7 +2,7 @@ define(['leaflet', 'helper'],
function (L, helper) { function (L, helper) {
'use strict'; 'use strict';
return L.TileLayer.extend({ return L.GridLayer.extend({
setData: function (d) { setData: function (d) {
this.data = d; this.data = d;
@ -12,12 +12,18 @@ define(['leaflet', 'helper'],
}); });
this.redraw(); this.redraw();
}, },
drawTile: function (canvas, tilePoint) { createTile: function (tilePoint) {
if (!this.data) { var tile = L.DomUtil.create('canvas', 'leaflet-tile');
return;
}
var tileSize = this.options.tileSize; var tileSize = this.options.tileSize;
tile.width = tileSize;
tile.height = tileSize;
if (!this.data) {
return tile;
}
var ctx = tile.getContext('2d');
var s = tilePoint.multiplyBy(tileSize); var s = tilePoint.multiplyBy(tileSize);
var map = this._map; var map = this._map;
@ -27,10 +33,9 @@ define(['leaflet', 'helper'],
var nodes = this.data.search(bbox); var nodes = this.data.search(bbox);
if (nodes.length === 0) { if (nodes.length === 0) {
return; return tile;
} }
var ctx = canvas.getContext('2d');
var startDistance = 12; var startDistance = 12;
ctx.beginPath(); ctx.beginPath();
@ -45,6 +50,8 @@ define(['leaflet', 'helper'],
ctx.fillStyle = 'rgba(220, 0, 103, 0.7)'; ctx.fillStyle = 'rgba(220, 0, 103, 0.7)';
ctx.fill(); ctx.fill();
return tile;
} }
}); });
}); });

View File

@ -70,9 +70,9 @@ define(['leaflet', 'rbush', 'helper'],
return { minX: x, minY: y, maxX: x + width, maxY: y + height }; return { minX: x, minY: y, maxX: x + width, maxY: y + height };
} }
return L.TileLayer.extend({ return L.GridLayer.extend({
onAdd: function (map) { onAdd: function (map) {
L.TileLayer.prototype.onAdd.call(this, map); L.GridLayer.prototype.onAdd.call(this, map);
if (this.data) { if (this.data) {
this.prepareLabels(); this.prepareLabels();
} }
@ -180,12 +180,17 @@ define(['leaflet', 'rbush', 'helper'],
this.redraw(); this.redraw();
}, },
drawTile: function (canvas, tilePoint, zoom) { createTile: function (tilePoint) {
if (!this.labels && zoom >= this.options.minZoom) { var tile = L.DomUtil.create('canvas', 'leaflet-tile');
return;
}
var tileSize = this.options.tileSize; var tileSize = this.options.tileSize;
tile.width = tileSize;
tile.height = tileSize;
if (!this.labels) {
return tile;
}
var s = tilePoint.multiplyBy(tileSize); var s = tilePoint.multiplyBy(tileSize);
var map = this._map; var map = this._map;
bodyStyle = window.getComputedStyle(document.querySelector('body')); bodyStyle = window.getComputedStyle(document.querySelector('body'));
@ -202,7 +207,7 @@ define(['leaflet', 'rbush', 'helper'],
var bbox = helper.getTileBBox(s, map, tileSize, this.margin); var bbox = helper.getTileBBox(s, map, tileSize, this.margin);
var labels = this.labels.search(bbox).map(projectNodes); var labels = this.labels.search(bbox).map(projectNodes);
var ctx = canvas.getContext('2d'); var ctx = tile.getContext('2d');
ctx.lineWidth = 5; ctx.lineWidth = 5;
ctx.strokeStyle = labelShadow; ctx.strokeStyle = labelShadow;
@ -222,8 +227,10 @@ define(['leaflet', 'rbush', 'helper'],
} }
labels.filter(function (d) { labels.filter(function (d) {
return zoom >= d.label.minZoom; return tilePoint.z >= d.label.minZoom;
}).forEach(drawLabel); }).forEach(drawLabel);
return tile;
} }
}); });
}); });