From 9da159f842b4a9529739e46abab8eaf4726520d4 Mon Sep 17 00:00:00 2001 From: Mike Primm Date: Thu, 8 Sep 2011 00:22:44 +0800 Subject: [PATCH] Add 'hidebydefault' option for Markers layers --- src/main/resources/configuration.txt | 2 + web/js/markers.js | 287 ++++++++++++++------------- 2 files changed, 146 insertions(+), 143 deletions(-) diff --git a/src/main/resources/configuration.txt b/src/main/resources/configuration.txt index a2014af2..73ae9a7f 100644 --- a/src/main/resources/configuration.txt +++ b/src/main/resources/configuration.txt @@ -47,6 +47,8 @@ components: #showlabel: true # If set, enables support for using signs for markers #enablesigns: true + # If set, make markers layer hidden by default + #hidebydefault: true - class: org.dynmap.ClientComponent type: chat diff --git a/web/js/markers.js b/web/js/markers.js index 2d760192..cff32d74 100644 --- a/web/js/markers.js +++ b/web/js/markers.js @@ -1,144 +1,145 @@ - -var dynmapmarkersets = {}; - -componentconstructors['markers'] = function(dynmap, configuration) { - var me = this; - - function removeAllMarkers() { - $.each(dynmapmarkersets, function(setname, set) { - $.each(set.markers, function(mname, marker) { - set.layergroup.removeLayer(marker.our_marker); - }); - set.markers = {}; - }); - } - - function loadmarkers(world) { - removeAllMarkers(); - $.getJSON(dynmap.options.tileUrl+'_markers_/marker_'+world+'.json', function(data) { - var ts = data.timestamp; - $.each(data.sets, function(name, markerset) { - var ms = dynmapmarkersets[name]; - if(!ms) { - ms = { label: markerset.label, markers: {} } ; - createMarkerSet(ms, ts); - } - else { - if(ms.label != markerset.label) { - ms.label = markerset.label; - dynmap.layercontrol.removeLayer(ms.layergroup); - dynmap.layercontrol.addOverlay(ms.layergroup, ms.label); - } - ms.markers = {}; - ms.timestamp = ts; - } - dynmapmarkersets[name] = ms; - $.each(markerset.markers, function(mname, marker) { - ms.markers[mname] = { label: marker.label, x: marker.x, y: marker.y, z:marker.z, - icon: marker.icon }; - createMarker(ms, ms.markers[mname], ts); - }); - }); - }); - } - - function getPosition(marker) { - return dynmap.getProjection().fromLocationToLatLng({ x: marker.x, y: marker.y, z: marker.z }); - } - - function createMarker(set, marker, ts) { - var markerPosition = getPosition(marker); - marker.our_marker = new L.CustomMarker(markerPosition, { elementCreator: function() { - var div = document.createElement('div'); - - var markerPosition = getPosition(marker); - marker.our_marker.setLatLng(markerPosition); - - $(div) - .addClass('Marker') - .addClass('mapMarker') - .append($('').addClass('markerIcon16x16').attr({ src: dynmap.options.tileUrl+'_markers_/'+marker.icon+'.png' })) - .append($('') - .addClass(configuration.showlabel?'markerName-show':'markerName') - .text(marker.label)); - return div; - }}); - marker.timestamp = ts; - set.layergroup.addLayer(marker.our_marker); - } - - function createMarkerSet(set, ts) { - set.layergroup = new L.LayerGroup(); - set.timestamp = ts; - dynmap.map.addLayer(set.layergroup); - dynmap.layercontrol.addOverlay(set.layergroup, set.label); - } - - $(dynmap).bind('component.markers', function(event, msg) { - if(msg.msg == 'markerupdated') { - var marker = dynmapmarkersets[msg.set].markers[msg.id]; - if(marker && 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; - createMarker(dynmapmarkersets[msg.set], marker); - } - else if(msg.msg == 'markerdeleted') { - var marker = dynmapmarkersets[msg.set].markers[msg.id]; - if(marker && marker.our_marker) { - dynmapmarkersets[msg.set].layergroup.removeLayer(marker.our_marker); - } - delete dynmapmarkersets[msg.set].markers[msg.id]; - } - else if(msg.msg == 'setupdated') { - if(!dynmapmarkersets[msg.id]) { - dynmapmarkersets[msg.id] = { label: msg.label, markers:{} }; - createMarkerSet(dynmapmarkersets[msg.id]); - } - else { - if(dynmapmarkersets[msg.id].label != msg.label) { - dynmapmarkersets[msg.id].label = msg.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]) { - dynmap.layercontrol.removeLayer(dynmapmarkersets[msg.id].layergroup); - delete dynmapmarkersets[msg.id].layergroup; - delete dynmapmarkersets[msg.id]; - } - } - }); - - // Remove marker on start of map change - $(dynmap).bind('mapchanging', function(event) { - $.each(dynmapmarkersets, function(setname, set) { - $.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) { - $.each(dynmapmarkersets, function(setname, set) { - $.each(set.markers, function(mname, marker) { - var marker = set.markers[mname]; - var markerPosition = getPosition(marker); - marker.our_marker.setLatLng(markerPosition); - if(dynmap.map.hasLayer(marker.our_marker) == false) - set.layergroup.addLayer(marker.our_marker); - }); - }); - }); - // Load markers for new world - $(dynmap).bind('worldchanged', function(event) { - loadmarkers(this.world.name); - }); - - loadmarkers(dynmap.world.name); - + +var dynmapmarkersets = {}; + +componentconstructors['markers'] = function(dynmap, configuration) { + var me = this; + + function removeAllMarkers() { + $.each(dynmapmarkersets, function(setname, set) { + $.each(set.markers, function(mname, marker) { + set.layergroup.removeLayer(marker.our_marker); + }); + set.markers = {}; + }); + } + + function loadmarkers(world) { + removeAllMarkers(); + $.getJSON(dynmap.options.tileUrl+'_markers_/marker_'+world+'.json', function(data) { + var ts = data.timestamp; + $.each(data.sets, function(name, markerset) { + var ms = dynmapmarkersets[name]; + if(!ms) { + ms = { label: markerset.label, markers: {} } ; + createMarkerSet(ms, ts); + } + else { + if(ms.label != markerset.label) { + ms.label = markerset.label; + dynmap.layercontrol.removeLayer(ms.layergroup); + dynmap.layercontrol.addOverlay(ms.layergroup, ms.label); + } + ms.markers = {}; + ms.timestamp = ts; + } + dynmapmarkersets[name] = ms; + $.each(markerset.markers, function(mname, marker) { + ms.markers[mname] = { label: marker.label, x: marker.x, y: marker.y, z:marker.z, + icon: marker.icon }; + createMarker(ms, ms.markers[mname], ts); + }); + }); + }); + } + + function getPosition(marker) { + return dynmap.getProjection().fromLocationToLatLng({ x: marker.x, y: marker.y, z: marker.z }); + } + + function createMarker(set, marker, ts) { + var markerPosition = getPosition(marker); + marker.our_marker = new L.CustomMarker(markerPosition, { elementCreator: function() { + var div = document.createElement('div'); + + var markerPosition = getPosition(marker); + marker.our_marker.setLatLng(markerPosition); + + $(div) + .addClass('Marker') + .addClass('mapMarker') + .append($('').addClass('markerIcon16x16').attr({ src: dynmap.options.tileUrl+'_markers_/'+marker.icon+'.png' })) + .append($('') + .addClass(configuration.showlabel?'markerName-show':'markerName') + .text(marker.label)); + return div; + }}); + marker.timestamp = ts; + set.layergroup.addLayer(marker.our_marker); + } + + function createMarkerSet(set, ts) { + set.layergroup = new L.LayerGroup(); + set.timestamp = ts; + if(!configuration.hidebydefault) + dynmap.map.addLayer(set.layergroup); + dynmap.layercontrol.addOverlay(set.layergroup, set.label); + } + + $(dynmap).bind('component.markers', function(event, msg) { + if(msg.msg == 'markerupdated') { + var marker = dynmapmarkersets[msg.set].markers[msg.id]; + if(marker && 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; + createMarker(dynmapmarkersets[msg.set], marker); + } + else if(msg.msg == 'markerdeleted') { + var marker = dynmapmarkersets[msg.set].markers[msg.id]; + if(marker && marker.our_marker) { + dynmapmarkersets[msg.set].layergroup.removeLayer(marker.our_marker); + } + delete dynmapmarkersets[msg.set].markers[msg.id]; + } + else if(msg.msg == 'setupdated') { + if(!dynmapmarkersets[msg.id]) { + dynmapmarkersets[msg.id] = { label: msg.label, markers:{} }; + createMarkerSet(dynmapmarkersets[msg.id]); + } + else { + if(dynmapmarkersets[msg.id].label != msg.label) { + dynmapmarkersets[msg.id].label = msg.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]) { + dynmap.layercontrol.removeLayer(dynmapmarkersets[msg.id].layergroup); + delete dynmapmarkersets[msg.id].layergroup; + delete dynmapmarkersets[msg.id]; + } + } + }); + + // Remove marker on start of map change + $(dynmap).bind('mapchanging', function(event) { + $.each(dynmapmarkersets, function(setname, set) { + $.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) { + $.each(dynmapmarkersets, function(setname, set) { + $.each(set.markers, function(mname, marker) { + var marker = set.markers[mname]; + var markerPosition = getPosition(marker); + marker.our_marker.setLatLng(markerPosition); + if(dynmap.map.hasLayer(marker.our_marker) == false) + set.layergroup.addLayer(marker.our_marker); + }); + }); + }); + // Load markers for new world + $(dynmap).bind('worldchanged', function(event) { + loadmarkers(this.world.name); + }); + + loadmarkers(dynmap.world.name); + }; \ No newline at end of file