Simplify geolink-related code

#zoom/lat/lon hash format is now used consistently.
This commit is contained in:
John Firebaugh 2013-10-02 14:46:00 -07:00
parent 2f076d8f34
commit d414ffc61a
6 changed files with 26 additions and 46 deletions

View file

@ -28,13 +28,6 @@ function zoomPrecision(zoom) {
return Math.max(0, Math.ceil(Math.log(zoom) / Math.LN2));
}
function normalBounds(bounds) {
if (bounds instanceof L.LatLngBounds) return bounds;
return new L.LatLngBounds(
new L.LatLng(bounds[0][0], bounds[0][1]),
new L.LatLng(bounds[1][0], bounds[1][1]));
}
function remoteEditHandler(bbox, select) {
var loaded = false,
query = {
@ -69,31 +62,28 @@ function remoteEditHandler(bbox, select) {
* Called as the user scrolls/zooms around to maniplate hrefs of the
* view tab and various other links
*/
function updatelinks(loc, zoom, layers, bounds, object) {
function updatelinks(loc, zoom, layers, object) {
$(".geolink").each(function(index, link) {
var href = link.href.split(/[?#]/)[0],
args = querystring.parse(link.search.substring(1));
if (bounds && $(link).hasClass("bbox")) args.bbox = normalBounds(bounds).toBBoxString();
if (object && $(link).hasClass("object")) args[object.type] = object.id;
var query = querystring.stringify(args);
if (query) href += '?' + query;
if ($(link).hasClass("llz")) {
args = {
lat: loc.lat,
lon: loc.lon || loc.lng,
zoom: zoom
};
args = {
lat: loc.lat,
lon: loc.lon || loc.lng,
zoom: zoom
};
if (layers && $(link).hasClass("layers")) {
args.layers = layers;
}
href += OSM.formatHash(args);
if (layers && $(link).hasClass("layers")) {
args.layers = layers;
}
href += OSM.formatHash(args);
link.href = href;
});

View file

@ -139,7 +139,6 @@ $(document).ready(function () {
map.getCenter().wrap(),
map.getZoom(),
map.getLayersCode(),
map.getBounds().wrap(),
map._object);
var expiry = new Date();

View file

@ -190,7 +190,7 @@ function initializeNotes(map, params) {
notes[feature.properties.id] = updateMarker(marker, feature);
newNote = null;
addNoteButton.removeClass("active").addClass("geolink");
addNoteButton.removeClass("active");
}
}
@ -226,7 +226,7 @@ function initializeNotes(map, params) {
if (addNoteButton.hasClass("disabled")) return;
if (addNoteButton.hasClass("active")) return;
addNoteButton.removeClass("geolink").addClass("active");
addNoteButton.addClass("active");
map.addLayer(noteLayer);
@ -263,7 +263,7 @@ function initializeNotes(map, params) {
newNote.addTo(noteLayer).bindPopup(popupContent[0], popupOptions()).openPopup();
newNote.on("remove", function (e) {
addNoteButton.removeClass("active").addClass("geolink");
addNoteButton.removeClass("active");
}).on("dragstart", function (e) {
$(newNote).stopTime("removenote");
}).on("dragend", function (e) {

View file

@ -1,6 +1,6 @@
<header>
<h1>
<a href="<% root_path %>" class="geolink llz layers">
<a href="<% root_path %>" class="geolink layers">
<%= image_tag "osm_logo.png", :alt => t('layouts.logo.alt_text'), :class => 'logo' %>
<%= t 'layouts.project_name.h1' %>
</a>
@ -8,9 +8,9 @@
<nav class='primary'>
<ul>
<li id="view_tab" class="<%= current_page_class(root_path) %>">
<%= link_to t('layouts.view'), root_path, :class => 'tab geolink llz layers' %>
<%= link_to t('layouts.view'), root_path, :class => 'tab geolink layers' %>
</li><li id="edit_tab" class="dropdown <%= current_page_class(edit_path) %>">
<%= link_to t('layouts.edit'), edit_path, :class => "tab geolink llz object",
<%= link_to t('layouts.edit'), edit_path, :class => "tab geolink object",
:id => 'editanchor',
:data => { :editor => preferred_editor }
%><a class='dropdown-toggle' data-toggle='dropdown' href='#'><b class="caret"></b></a>
@ -20,14 +20,14 @@
<%= link_to t('layouts.edit_with', :editor => t("editor.#{editor}.description")),
edit_path(:editor => editor),
:data => { :editor => editor },
:class => "geolink llz object" %>
:class => "geolink object" %>
</li>
<% end %>
</ul>
</li><li id="history_tab" class="<%= current_page_class(browse_changesets_path) %>">
<%= link_to t('layouts.history'), browse_changesets_path, :class => 'tab geolink llz' %>
<%= link_to t('layouts.history'), browse_changesets_path, :class => 'tab geolink' %>
</li><li id="export_tab" class="<%= current_page_class(export_path) %>">
<%= link_to t('layouts.export'), export_path, :class => 'tab geolink llz' %>
<%= link_to t('layouts.export'), export_path, :class => 'tab geolink' %>
</li>
</ul>
</nav>

View file

@ -87,8 +87,8 @@
});
});
var mapMoved = $.throttle(250, function(lon, lat, zoom, minlon, minlat, maxlon, maxlat) {
updatelinks({ lon: lon, lat: lat }, zoom, null, [[minlat, minlon], [maxlat, maxlon]]);
var mapMoved = $.throttle(250, function(lon, lat, zoom) {
updatelinks({ lon: lon, lat: lat }, zoom);
var hash = OSM.formatHash({ lon: lon, lat: lat, zoom: zoom });
if (hash !== location.hash) {

View file

@ -33,24 +33,15 @@
});
id.map().on('move.embed', parent.$.throttle(250, function() {
var extent = id.map().extent(),
zoom = ~~id.map().zoom(),
center = id.map().center();
var zoom = ~~id.map().zoom(),
center = id.map().center(),
llz = { lon: center[0], lat: center[1], zoom: zoom };
parent.updatelinks({
lon: center[0],
lat: center[1]
},
zoom,
null,
[[extent[0][1],
extent[0][0]],
[extent[1][1],
extent[1][0]]]);
parent.updatelinks(llz, zoom);
// Manually resolve URL to avoid iframe JS context weirdness.
// http://bl.ocks.org/jfirebaugh/5439412
var hash = parent.OSM.formatHash({ lon: center[0], lat: center[1], zoom: zoom });
var hash = parent.OSM.formatHash(llz);
if (hash !== parent.location.hash) {
parent.location.replace(parent.location.href.replace(/(#.*|$)/, hash));
}