first stage of having faction-specific layers - portals, links and fields are now a layer group consisting of three faction-specific leaflet LayerGroups

also, move load.js from directly embedded in boot.js into an external file
This commit is contained in:
Jon Atkins 2013-09-12 03:07:25 +01:00
parent 9579ca8377
commit ac6a235564
4 changed files with 81 additions and 28 deletions

View File

@ -140,9 +140,11 @@ window.setupMap = function() {
var addLayers = {};
var hiddenLayer = [];
portalsLayers = [];
portalsFactionLayers = [];
var portalsLayers = [];
for(var i = 0; i <= 8; i++) {
portalsLayers[i] = L.layerGroup([]);
portalsFactionLayers[i] = [L.layerGroup(), L.layerGroup(), L.layerGroup()];
portalsLayers[i] = L.layerGroup(portalsFactionLayers[i]);
map.addLayer(portalsLayers[i]);
var t = (i === 0 ? 'Unclaimed' : 'Level ' + i) + ' Portals';
addLayers[t] = portalsLayers[i];
@ -150,13 +152,15 @@ window.setupMap = function() {
if(!isLayerGroupDisplayed(t, true)) hiddenLayer.push(portalsLayers[i]);
}
fieldsLayer = L.layerGroup([]);
fieldsFactionLayers = [L.layerGroup(), L.layerGroup(), 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);
linksLayer = L.layerGroup([]);
linksFactionLayers = [L.layerGroup(), L.layerGroup(), L.layerGroup()];
var linksLayer = L.layerGroup(linksFactionLayers);
map.addLayer(linksLayer, true);
addLayers['Links'] = linksLayer;
// Store it in hiddenLayer to remove later
@ -502,11 +506,10 @@ function boot() {
}
// this is the minified load.js script that allows us to easily load
// further javascript files async as well as in order.
// https://github.com/chriso/load.js
// Copyright (c) 2010 Chris O'Hara <cohara87@gmail.com>. MIT Licensed
function asyncLoadScript(a){return function(b,c){var d=document.createElement("script");d.type="text/javascript",d.src=a,d.onload=b,d.onerror=c,d.onreadystatechange=function(){var a=this.readyState;if(a==="loaded"||a==="complete")d.onreadystatechange=null,b()},head.insertBefore(d,head.firstChild)}}(function(a){a=a||{};var b={},c,d;c=function(a,d,e){var f=a.halt=!1;a.error=function(a){throw a},a.next=function(c){c&&(f=!1);if(!a.halt&&d&&d.length){var e=d.shift(),g=e.shift();f=!0;try{b[g].apply(a,[e,e.length,g])}catch(h){a.error(h)}}return a};for(var g in b){if(typeof a[g]=="function")continue;(function(e){a[e]=function(){var g=Array.prototype.slice.call(arguments);if(e==="onError"){if(d)return b.onError.apply(a,[g,g.length]),a;var h={};return b.onError.apply(h,[g,g.length]),c(h,null,"onError")}return g.unshift(e),d?(a.then=a[e],d.push(g),f?a:a.next()):c({},[g],e)}})(g)}return e&&(a.then=a[e]),a.call=function(b,c){c.unshift(b),d.unshift(c),a.next(!0)},a.next()},d=a.addMethod=function(d){var e=Array.prototype.slice.call(arguments),f=e.pop();for(var g=0,h=e.length;g<h;g++)typeof e[g]=="string"&&(b[e[g]]=f);--h||(b["then"+d.substr(0,1).toUpperCase()+d.substr(1)]=f),c(a)},d("chain",function(a){var b=this,c=function(){if(!b.halt){if(!a.length)return b.next(!0);try{null!=a.shift().call(b,c,b.error)&&c()}catch(d){b.error(d)}}};c()}),d("run",function(a,b){var c=this,d=function(){c.halt||--b||c.next(!0)},e=function(a){c.error(a)};for(var f=0,g=b;!c.halt&&f<g;f++)null!=a[f].call(c,d,e)&&d()}),d("defer",function(a){var b=this;setTimeout(function(){b.next(!0)},a.shift())}),d("onError",function(a,b){var c=this;this.error=function(d){c.halt=!0;for(var e=0;e<b;e++)a[e].call(c,d)}})})(this);var head=document.getElementsByTagName("head")[0]||document.documentElement;addMethod("load",function(a,b){for(var c=[],d=0;d<b;d++)(function(b){c.push(asyncLoadScript(a[b]))})(d);this.call("run",c)})
console.log('...?');
@@INCLUDERAW:external/load.js@@
try { console.log('Loading included JS now'); } catch(e) {}
@@INCLUDERAW:external/leaflet.js@@

View File

@ -139,13 +139,27 @@ window.Render.prototype.endRenderPass = function() {
}
window.Render.prototype.bringPortalsToFront = function() {
for (var i in portalsLayers) {
var layer = portalsLayers[i];
if (window.map.hasLayer(layer)) {
layer.eachLayer (function(p) {
p.bringToFront();
});
for (var lvl in portalsFactionLayers) {
// portals are stored in separate layers per faction
// to avoid giving weight to one faction or another, we'll push portals to front based on GUID order
var portals = {};
for (var fac in portalsFactionLayers[lvl]) {
var layer = portalsFactionLayers[lvl][fac];
if (layer._map) {
layer.eachLayer (function(p) {
portals[p.options.guid] = p;
});
}
}
var guids = Object.keys(portals);
guids.sort();
for (var j in guids) {
var guid = guids[j];
portals[guid].bringToFront();
}
}
}
@ -167,7 +181,7 @@ window.Render.prototype.deletePortalEntity = function(guid) {
window.Render.prototype.deleteLinkEntity = function(guid) {
if (guid in window.links) {
var l = window.links[guid];
linksLayer.removeLayer(l);
linksFactionLayers[l.options.team].removeLayer(l);
delete window.links[guid];
}
}
@ -194,7 +208,7 @@ window.Render.prototype.deleteFieldEntity = function(guid) {
deletePortalLinkedField (fd.capturedRegion.vertexB.guid);
deletePortalLinkedField (fd.capturedRegion.vertexC.guid);
fieldsLayer.removeLayer(f);
fieldsFactionLayers[f.options.team].removeLayer(f);
delete window.fields[guid];
}
}
@ -311,9 +325,7 @@ window.Render.prototype.createPortalEntity = function(ent) {
renderPortalDetails (selectedPortal);
}
// //TODO? postpone adding to the map layer
// portalsLayers[parseInt(portalLevel)].addLayer(marker);
//TODO? postpone adding to the map layer
this.addPortalToMapLayer(marker);
}
@ -349,6 +361,8 @@ window.Render.prototype.createFieldEntity = function(ent) {
fillOpacity: 0.25,
stroke: false,
clickable: false,
team: team,
guid: ent[0],
timestamp: ent[1],
details: ent[2],
@ -375,7 +389,7 @@ window.Render.prototype.createFieldEntity = function(ent) {
window.fields[ent[0]] = poly;
// TODO? postpone adding to the layer??
fieldsLayer.addLayer(poly);
fieldsFactionLayers[poly.options.team].addLayer(poly);
}
window.Render.prototype.createLinkEntity = function(ent) {
@ -406,6 +420,8 @@ window.Render.prototype.createLinkEntity = function(ent) {
opacity: 1,
weight: 2,
clickable: false,
team: team,
guid: ent[0],
timestamp: ent[1],
details: ent[2],
@ -416,7 +432,7 @@ window.Render.prototype.createLinkEntity = function(ent) {
window.links[ent[0]] = poly;
// TODO? postpone adding to the layer??
linksLayer.addLayer(poly);
linksFactionLayers[poly.options.team].addLayer(poly);
}
@ -503,14 +519,14 @@ window.Render.prototype.resetPortalClusters = function() {
for (var i=0; i<c.length; i++) {
var guid = c[i];
var p = window.portals[guid];
var layerGroup = portalsLayers[parseInt(p.options.level)];
var layerGroup = portalsFactionLayers[parseInt(p.options.level)][p.options.team];
if (i<this.CLUSTER_PORTAL_LIMIT || p.options.guid == selectedPortal) {
if (!layerGroup.hasLayer(p)) {
portalsLayers[parseInt(p.options.level)].addLayer(p);
layerGroup.addLayer(p);
}
} else {
if (layerGroup.hasLayer(p)) {
portalsLayers[parseInt(p.options.level)].removeLayer(p);
layerGroup.removeLayer(p);
}
}
}
@ -532,14 +548,14 @@ window.Render.prototype.addPortalToMapLayer = function(portal) {
// however, it won't make a lot of visible difference compared to just pushing to the end of the list, then
// adding to the visible layer if the list is below the limit
if (this.portalClusters[cid].length < this.CLUSTER_PORTAL_LIMIT || portal.options.guid == selectedPortal) {
portalsLayers[parseInt(portal.options.level)].addLayer(portal);
portalsFactionLayers[parseInt(portal.options.level)][portal.options.team].addLayer(portal);
}
}
window.Render.prototype.removePortalFromMapLayer = function(portal) {
//remove it from the portalsLevels layer
portalsLayers[parseInt(portal.options.level)].removeLayer(portal);
portalsFactionLayers[parseInt(portal.options.level)][portal.options.team].removeLayer(portal);
// and ensure there's no mention of the portal in the cluster list
var cid = this.getPortalClusterID(portal);

33
external/load.js vendored Normal file
View File

@ -0,0 +1,33 @@
/* Copyright (c) 2010 Chris O'Hara <cohara87@gmail.com>. MIT Licensed */
//Include the chain.js microframework (http://github.com/chriso/chain.js)
(function(a){a=a||{};var b={},c,d;c=function(a,d,e){var f=a.halt=!1;a.error=function(a){throw a},a.next=function(c){c&&(f=!1);if(!a.halt&&d&&d.length){var e=d.shift(),g=e.shift();f=!0;try{b[g].apply(a,[e,e.length,g])}catch(h){a.error(h)}}return a};for(var g in b){if(typeof a[g]==="function")continue;(function(e){a[e]=function(){var g=Array.prototype.slice.call(arguments);if(e==="onError"){if(d){b.onError.apply(a,[g,g.length]);return a}var h={};b.onError.apply(h,[g,g.length]);return c(h,null,"onError")}g.unshift(e);if(!d)return c({},[g],e);a.then=a[e],d.push(g);return f?a:a.next()}})(g)}e&&(a.then=a[e]),a.call=function(b,c){c.unshift(b),d.unshift(c),a.next(!0)};return a.next()},d=a.addMethod=function(d){var e=Array.prototype.slice.call(arguments),f=e.pop();for(var g=0,h=e.length;g<h;g++)typeof e[g]==="string"&&(b[e[g]]=f);--h||(b["then"+d.substr(0,1).toUpperCase()+d.substr(1)]=f),c(a)},d("chain",function(a){var b=this,c=function(){if(!b.halt){if(!a.length)return b.next(!0);try{null!=a.shift().call(b,c,b.error)&&c()}catch(d){b.error(d)}}};c()}),d("run",function(a,b){var c=this,d=function(){c.halt||--b||c.next(!0)},e=function(a){c.error(a)};for(var f=0,g=b;!c.halt&&f<g;f++)null!=a[f].call(c,d,e)&&d()}),d("defer",function(a){var b=this;setTimeout(function(){b.next(!0)},a.shift())}),d("onError",function(a,b){var c=this;this.error=function(d){c.halt=!0;for(var e=0;e<b;e++)a[e].call(c,d)}})})(this);
var head = document.getElementsByTagName('head')[0] || document.documentElement;
addMethod('load', function (args, argc) {
for (var queue = [], i = 0; i < argc; i++) {
(function (i) {
queue.push(asyncLoadScript(args[i]));
}(i));
}
this.call('run', queue);
});
function asyncLoadScript(src) {
return function (onload, onerror) {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = src;
script.onload = onload;
script.onerror = onerror;
script.onreadystatechange = function () {
var state = this.readyState;
if (state === 'loaded' || state === 'complete') {
script.onreadystatechange = null;
onload();
}
};
head.insertBefore(script, head.firstChild);
}
}

View File

@ -211,7 +211,8 @@ window.selectedPortal = null;
window.portalRangeIndicator = null;
window.portalAccessIndicator = null;
window.mapRunsUserAction = false;
var portalsLayers, linksLayer, fieldsLayer;
//var portalsLayers, linksLayer, fieldsLayer;
var portalsFactionLayers, linksFactionLayers, fieldsFactionLayers;
// contain references to all entities loaded from the server. If render limits are hit,
// not all may be added to the leaflet layers