[TASK] Cleanup duplicate code

- map buttons
- layer getTileBBox
This commit is contained in:
Xaver Maierhofer 2017-02-05 02:34:09 +01:00 committed by Geno
parent 59a73a3fb5
commit 6505f5491d
4 changed files with 25 additions and 53 deletions

View File

@ -9,7 +9,7 @@ define(['map/clientlayer', 'map/labelslayer',
zoomControl: false zoomControl: false
}; };
var LocateButton = L.Control.extend({ var ButtonBase = L.Control.extend({
options: { options: {
position: 'bottomright' position: 'bottomright'
}, },
@ -22,6 +22,17 @@ define(['map/clientlayer', 'map/labelslayer',
this.f = f; this.f = f;
}, },
update: function () {
this.button.classList.toggle('active', this.active);
},
set: function (v) {
this.active = v;
this.update();
}
});
var LocateButton = ButtonBase.extend({
onAdd: function () { onAdd: function () {
var button = L.DomUtil.create('button', 'ion-locate shadow'); var button = L.DomUtil.create('button', 'ion-locate shadow');
button.setAttribute('data-tooltip', _.t('button.tracking')); button.setAttribute('data-tooltip', _.t('button.tracking'));
@ -33,33 +44,12 @@ define(['map/clientlayer', 'map/labelslayer',
return button; return button;
}, },
update: function () {
this.button.classList.toggle('active', this.active);
},
set: function (v) {
this.active = v;
this.update();
},
onClick: function () { onClick: function () {
this.f(!this.active); this.f(!this.active);
} }
}); });
var CoordsPickerButton = L.Control.extend({ var CoordsPickerButton = ButtonBase.extend({
options: {
position: 'bottomright'
},
active: false,
button: undefined,
initialize: function (f, o) {
L.Util.setOptions(this, o);
this.f = f;
},
onAdd: function () { onAdd: function () {
var button = L.DomUtil.create('button', 'ion-pin shadow'); var button = L.DomUtil.create('button', 'ion-pin shadow');
button.setAttribute('data-tooltip', _.t('button.location')); button.setAttribute('data-tooltip', _.t('button.location'));
@ -73,20 +63,10 @@ define(['map/clientlayer', 'map/labelslayer',
return button; return button;
}, },
update: function () {
this.button.classList.toggle('active', this.active);
},
set: function (v) {
this.active = v;
this.update();
},
onClick: function (e) { onClick: function (e) {
L.DomEvent.stopPropagation(e); L.DomEvent.stopPropagation(e);
this.f(!this.active); this.f(!this.active);
} }
}); });
function mkMarker(dict, iconFunc, router) { function mkMarker(dict, iconFunc, router) {

View File

@ -1,5 +1,5 @@
define(['leaflet'], define(['leaflet', 'helper'],
function (L) { function (L, helper) {
'use strict'; 'use strict';
return L.TileLayer.Canvas.extend({ return L.TileLayer.Canvas.extend({
@ -13,13 +13,6 @@ define(['leaflet'],
this.redraw(); this.redraw();
}, },
drawTile: function (canvas, tilePoint) { drawTile: function (canvas, tilePoint) {
function getTileBBox(s, map, tileSize, margin) {
var tl = map.unproject([s.x - margin, s.y - margin]);
var br = map.unproject([s.x + margin + tileSize, s.y + margin + tileSize]);
return [br.lat, tl.lng, tl.lat, br.lng];
}
if (!this.data) { if (!this.data) {
return; return;
} }
@ -29,7 +22,7 @@ define(['leaflet'],
var map = this._map; var map = this._map;
var margin = 50; var margin = 50;
var bbox = getTileBBox(s, map, tileSize, margin); var bbox = helper.getTileBBox(s, map, tileSize, margin);
var nodes = this.data.search(bbox); var nodes = this.data.search(bbox);

View File

@ -1,5 +1,5 @@
define(['leaflet', 'rbush'], define(['leaflet', 'rbush', 'helper'],
function (L, rbush) { function (L, rbush, helper) {
'use strict'; 'use strict';
var labelLocations = [['left', 'middle', 0 / 8], var labelLocations = [['left', 'middle', 0 / 8],
@ -188,13 +188,6 @@ define(['leaflet', 'rbush'],
this.redraw(); this.redraw();
}, },
drawTile: function (canvas, tilePoint, zoom) { drawTile: function (canvas, tilePoint, zoom) {
function getTileBBox(s, map, tileSize, margin) {
var tl = map.unproject([s.x - margin, s.y - margin]);
var br = map.unproject([s.x + margin + tileSize, s.y + margin + tileSize]);
return [br.lat, tl.lng, tl.lat, br.lng];
}
if (!this.labels) { if (!this.labels) {
return; return;
} }
@ -212,7 +205,7 @@ define(['leaflet', 'rbush'],
return { p: p, label: d.label }; return { p: p, label: d.label };
} }
var bbox = 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);

View File

@ -232,5 +232,11 @@ define({
} }
return p; return p;
},
getTileBBox: function getTileBBox(s, map, tileSize, margin) {
var tl = map.unproject([s.x - margin, s.y - margin]);
var br = map.unproject([s.x + margin + tileSize, s.y + margin + tileSize]);
return [br.lat, tl.lng, tl.lat, br.lng];
} }
}); });