Last active
August 29, 2015 13:56
-
-
Save underbluewaters/9217216 to your computer and use it in GitHub Desktop.
Mapbox code review
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset=utf-8 /> | |
<title></title> | |
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> | |
<script src='https://api.tiles.mapbox.com/mapbox.js/v1.6.1/mapbox.js'></script> | |
<link href='https://api.tiles.mapbox.com/mapbox.js/v1.6.1/mapbox.css' rel='stylesheet' /> | |
<style> | |
body { margin:0; padding:0; } | |
#map { position:absolute; top:0; bottom:0; width:100%; } | |
</style> | |
</head> | |
<body> | |
<style> | |
#map-ui { | |
position: absolute; | |
top: 10px; | |
right: 10px; | |
z-index: 100; | |
} | |
#map-ui ul { | |
list-style: none; | |
margin: 0; | |
padding: 0; | |
} | |
#map-ui a { | |
font-size: 13px; | |
background: #FFF; | |
color: #3C4E5A; | |
display: block; | |
margin: 0; | |
padding: 0; | |
border: 1px solid #BBB; | |
border-bottom-width: 0; | |
min-width: 138px; | |
padding: 10px; | |
text-decoration: none; | |
} | |
#map-ui a:hover { | |
background: #ECF5FA; | |
} | |
#map-ui li:last-child a { | |
border-bottom-width: 1px; | |
-webkit-border-radius: 0 0 3px 3px; | |
border-radius: 0 0 3px 3px; | |
} | |
#map-ui li:first-child a { | |
-webkit-border-radius: 3px 3px 0 0; | |
border-radius: 3px 3px 0 0; | |
} | |
#map-ui a.active { | |
background: #3887BE; | |
border-color: #3887BE; | |
color: #FFF; | |
} | |
</style> | |
<div id='map-ui'> | |
<ul class="layers"> | |
</ul> | |
</div> | |
<div id='map'></div> | |
<script type='text/javascript'> | |
var map = L.mapbox.map('map', 'globalni.h4had364') | |
.setView([34.99, 37.782], 7); | |
var URLLayer = L.mapbox.markerLayer() | |
.loadURL('https://dl.dropboxusercontent.com/u/15138025/Incidents_select.geojson') | |
.addTo(map); | |
// Roll your own configuration tool whenever possible | |
var layers = [ | |
{label: "Regime Army", filterProp: 'Organization', value: "Regime Army"}, | |
{label: "Regime Air Force", filterProp: 'Organization', value: "Regime Air Force"}, | |
{label: "Regime Intelligence and Security Services", filterProp: 'Organization', value: "Regime - Intelligence and Security Services"}, | |
{label: "Terrorist Insurgents", filterProp: 'Organization', value: "Terrorist Insurgents"}, | |
{label: "Date - 1/5", filterProp: 'Date', value: '2014-01-05'}, | |
{label: "All Layers", filterProp: 'None'} | |
]; | |
function evalFilters(filters, f){ | |
for(var i=0; i<filters.length; i++){ | |
if(f.properties[filters[i].filterProp] === filters[i].value) { | |
return true; | |
} | |
} | |
return false; | |
} | |
function getActiveFilters(){ | |
return $('ul li a.active').map(function(){ | |
var i = $(this).attr('data-layer-index'); | |
return layers[i]; | |
}); | |
} | |
// document.ready is standard boilerplate to wait until the page loads | |
// before adding elements to it | |
$(document).ready(function(){ | |
for(var i=0; i<layers.length; i++){ | |
var layer = layers[i]; | |
// Create the list item, with appropriate label and a reference | |
// to the layers' location in the layers array | |
$('ul.layers').append('<li><a data-layer-index="'+i+'" href="#">'+layer.label+'</a></li>'); | |
} | |
// Highlight the "All" layer, since by default it's active because | |
// there are no filter in place | |
$('ul.layers li').last().find('a').addClass('active'); | |
// Setup an event listener that handles clicks on *all* layers | |
$('ul.layers li a').click(function(e){ | |
// prevent changing url to /# | |
e.preventDefault(); | |
// get a jquery-ified reference to the link | |
$el = $(e.target); | |
// get config of appropriate "layer" | |
var layerIndex = $(e.target).attr('data-layer-index'); | |
var layer = layers[layerIndex]; | |
console.log('layer clicked', layer); | |
if (layer.filterProp === "None") { | |
// special case, clear filters | |
$('ul.layers .active').removeClass('active'); | |
$el.addClass('active'); | |
} else { | |
// turn off "all" | |
$('ul li a').last().removeClass('active'); | |
// toggle the layer on or off | |
$el.toggleClass('active'); | |
} | |
filters = getActiveFilters(); | |
URLLayer.setFilter(function(f){ | |
return evalFilters(filters, f); | |
}); | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment