use 'overlayadd/remove' instead of 'layeradd/remove' events. in theory should be more efficient as layeradd/remove gets called for every single portal/link/field/etc on the map

This commit is contained in:
Jon Atkins 2013-09-13 23:12:49 +01:00
parent b5a52d810e
commit f646f481fc

View File

@ -140,7 +140,7 @@ window.setupMap = function() {
var portalsLayers = [];
for(var i = 0; i <= 8; i++) {
portalsFactionLayers[i] = [L.layerGroup(), L.layerGroup(), L.layerGroup()];
portalsLayers[i] = L.layerGroup();
portalsLayers[i] = L.layerGroup(portalsFactionLayers[i]);
map.addLayer(portalsLayers[i]);
var t = (i === 0 ? 'Unclaimed' : 'Level ' + i) + ' Portals';
addLayers[t] = portalsLayers[i];
@ -149,14 +149,14 @@ window.setupMap = function() {
}
fieldsFactionLayers = [L.layerGroup(), L.layerGroup(), L.layerGroup()];
var fieldsLayer = L.layerGroup();
var fieldsLayer = L.layerGroup(fieldsFactionLayers);
map.addLayer(fieldsLayer, true);
addLayers['Fields'] = fieldsLayer;
// Store it in hiddenLayer to remove later
if(!isLayerGroupDisplayed('Fields', true)) hiddenLayer.push(fieldsLayer);
linksFactionLayers = [L.layerGroup(), L.layerGroup(), L.layerGroup()];
var linksLayer = L.layerGroup();
var linksLayer = L.layerGroup(linksFactionLayers);
map.addLayer(linksLayer, true);
addLayers['Links'] = linksLayer;
// Store it in hiddenLayer to remove later
@ -164,13 +164,15 @@ window.setupMap = function() {
// faction-specific layers
// these layers don't actually contain any data. instead, everytime they're added/removed from the map,
// the matching sub-layers within the above portals/fields/links are added/removed from their parent
// the matching sub-layers within the above portals/fields/links are added/removed from their parent with
// the below 'onoverlayadd/onoverlayremovve' events
var factionLayers = [L.layerGroup(), L.layerGroup(), L.layerGroup()];
window.map.on('layeradd layerremove', function(e) {
for (var fac in factionLayers) {
if (e.layer === factionLayers[fac]) {
if (e.type == 'layeradd') {
map.addLayer (factionLayers[fac]);
}
var setFactionLayersState = function(fac,enabled) {
if (enabled) {
if (!fieldsLayer.hasLayer(fieldsFactionLayers[fac])) fieldsLayer.addLayer (fieldsFactionLayers[fac]);
if (!linksLayer.hasLayer(linksFactionLayers[fac])) linksLayer.addLayer (linksFactionLayers[fac]);
for (var lvl in portalsLayers) {
@ -184,11 +186,6 @@ window.setupMap = function() {
}
}
}
}
});
for (var fac in factionLayers) {
map.addLayer (factionLayers[fac]);
}
// to avoid any favouritism, we'll put the player's own faction layer first
if (PLAYER.team == 'RESISTANCE') {
@ -201,6 +198,23 @@ window.setupMap = function() {
if (!isLayerGroupDisplayed('Resistance', true)) hiddenLayer.push (factionLayers[TEAM_RES]);
if (!isLayerGroupDisplayed('Enlightened', true)) hiddenLayer.push (factionLayers[TEAM_ENL]);
setFactionLayersState (TEAM_NONE, true);
setFactionLayersState (TEAM_RES, isLayerGroupDisplayed('Resistance', true));
setFactionLayersState (TEAM_ENL, isLayerGroupDisplayed('Enlightened', true));
// NOTE: these events are fired by the layer chooser, so won't happen until that's created and added to the map
window.map.on('overlayadd overlayremove', function(e) {
var displayed = (e.type == 'overlayadd');
switch (e.name) {
case 'Resistance':
setFactionLayersState (TEAM_RES, displayed);
break;
case 'Enlightened':
setFactionLayersState (TEAM_ENL, displayed);
break;
}
});
window.layerChooser = new L.Control.Layers({
'MapQuest OSM': views[0],