Add 'hidebydefault' option for Markers layers

This commit is contained in:
Mike Primm 2011-09-08 00:22:44 +08:00 committed by mikeprimm
parent 771059bf5a
commit 9da159f842
2 changed files with 146 additions and 143 deletions

View File

@ -47,6 +47,8 @@ components:
#showlabel: true #showlabel: true
# If set, enables support for using signs for markers # If set, enables support for using signs for markers
#enablesigns: true #enablesigns: true
# If set, make markers layer hidden by default
#hidebydefault: true
- class: org.dynmap.ClientComponent - class: org.dynmap.ClientComponent
type: chat type: chat

View File

@ -1,144 +1,145 @@
var dynmapmarkersets = {}; var dynmapmarkersets = {};
componentconstructors['markers'] = function(dynmap, configuration) { componentconstructors['markers'] = function(dynmap, configuration) {
var me = this; var me = this;
function removeAllMarkers() { function removeAllMarkers() {
$.each(dynmapmarkersets, function(setname, set) { $.each(dynmapmarkersets, function(setname, set) {
$.each(set.markers, function(mname, marker) { $.each(set.markers, function(mname, marker) {
set.layergroup.removeLayer(marker.our_marker); set.layergroup.removeLayer(marker.our_marker);
}); });
set.markers = {}; set.markers = {};
}); });
} }
function loadmarkers(world) { function loadmarkers(world) {
removeAllMarkers(); removeAllMarkers();
$.getJSON(dynmap.options.tileUrl+'_markers_/marker_'+world+'.json', function(data) { $.getJSON(dynmap.options.tileUrl+'_markers_/marker_'+world+'.json', function(data) {
var ts = data.timestamp; var ts = data.timestamp;
$.each(data.sets, function(name, markerset) { $.each(data.sets, function(name, markerset) {
var ms = dynmapmarkersets[name]; var ms = dynmapmarkersets[name];
if(!ms) { if(!ms) {
ms = { label: markerset.label, markers: {} } ; ms = { label: markerset.label, markers: {} } ;
createMarkerSet(ms, ts); createMarkerSet(ms, ts);
} }
else { else {
if(ms.label != markerset.label) { if(ms.label != markerset.label) {
ms.label = markerset.label; ms.label = markerset.label;
dynmap.layercontrol.removeLayer(ms.layergroup); dynmap.layercontrol.removeLayer(ms.layergroup);
dynmap.layercontrol.addOverlay(ms.layergroup, ms.label); dynmap.layercontrol.addOverlay(ms.layergroup, ms.label);
} }
ms.markers = {}; ms.markers = {};
ms.timestamp = ts; ms.timestamp = ts;
} }
dynmapmarkersets[name] = ms; dynmapmarkersets[name] = ms;
$.each(markerset.markers, function(mname, marker) { $.each(markerset.markers, function(mname, marker) {
ms.markers[mname] = { label: marker.label, x: marker.x, y: marker.y, z:marker.z, ms.markers[mname] = { label: marker.label, x: marker.x, y: marker.y, z:marker.z,
icon: marker.icon }; icon: marker.icon };
createMarker(ms, ms.markers[mname], ts); createMarker(ms, ms.markers[mname], ts);
}); });
}); });
}); });
} }
function getPosition(marker) { function getPosition(marker) {
return dynmap.getProjection().fromLocationToLatLng({ x: marker.x, y: marker.y, z: marker.z }); return dynmap.getProjection().fromLocationToLatLng({ x: marker.x, y: marker.y, z: marker.z });
} }
function createMarker(set, marker, ts) { function createMarker(set, marker, ts) {
var markerPosition = getPosition(marker); var markerPosition = getPosition(marker);
marker.our_marker = new L.CustomMarker(markerPosition, { elementCreator: function() { marker.our_marker = new L.CustomMarker(markerPosition, { elementCreator: function() {
var div = document.createElement('div'); var div = document.createElement('div');
var markerPosition = getPosition(marker); var markerPosition = getPosition(marker);
marker.our_marker.setLatLng(markerPosition); marker.our_marker.setLatLng(markerPosition);
$(div) $(div)
.addClass('Marker') .addClass('Marker')
.addClass('mapMarker') .addClass('mapMarker')
.append($('<img/>').addClass('markerIcon16x16').attr({ src: dynmap.options.tileUrl+'_markers_/'+marker.icon+'.png' })) .append($('<img/>').addClass('markerIcon16x16').attr({ src: dynmap.options.tileUrl+'_markers_/'+marker.icon+'.png' }))
.append($('<span/>') .append($('<span/>')
.addClass(configuration.showlabel?'markerName-show':'markerName') .addClass(configuration.showlabel?'markerName-show':'markerName')
.text(marker.label)); .text(marker.label));
return div; return div;
}}); }});
marker.timestamp = ts; marker.timestamp = ts;
set.layergroup.addLayer(marker.our_marker); set.layergroup.addLayer(marker.our_marker);
} }
function createMarkerSet(set, ts) { function createMarkerSet(set, ts) {
set.layergroup = new L.LayerGroup(); set.layergroup = new L.LayerGroup();
set.timestamp = ts; set.timestamp = ts;
dynmap.map.addLayer(set.layergroup); if(!configuration.hidebydefault)
dynmap.layercontrol.addOverlay(set.layergroup, set.label); dynmap.map.addLayer(set.layergroup);
} dynmap.layercontrol.addOverlay(set.layergroup, set.label);
}
$(dynmap).bind('component.markers', function(event, msg) {
if(msg.msg == 'markerupdated') { $(dynmap).bind('component.markers', function(event, msg) {
var marker = dynmapmarkersets[msg.set].markers[msg.id]; if(msg.msg == 'markerupdated') {
if(marker && marker.our_marker) { var marker = dynmapmarkersets[msg.set].markers[msg.id];
dynmapmarkersets[msg.set].layergroup.removeLayer(marker.our_marker); if(marker && marker.our_marker) {
delete marker.our_marker; dynmapmarkersets[msg.set].layergroup.removeLayer(marker.our_marker);
} delete marker.our_marker;
marker = { x: msg.x, y: msg.y, z: msg.z, icon: msg.icon, label: msg.label }; }
dynmapmarkersets[msg.set].markers[msg.id] = marker; marker = { x: msg.x, y: msg.y, z: msg.z, icon: msg.icon, label: msg.label };
createMarker(dynmapmarkersets[msg.set], marker); dynmapmarkersets[msg.set].markers[msg.id] = marker;
} createMarker(dynmapmarkersets[msg.set], marker);
else if(msg.msg == 'markerdeleted') { }
var marker = dynmapmarkersets[msg.set].markers[msg.id]; else if(msg.msg == 'markerdeleted') {
if(marker && marker.our_marker) { var marker = dynmapmarkersets[msg.set].markers[msg.id];
dynmapmarkersets[msg.set].layergroup.removeLayer(marker.our_marker); if(marker && marker.our_marker) {
} dynmapmarkersets[msg.set].layergroup.removeLayer(marker.our_marker);
delete dynmapmarkersets[msg.set].markers[msg.id]; }
} delete dynmapmarkersets[msg.set].markers[msg.id];
else if(msg.msg == 'setupdated') { }
if(!dynmapmarkersets[msg.id]) { else if(msg.msg == 'setupdated') {
dynmapmarkersets[msg.id] = { label: msg.label, markers:{} }; if(!dynmapmarkersets[msg.id]) {
createMarkerSet(dynmapmarkersets[msg.id]); dynmapmarkersets[msg.id] = { label: msg.label, markers:{} };
} createMarkerSet(dynmapmarkersets[msg.id]);
else { }
if(dynmapmarkersets[msg.id].label != msg.label) { else {
dynmapmarkersets[msg.id].label = msg.label; if(dynmapmarkersets[msg.id].label != msg.label) {
dynmap.layercontrol.removeLayer(dynmapmarkersets[msg.id].layergroup); dynmapmarkersets[msg.id].label = msg.label;
dynmap.layercontrol.addOverlay(dynmapmarkersets[msg.id].layergroup, dynmapmarkersets[msg.id].label); dynmap.layercontrol.removeLayer(dynmapmarkersets[msg.id].layergroup);
} dynmap.layercontrol.addOverlay(dynmapmarkersets[msg.id].layergroup, dynmapmarkersets[msg.id].label);
} }
} }
else if(msg.msg == 'setdeleted') { }
if(dynmapmarkersets[msg.id]) { else if(msg.msg == 'setdeleted') {
dynmap.layercontrol.removeLayer(dynmapmarkersets[msg.id].layergroup); if(dynmapmarkersets[msg.id]) {
delete dynmapmarkersets[msg.id].layergroup; dynmap.layercontrol.removeLayer(dynmapmarkersets[msg.id].layergroup);
delete dynmapmarkersets[msg.id]; delete dynmapmarkersets[msg.id].layergroup;
} delete dynmapmarkersets[msg.id];
} }
}); }
});
// Remove marker on start of map change
$(dynmap).bind('mapchanging', function(event) { // Remove marker on start of map change
$.each(dynmapmarkersets, function(setname, set) { $(dynmap).bind('mapchanging', function(event) {
$.each(set.markers, function(mname, marker) { $.each(dynmapmarkersets, function(setname, set) {
set.layergroup.removeLayer(marker.our_marker); $.each(set.markers, function(mname, marker) {
}); set.layergroup.removeLayer(marker.our_marker);
}); });
}); });
// Remove marker on map change - let update place it again });
$(dynmap).bind('mapchanged', function(event) { // Remove marker on map change - let update place it again
$.each(dynmapmarkersets, function(setname, set) { $(dynmap).bind('mapchanged', function(event) {
$.each(set.markers, function(mname, marker) { $.each(dynmapmarkersets, function(setname, set) {
var marker = set.markers[mname]; $.each(set.markers, function(mname, marker) {
var markerPosition = getPosition(marker); var marker = set.markers[mname];
marker.our_marker.setLatLng(markerPosition); var markerPosition = getPosition(marker);
if(dynmap.map.hasLayer(marker.our_marker) == false) marker.our_marker.setLatLng(markerPosition);
set.layergroup.addLayer(marker.our_marker); if(dynmap.map.hasLayer(marker.our_marker) == false)
}); set.layergroup.addLayer(marker.our_marker);
}); });
}); });
// Load markers for new world });
$(dynmap).bind('worldchanged', function(event) { // Load markers for new world
loadmarkers(this.world.name); $(dynmap).bind('worldchanged', function(event) {
}); loadmarkers(this.world.name);
});
loadmarkers(dynmap.world.name);
loadmarkers(dynmap.world.name);
}; };