Basic javascript cleanup

Unifies some logic, cleans up style.
This commit is contained in:
Tom MacWright 2012-08-30 12:17:17 -04:00 committed by Tom Hughes
parent d6a8aaa369
commit 93c392cf5f
2 changed files with 37 additions and 44 deletions

View file

@ -12,22 +12,29 @@
//= require menu //= require menu
//= require sidebar //= require sidebar
function zoomPrecision(zoom) {
var decimals = Math.pow(10, Math.floor(zoom/3));
return function(x) {
return Math.round(x * decimals) / decimals;
};
}
/* /*
* Called as the user scrolls/zooms around to aniplate hrefs of the * Called as the user scrolls/zooms around to aniplate hrefs of the
* view tab and various other links * view tab and various other links
*/ */
function updatelinks(lon,lat,zoom,layers,minlon,minlat,maxlon,maxlat,objtype,objid) { function updatelinks(lon,lat,zoom,layers,minlon,minlat,maxlon,maxlat,objtype,objid) {
var decimals = Math.pow(10, Math.floor(zoom/3)); var toPrecision = zoomPrecision(zoom);
var node; var node;
lat = Math.round(lat * decimals) / decimals; lat = toPrecision(lat);
lon = Math.round(lon * decimals) / decimals; lon = toPrecision(lon);
if (minlon) { if (minlon) {
minlon = Math.round(minlon * decimals) / decimals; minlon = toPrecision(minlon);
minlat = Math.round(minlat * decimals) / decimals; minlat = toPrecision(minlat);
maxlon = Math.round(maxlon * decimals) / decimals; maxlon = toPrecision(maxlon);
maxlat = Math.round(maxlat * decimals) / decimals; maxlat = toPrecision(maxlat);
} }
$(".geolink").each(function (index, link) { $(".geolink").each(function (index, link) {

View file

@ -1,9 +1,11 @@
function startExport(sidebarHtml) { function startExport(sidebarHtml) {
var vectors; var vectors,
var box; box,
var transform; transform,
var markerLayer; markerLayer,
var markerControl; markerControl,
epsg4326 = new OpenLayers.Projection("EPSG:4326"),
epsg900913 = new OpenLayers.Projection("EPSG:900913");
vectors = new OpenLayers.Layer.Vector("Vector Layer", { vectors = new OpenLayers.Layer.Vector("Vector Layer", {
displayInLayerSwitcher: false displayInLayerSwitcher: false
@ -34,18 +36,13 @@ function startExport(sidebarHtml) {
$("#sidebar_title").html(I18n.t('export.start_rjs.export')); $("#sidebar_title").html(I18n.t('export.start_rjs.export'));
$("#sidebar_content").html(sidebarHtml); $("#sidebar_content").html(sidebarHtml);
$("#maxlat").change(boundsChanged); $("#maxlat,#minlon,#maxlon,#minlat").change(boundsChanged);
$("#minlon").change(boundsChanged);
$("#maxlon").change(boundsChanged);
$("#minlat").change(boundsChanged);
$("#drag_box").click(startDrag); $("#drag_box").click(startDrag);
$("#add_marker").click(startMarker); $("#add_marker").click(startMarker);
$("#format_osm").click(formatChanged); $("#format_osm,#format_mapnik,#format_html").click(formatChanged);
$("#format_mapnik").click(formatChanged);
$("#format_html").click(formatChanged);
$("#mapnik_scale").change(mapnikSizeChanged); $("#mapnik_scale").change(mapnikSizeChanged);
@ -72,12 +69,15 @@ function startExport(sidebarHtml) {
map.removeLayer(vectors); map.removeLayer(vectors);
} }
function boundsChanged() { function getMercatorBounds() {
var epsg4326 = new OpenLayers.Projection("EPSG:4326");
var bounds = new OpenLayers.Bounds($("#minlon").val(), $("#minlat").val(), var bounds = new OpenLayers.Bounds($("#minlon").val(), $("#minlat").val(),
$("#maxlon").val(), $("#maxlat").val()); $("#maxlon").val(), $("#maxlat").val());
bounds.transform(epsg4326, map.getProjectionObject()); return bounds.transform(epsg4326, epsg900913);
}
function boundsChanged() {
var bounds = getMercatorBounds();
map.events.unregister("moveend", map, mapMoved); map.events.unregister("moveend", map, mapMoved);
map.zoomToExtent(bounds); map.zoomToExtent(bounds);
@ -147,8 +147,6 @@ function startExport(sidebarHtml) {
$("#add_marker").html(I18n.t('export.start_rjs.change_marker')); $("#add_marker").html(I18n.t('export.start_rjs.change_marker'));
$("#marker_inputs").show(); $("#marker_inputs").show();
var epsg4326 = new OpenLayers.Projection("EPSG:4326");
var epsg900913 = new OpenLayers.Projection("EPSG:900913");
var geom = event.feature.geometry.clone().transform(epsg900913, epsg4326); var geom = event.feature.geometry.clone().transform(epsg900913, epsg4326);
$("#marker_lon").val(geom.x.toFixed(5)); $("#marker_lon").val(geom.x.toFixed(5));
@ -158,8 +156,7 @@ function startExport(sidebarHtml) {
} }
function clearMarker() { function clearMarker() {
$("#marker_lon").val(""); $("#marker_lon,#marker_lat").val("");
$("#marker_lat").val("");
$("#marker_inputs").hide(); $("#marker_inputs").hide();
$("#add_marker").html(I18n.t('export.start_rjs.add_marker')); $("#add_marker").html(I18n.t('export.start_rjs.add_marker'));
@ -177,15 +174,14 @@ function startExport(sidebarHtml) {
} }
function setBounds(bounds) { function setBounds(bounds) {
var epsg4326 = new OpenLayers.Projection("EPSG:4326"); var toPrecision = zoomPrecision(map.getZoom());
var decimals = Math.pow(10, Math.floor(map.getZoom() / 3));
bounds = bounds.clone().transform(map.getProjectionObject(), epsg4326); bounds = bounds.clone().transform(map.getProjectionObject(), epsg4326);
$("#minlon").val(Math.round(bounds.left * decimals) / decimals); $("#minlon").val(toPrecision(bounds.left));
$("#minlat").val(Math.round(bounds.bottom * decimals) / decimals); $("#minlat").val(toPrecision(bounds.bottom));
$("#maxlon").val(Math.round(bounds.right * decimals) / decimals); $("#maxlon").val(toPrecision(bounds.right));
$("#maxlat").val(Math.round(bounds.top * decimals) / decimals); $("#maxlat").val(toPrecision(bounds.top));
mapnikSizeChanged(); mapnikSizeChanged();
htmlUrlChanged(); htmlUrlChanged();
@ -240,8 +236,6 @@ function startExport(sidebarHtml) {
// Create "larger map" link // Create "larger map" link
var center = bounds.getCenterLonLat(); var center = bounds.getCenterLonLat();
var epsg4326 = new OpenLayers.Projection("EPSG:4326");
var epsg900913 = new OpenLayers.Projection("EPSG:900913");
bounds.transform(epsg4326, epsg900913); bounds.transform(epsg4326, epsg900913);
var zoom = map.getZoomForExtent(bounds); var zoom = map.getZoomForExtent(bounds);
@ -297,21 +291,13 @@ function startExport(sidebarHtml) {
} }
function maxMapnikScale() { function maxMapnikScale() {
var bounds = new OpenLayers.Bounds($("#minlon").val(), $("#minlat").val(), $("#maxlon").val(), $("#maxlat").val()); var bounds = getMercatorBounds();
var epsg4326 = new OpenLayers.Projection("EPSG:4326");
var epsg900913 = new OpenLayers.Projection("EPSG:900913");
bounds.transform(epsg4326, epsg900913);
return Math.floor(Math.sqrt(bounds.getWidth() * bounds.getHeight() / 0.3136)); return Math.floor(Math.sqrt(bounds.getWidth() * bounds.getHeight() / 0.3136));
} }
function mapnikImageSize(scale) { function mapnikImageSize(scale) {
var bounds = new OpenLayers.Bounds($("#minlon").val(), $("#minlat").val(), $("#maxlon").val(), $("#maxlat").val()); var bounds = getMercatorBounds();
var epsg4326 = new OpenLayers.Projection("EPSG:4326");
var epsg900913 = new OpenLayers.Projection("EPSG:900913");
bounds.transform(epsg4326, epsg900913);
return new OpenLayers.Size(Math.round(bounds.getWidth() / scale / 0.00028), return new OpenLayers.Size(Math.round(bounds.getWidth() / scale / 0.00028),
Math.round(bounds.getHeight() / scale / 0.00028)); Math.round(bounds.getHeight() / scale / 0.00028));