mapmakr/mapmakr/static/js/mapmakr.js

173 lines
5.8 KiB
JavaScript

/* */
var map;
var drawnItems;
var statsControl;
var drawControl;
// http://stackoverflow.com/a/18324384/661150
function callAjax(url, callback)
{
var xmlhttp;
// compatible with IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function(){
if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
callback(xmlhttp.responseText);
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
}
function gitmap_reload() {
callAjax("read.cgi", (function(req) {
var map = window.map;
drawnItems.clearLayers();
var objects = JSON.parse(req).objects;
var num_objects = objects.length;
for (var i=0; i<num_objects; i++) {
var gitmap_file = objects[i].file;
var gitmap_object = objects[i].contents;
var marker = L.marker(gitmap_object.location);
for (var opt in Object.keys(gitmap_object.options)) {
marker.options[opt] = gitmap_object.options[opt];
}
var popup = '';
if (gitmap_file.match(/^office/i)) {
marker.options.icon = L.AwesomeMarkers.icon({icon: 'building', prefix: 'fa', markerColor: 'red'});
popup = gitmap_file;
} else {
marker.options.icon = L.AwesomeMarkers.icon({icon: 'person', prefix: 'ion'});
text1 = '<a href="http://rover.redhat.com/people/profile/'+gitmap_file+'" target="_blank">roster</a>';
text2 = '';
popup = gitmap_file+' '+text1+' '+text2;
}
marker.options.id=gitmap_file;
marker.bindLabel(gitmap_file, { noHide: true });
marker.bindPopup(popup);
drawnItems.addLayer(marker);
}
if (drawControl == null) {
// add drawControl only after some drawnItems exist, so
// it is not shown disabled
drawControl = new L.Control.Draw({
draw: {
position: 'topleft',
polygon: false,
rectangle: false,
polyline: false,
circle: false
},
edit: {
featureGroup: drawnItems
}
});
map.addControl(drawControl);
}
}));
callAjax("summary.cgi", function(req) {
var sc = window.statsControl;
var leaflet = '<a href="http://leafletjs.com">Leaflet</a>';
sc.setPrefix (req + ' | ' + leaflet);
});
}
function gitmap_setup() {
var osmUrl = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
osmAttrib = '&copy; <a href="http://openstreetmap.org/copyright">OpenStreetMap</a> contributors',
osm = L.tileLayer(osmUrl, {maxZoom: 18, attribution: osmAttrib});
map = new L.Map('map', {layers: [osm], attributionControl: false, center: new L.LatLng(0, 0), zoom: 2 });
drawnItems = new L.MarkerClusterGroup({maxClusterRadius: 45}); // new L.FeatureGroup();
map.addLayer(drawnItems);
gitmap_reload();
// drawcontrol formerly added here
var refreshButton = new L.easyButton('icon ion-refresh', function() {gitmap_reload();});
refreshButton.button.style.fontSize = '18px';
map.addControl (refreshButton);
var helpButton = new L.easyButton('icon ion-help-circled', function() {open("about.html");});
helpButton.button.style.fontSize = '18px';
map.addControl (helpButton);
var controlSearch = new L.Control.Search({layer: drawnItems,
propertyName: 'id',
initial: false,
zoom: 12});
map.addControl( controlSearch );
statsControl = new L.control.attribution({position:'bottomright', prefix:''});
map.addControl(statsControl);
scaleControl = new L.control.scale();
map.addControl(scaleControl);
terminator = new L.terminator();
terminator.options.opacity=0.2;
terminator.options.fillOpacity=0.2;
terminator.addTo(map);
setInterval(function() {updateTerminator(terminator);}, 60000); // 1 minute
function updateTerminator(t) {
var t2 = L.terminator();
t.setLatLngs(t2.getLatLngs());
t.redraw();
}
// called after new marker is created
map.on('draw:created', function (e) {
var type = e.layerType,
layer = e.layer;
if (type !== 'marker') return;
var userid = prompt("Marker name (e.g., kerberos userid):", "");
if (userid != null) {
layer.options.id=userid;
layer.bindPopup(userid);
drawnItems.addLayer(layer);
callAjax("write.cgi?op=new"+
"&id="+encodeURIComponent(userid)+
"&location="+encodeURIComponent(JSON.stringify(layer.getLatLng()))+
"&options="+encodeURIComponent("{}"),
function(req){alert("response:\n"+req);
gitmap_reload();});
}
});
map.on('draw:edited', function (e) {
e.layers.eachLayer(function (layer) {
var type = layer.layerType;
var userid = layer.options.id;
callAjax("write.cgi?op=edit"+
"&id="+encodeURIComponent(userid)+
"&location="+encodeURIComponent(JSON.stringify(layer.getLatLng()))+
"&options="+encodeURIComponent("{}"),
function(req){alert("response:\n"+req);
gitmap_reload();});
});
});
map.on('draw:deleted', function (e) {
e.layers.eachLayer(function (layer) {
var type = layer.layerType;
var userid = layer.options.id;
callAjax("write.cgi?op=delete"+
"&id="+encodeURIComponent(userid),
function(req){alert("response:\n"+req);
gitmap_reload();});
});
});
}