Select which key entries to show on the client

Rather than requesting a new key every time the zoom or layer
changes, serve up all the entries initially with appropriate
annotations to allow the client to decide which ones to show.
This commit is contained in:
Tom Hughes 2012-08-31 10:29:32 +01:00
parent 51f5984c1c
commit 815c98c977
4 changed files with 27 additions and 19 deletions

View file

@ -4,13 +4,25 @@ $(document).ready(function () {
title = $(this).text(); title = $(this).text();
function updateMapKey() { function updateMapKey() {
$("#sidebar_content").load(url, { var mapLayer = map.baseLayer.keyid,
layer: map.baseLayer.keyid, mapZoom = map.getZoom();
zoom: map.getZoom()
$(".mapkey-table-entry").each(function () {
var entryLayer = $(this).attr("data-layer"),
entryZoomMin = parseInt($(this).attr("data-zoom-min")),
entryZoomMax = parseInt($(this).attr("data-zoom-max"));
if (mapLayer == entryLayer &&
mapZoom >= entryZoomMin && mapZoom <= entryZoomMax) {
$(this).show();
} else {
$(this).hide();
}
}); });
} }
updateMapKey(); $("#sidebar_content").load(url, updateMapKey);
openSidebar({ title: title }); openSidebar({ title: title });
$("#sidebar").one("closed", function () { $("#sidebar").one("closed", function () {

View file

@ -1,19 +1,15 @@
<div id="mapkey"> <div id="mapkey">
<table class="mapkey-table"> <table class="mapkey-table">
<% YAML.load_file("#{Rails.root}/config/key.yml").each do |name,data| %> <% YAML.load_file("#{Rails.root}/config/key.yml").each do |name,data| %>
<% if params[:layer] == name %> <% data.each do |entry| %>
<% data.each do |entry| %> <tr class="mapkey-table-entry" data-layer="<%= name %>" data-zoom-min="<%= entry['min_zoom'] %>" data-zoom-max="<%= entry['max_zoom'] %>">
<% if params[:zoom].to_i >= entry['min_zoom'] && params[:zoom].to_i <= entry['max_zoom'] %> <td class="mapkey-table-key">
<tr> <%= image_tag "key/#{name}/#{entry['image']}" %>
<td class="mapkey-table-key"> </td>
<%= image_tag "key/#{name}/#{entry['image']}" %> <td class="mapkey-table-value">
</td> <%= [*t("site.key.table.entry.#{entry['name']}")].to_sentence %>
<td class="mapkey-table-value"> </td>
<%= [*t("site.key.table.entry.#{entry['name']}")].to_sentence %> </tr>
</td>
</tr>
<% end %>
<% end %>
<% end %> <% end %>
<% end %> <% end %>
</table> </table>

View file

@ -106,7 +106,7 @@ OpenStreetMap::Application.routes.draw do
match '/login' => 'user#login', :via => [:get, :post] match '/login' => 'user#login', :via => [:get, :post]
match '/logout' => 'user#logout', :via => [:get, :post] match '/logout' => 'user#logout', :via => [:get, :post]
match '/offline' => 'site#offline', :via => :get match '/offline' => 'site#offline', :via => :get
match '/key' => 'site#key', :via => :post match '/key' => 'site#key', :via => :get
match '/user/new' => 'user#new', :via => :get match '/user/new' => 'user#new', :via => :get
match '/user/terms' => 'user#terms', :via => [:get, :post] match '/user/terms' => 'user#terms', :via => [:get, :post]
match '/user/save' => 'user#save', :via => :post match '/user/save' => 'user#save', :via => :post

View file

@ -47,7 +47,7 @@ class SiteControllerTest < ActionController::TestCase
{ :controller => "site", :action => "offline" } { :controller => "site", :action => "offline" }
) )
assert_routing( assert_routing(
{ :path => "/key", :method => :post }, { :path => "/key", :method => :get },
{ :controller => "site", :action => "key" } { :controller => "site", :action => "key" }
) )
assert_routing( assert_routing(