Skip to content
Browse files

Add Atom feed (+ in-page link) for issues on the browse area page

This dynamically updates the link when scrolling the map
  • Loading branch information...
1 parent 588a7b3 commit da81acd96af71c9580a4e8a1193c6d4d34b043bf @mhl committed
View
192 app/controllers/problems_controller.rb
@@ -7,7 +7,8 @@ class ProblemsController < ApplicationController
:find_train_route,
:find_ferry_route,
:find_other_route,
- :browse]
+ :browse,
+ :atom_link]
before_filter :find_visible_problem, :only => [:show, :update, :add_comment]
before_filter :require_problem_reporter, :only => [:convert]
skip_before_filter :require_beta_password, :only => [:frontpage]
@@ -376,6 +377,15 @@ def find_ferry_route
def choose_location
end
+ def atom_link
+ @issues_feed_title = atom_feed_title @lon, @lat
+ @issues_feed_params = params.clone
+ @issues_feed_params['action'] = 'browse'
+ @issues_feed_params['format'] = 'atom'
+ render :partial => 'shared/atom_link',
+ :locals => { :feed_link_text => t('problems.browse.feed_link_text') }
+ end
+
def browse
if params[:geolocate] == '1'
@geolocate_on_load = true
@@ -430,9 +440,16 @@ def find_nearest_stop(lon, lat, transport_mode_name)
end
end
+ def atom_feed_title(lon, lat)
+ t('problems.browse.feed_title',
+ :longitude => lon,
+ :latitude => lat)
+ end
+
def find_area(options)
@map_height = options[:map_height]
@map_width = options[:map_width]
+ to_render = nil
if is_valid_lon_lat?(params[:lon], params[:lat])
lat = params[:lat].to_f
lon = params[:lon].to_f
@@ -442,100 +459,107 @@ def find_area(options)
@geolocate_on_load = false
# don't show geolocate button
@geolocation_failed = true
- render options[:find_template]
- return
- end
- nearest_stop = find_nearest_stop(params[:lon], params[:lat], nil)
- if nearest_stop
- map_params_from_location([nearest_stop],
- find_other_locations=true,
- @map_height,
- @map_width,
- options[:map_options])
- @locations = [nearest_stop]
- render options[:browse_template]
- return
- else # no nearest stop suggests empty database
- location_search.fail
- @error_message = t('problems.find_stop.please_enter_an_area')
- end
- elsif params[:name]
- if params[:name].blank?
- @error_message = t('problems.find_stop.please_enter_an_area')
- render options[:find_template]
- return
- end
- location_search = LocationSearch.new_search!(session_id, :name => params[:name],
- :location_type => 'Stop/station')
- stop_info = Gazetteer.place_from_name(params[:name], params[:stop_name], options[:map_options][:mode])
- # got back localities
- if stop_info[:localities]
- if stop_info[:localities].size > 1
- @localities = stop_info[:localities]
- @matched_stops_or_stations = stop_info[:matched_stops_or_stations]
- @name = params[:name]
- render :choose_locality
- return
- else
- return render_browse_template(stop_info[:localities], options[:map_options], options[:browse_template])
- end
- # got back district
- elsif stop_info[:district]
- return render_browse_template([stop_info[:district]], options[:map_options], options[:browse_template])
- # got back admin area
- elsif stop_info[:admin_area]
- return render_browse_template([stop_info[:admin_area]], options[:map_options], options[:browse_template])
- # got back stops/stations
- elsif stop_info[:locations]
- if options[:map_options][:mode] == :browse
- return render_browse_template(stop_info[:locations], options[:map_options], options[:browse_template])
- else
- map_params_from_location(stop_info[:locations],
+ to_render = options[:find_template]
+ else
+ nearest_stop = find_nearest_stop(params[:lon], params[:lat], nil)
+ if nearest_stop
+ map_params_from_location([nearest_stop],
find_other_locations=true,
@map_height,
@map_width,
options[:map_options])
- @locations = stop_info[:locations]
- render options[:browse_template]
- return
- end
- # got back postcode info
- elsif stop_info[:postcode_info]
- postcode_info = stop_info[:postcode_info]
- if postcode_info[:error]
+ @locations = [nearest_stop]
+ to_render = options[:browse_template]
+ else # no nearest stop suggests empty database
location_search.fail
- if postcode_info[:error] == :area_not_known
- @error_message = t('problems.find_stop.postcode_area_not_known')
- elsif postcode_info[:error] == :service_unavailable
- @error_message = t('problems.find_stop.postcode_service_unavailable')
+ @error_message = t('problems.find_stop.please_enter_an_area')
+ end
+ end
+ elsif params[:name]
+ if params[:name].blank?
+ @error_message = t('problems.find_stop.please_enter_an_area')
+ to_render = options[:find_template]
+ else
+ location_search = LocationSearch.new_search!(session_id, :name => params[:name],
+ :location_type => 'Stop/station')
+ stop_info = Gazetteer.place_from_name(params[:name], params[:stop_name], options[:map_options][:mode])
+ # got back localities
+ if stop_info[:localities]
+ if stop_info[:localities].size > 1
+ @localities = stop_info[:localities]
+ @matched_stops_or_stations = stop_info[:matched_stops_or_stations]
+ @name = params[:name]
+ to_render = :choose_locality
else
- @error_message = t('problems.find_stop.postcode_not_found')
+ return render_browse_template(stop_info[:localities], options[:map_options], options[:browse_template])
+ end
+ # got back district
+ elsif stop_info[:district]
+ return render_browse_template([stop_info[:district]], options[:map_options], options[:browse_template])
+ # got back admin area
+ elsif stop_info[:admin_area]
+ return render_browse_template([stop_info[:admin_area]], options[:map_options], options[:browse_template])
+ # got back stops/stations
+ elsif stop_info[:locations]
+ if options[:map_options][:mode] == :browse
+ return render_browse_template(stop_info[:locations], options[:map_options], options[:browse_template])
+ else
+ map_params_from_location(stop_info[:locations],
+ find_other_locations=true,
+ @map_height,
+ @map_width,
+ options[:map_options])
+ @locations = stop_info[:locations]
+ to_render = options[:browse_template]
+ end
+ # got back postcode info
+ elsif stop_info[:postcode_info]
+ postcode_info = stop_info[:postcode_info]
+ if postcode_info[:error]
+ location_search.fail
+ if postcode_info[:error] == :area_not_known
+ @error_message = t('problems.find_stop.postcode_area_not_known')
+ elsif postcode_info[:error] == :service_unavailable
+ @error_message = t('problems.find_stop.postcode_service_unavailable')
+ else
+ @error_message = t('problems.find_stop.postcode_not_found')
+ end
+ to_render = options[:find_template]
+ else
+ @lat = postcode_info[:lat] unless @lat
+ @lon = postcode_info[:lon] unless @lon
+ @zoom = postcode_info[:zoom] unless @zoom
+ map_data = Map.other_locations(@lat, @lon, @zoom, @map_height, @map_width, @highlight)
+ @other_locations = map_data[:locations]
+ @issues_on_map = map_data[:issues]
+ @nearest_issues = map_data[:nearest_issues]
+ @distance = map_data[:distance]
+ @locations = []
+ @find_other_locations = true
+ to_render = options[:browse_template]
end
- render options[:find_template]
- return
else
- @lat = postcode_info[:lat] unless @lat
- @lon = postcode_info[:lon] unless @lon
- @zoom = postcode_info[:zoom] unless @zoom
- map_data = Map.other_locations(@lat, @lon, @zoom, @map_height, @map_width, @highlight)
- @other_locations = map_data[:locations]
- @issues_on_map = map_data[:issues]
- @nearest_issues = map_data[:nearest_issues]
- @distance = map_data[:distance]
- @locations = []
- @find_other_locations = true
- render options[:browse_template]
- return
+ # didn't find anything
+ location_search.fail
+ @error_message = t('problems.find_stop.area_not_found')
+ to_render = options[:find_template]
end
- else
- # didn't find anything
- location_search.fail
- @error_message = t('problems.find_stop.area_not_found')
- render options[:find_template]
- return
end
end
-
+ respond_to do |format|
+ format.html do
+ @issues_feed_params = params.clone
+ @issues_feed_params[:format] = 'atom'
+ render to_render if to_render
+ end
+ format.atom do
+ @title = atom_feed_title @lon, @lat
+ @issues = []
+ @issues.concat(@issues_on_map) if @issues_on_map
+ @issues.concat(@nearest_issues) if @nearest_issues
+ render :template => 'shared/issues.atom.builder', :layout => false
+ end
+ end
end
def is_valid_lon_lat?(lon, lat)
View
1 app/views/problems/browse_area.erb
@@ -7,6 +7,7 @@
<div id="main-content" class="browse-map-container container">
<div class="leftcol mediumnarrowcol">
+ <%= render :partial => 'shared/atom_link', :locals => { :feed_link_text => t('problems.browse.feed_link_text') } %>
<div id="issues-in-area">
<%= render :partial => 'shared/issues_in_area'%>
</div>
View
2 config/locales/views/problems/en.yml
@@ -8,6 +8,8 @@ en:
title: "Issues in '%{area}'"
no_other_issues: No issues have been reported yet.
title_no_name: "Browsing issues"
+ feed_title: "Issues around longitude %{longitude} and latitude %{latitude} on FixMyTransport"
+ feed_link_text: "Get updates on issues in this area"
choose_area:
choose_area: "Choose area"
multiple_localities: "We found more than one place matching \"%{name}\". Please select one, or try a different search if yours is not here:"
View
7 config/routes.rb
@@ -72,6 +72,13 @@
map.issues '/issues', :action => 'issues_index', :controller => 'problems'
map.browse_issues '/issues/browse', :action => 'browse', :controller => 'problems'
+ map.browse_issues_atom_link '/issues/browse/atom_link/:zoom/:lat/:lon', :action => 'atom_link',
+ :controller => 'problems',
+ :conditions => { :method => :get },
+ :requirements => { :zoom => /\d\d?/,
+ :lon => /[-+]?[0-9]*\.?[0-9]+/,
+ :lat => /[-+]?[0-9]*\.?[0-9]+/ }
+
# stops
map.add_comment_stop "/stops/:scope/:id/add_comment", :controller => "locations",
:action => 'add_comment_to_stop',
View
18 public/javascripts/map.js
@@ -169,6 +169,14 @@ var area_init, route_init;
// do nothing
}
+ function replaceAtomLink(data, textStatus, jqXHR) {
+ $('#in-page-atom-link').replaceWith(data);
+ }
+
+ function replaceAtomLinkFailure(jqXHR, textStatus, errorThrown) {
+ // ignore errors here; there's nothing much that can be done
+ }
+
function getQueryStringParametersMap() {
// Based on: http://stackoverflow.com/a/3855394/223092
var result = {}, i, value, parts;
@@ -190,7 +198,7 @@ var area_init, route_init;
function updateLocations(eevent) {
var currentZoom = map.getZoom(), newLat, newLon, newPath, url;
- var parameters, key, center;
+ var parameters, key, center, mapViewParameters;
var positionKeys = {'lon': true,
'lat': true,
'zoom': true};
@@ -213,7 +221,8 @@ var area_init, route_init;
center = center.transform(map.getProjectionObject(), proj);
newLat = Math.round(center.lat*1000)/1000;
newLon = Math.round(center.lon*1000)/1000;
- url = "/locations/" + map.getZoom() + "/" + newLat + "/" + newLon + "/" + linkType;
+ mapViewParameters = map.getZoom() + "/" + newLat + "/" + newLon;
+ url = "/locations/" + mapViewParameters + "/" + linkType;
parameters = "?height=" + $('#map').height() + "&width=" + $('#map').width();
parameters = parameters + "&highlight=" + highlight;
$.ajax({
@@ -221,6 +230,11 @@ var area_init, route_init;
dataType: 'json',
success: loadNewMarkers,
failure: markerFail});
+ $.ajax({
+ url: "/issues/browse/atom_link/" + mapViewParameters,
+ dataType: 'html',
+ success: replaceAtomLink,
+ failure: replaceAtomLinkFailure});
// If we're able to replace the URL with history.replaceState,
// update it to give a permalink to the new map position:
if (history.replaceState) {

0 comments on commit da81acd

Please sign in to comment.
Something went wrong with that request. Please try again.