diff options
author | Matthew Somerville <matthew-github@dracos.co.uk> | 2017-08-17 15:50:07 +0100 |
---|---|---|
committer | Matthew Somerville <matthew-github@dracos.co.uk> | 2017-08-17 16:43:21 +0100 |
commit | d034f3c1447ea91cb4333365ce123d58deb2de0d (patch) | |
tree | 1c7e9eab5b32ecbc35bb4886ea45b4d84ee88578 | |
parent | 676d755848465c6fec01243fd6c7df7e249d5c2b (diff) |
Refactor /around list code to share with others.
Both /reports and /my share an ID and a /reports/ajax function, use
these also on /around (and share ajax/non-ajax code).
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Around.pm | 126 | ||||
-rw-r--r-- | perllib/FixMyStreet/App/Controller/Report.pm | 2 | ||||
-rwxr-xr-x | templates/web/base/around/tabbed_lists.html | 2 | ||||
-rw-r--r-- | web/cobrands/fixmystreet/staff.js | 2 | ||||
-rw-r--r-- | web/js/map-OpenLayers.js | 4 | ||||
-rw-r--r-- | web/js/map-google.js | 9 |
6 files changed, 50 insertions, 95 deletions
diff --git a/perllib/FixMyStreet/App/Controller/Around.pm b/perllib/FixMyStreet/App/Controller/Around.pm index a8782eba2..4a82c67cc 100644 --- a/perllib/FixMyStreet/App/Controller/Around.pm +++ b/perllib/FixMyStreet/App/Controller/Around.pm @@ -163,52 +163,20 @@ sub display_location : Private { $c->forward('/auth/get_csrf_token'); - # get the lat,lng - my $latitude = $c->stash->{latitude}; - my $longitude = $c->stash->{longitude}; - - # Deal with pin hiding/age - my $all_pins = $c->get_param('all_pins') ? 1 : undef; - $c->stash->{all_pins} = $all_pins; - my $interval = $all_pins ? undef : $c->cobrand->on_map_default_max_pin_age; - - $c->forward( '/reports/stash_report_filter_status' ); - # Check the category to filter by, if any, is valid $c->forward('check_and_stash_category'); - $c->forward( '/reports/stash_report_sort', [ 'created-desc' ]); - - # get the map features - my ( $on_map_all, $on_map, $nearby, $distance ) = - FixMyStreet::Map::map_features( $c, - latitude => $latitude, longitude => $longitude, - interval => $interval, categories => [ keys %{$c->stash->{filter_category}} ], - states => $c->stash->{filter_problem_states}, - order => $c->stash->{sort_order}, - ); - # copy the found reports to the stash - $c->stash->{on_map} = $on_map; - $c->stash->{around_map} = $nearby; - $c->stash->{distance} = $distance; + my $latitude = $c->stash->{latitude}; + my $longitude = $c->stash->{longitude}; - # create a list of all the pins - my @pins; - unless ($c->get_param('no_pins')) { - @pins = map { - # Here we might have a DB::Problem or a DB::Result::Nearby, we always want the problem. - my $p = (ref $_ eq 'FixMyStreet::DB::Result::Nearby') ? $_->problem : $_; - $p->pin_data($c, 'around'); - } @$on_map_all, @$nearby; - } + $c->forward('map_features', [ { latitude => $latitude, longitude => $longitude } ] ); - $c->stash->{page} = 'around'; # So the map knows to make clickable pins, update on pan FixMyStreet::Map::display_map( $c, latitude => $latitude, longitude => $longitude, clickable => 1, - pins => \@pins, + pins => $c->stash->{pins}, area => $c->cobrand->areas_on_around, ); @@ -268,6 +236,42 @@ sub check_and_stash_category : Private { $c->stash->{filter_category} = \%valid_categories; } +sub map_features : Private { + my ($self, $c, $extra) = @_; + + $c->stash->{page} = 'around'; # Needed by _item.html / so the map knows to make clickable pins, update on pan + + $c->forward( '/reports/stash_report_filter_status' ); + $c->forward( '/reports/stash_report_sort', [ 'created-desc' ]); + + # Deal with pin hiding/age + my $all_pins = $c->get_param('all_pins') ? 1 : undef; + $c->stash->{all_pins} = $all_pins; + my $interval = $all_pins ? undef : $c->cobrand->on_map_default_max_pin_age; + + my ( $on_map_all, $on_map_list, $nearby, $distance ) = + FixMyStreet::Map::map_features( + $c, interval => $interval, %$extra, + categories => [ keys %{$c->stash->{filter_category}} ], + states => $c->stash->{filter_problem_states}, + order => $c->stash->{sort_order}, + ); + + my @pins; + unless ($c->get_param('no_pins')) { + @pins = map { + # Here we might have a DB::Problem or a DB::Result::Nearby, we always want the problem. + my $p = (ref $_ eq 'FixMyStreet::DB::Result::Nearby') ? $_->problem : $_; + $p->pin_data($c, 'around'); + } @$on_map_all, @$nearby; + } + + $c->stash->{pins} = \@pins; + $c->stash->{on_map} = $on_map_list; + $c->stash->{around_map} = $nearby; + $c->stash->{distance} = $distance; +} + =head2 /ajax Handle the ajax calls that the map makes when it is dragged. The info returned @@ -279,8 +283,6 @@ the map. sub ajax : Path('/ajax') { my ( $self, $c ) = @_; - $c->res->content_type('application/json; charset=utf-8'); - my $bbox = $c->get_param('bbox'); unless ($bbox) { $c->res->status(404); @@ -288,53 +290,13 @@ sub ajax : Path('/ajax') { return; } - # assume this is not cacheable - may need to be more fine-grained later - $c->res->header( 'Cache_Control' => 'max-age=0' ); - - $c->stash->{page} = 'around'; # Needed by _item.html - - # how far back should we go? - my $all_pins = $c->get_param('all_pins') ? 1 : undef; - my $interval = $all_pins ? undef : $c->cobrand->on_map_default_max_pin_age; - - $c->forward( '/reports/stash_report_filter_status' ); - $c->forward( '/reports/stash_report_sort', [ 'created-desc' ]); - - # extract the data from the map - my ( $on_map_all, $on_map_list, $nearby, $dist ) = - FixMyStreet::Map::map_features($c, - bbox => $bbox, interval => $interval, - categories => [ $c->get_param_list('filter_category', 1) ], - states => $c->stash->{filter_problem_states}, - order => $c->stash->{sort_order}, - ); - - # create a list of all the pins - my @pins = map { - # Here we might have a DB::Problem or a DB::Result::Nearby, we always want the problem. - my $p = (ref $_ eq 'FixMyStreet::DB::Result::Nearby') ? $_->problem : $_; - my $colour = $c->cobrand->pin_colour( $p, 'around' ); - my $title = $c->cobrand->call_hook(pin_hover_title => $p, $p->title_safe) || $p->title_safe; - [ $p->latitude, $p->longitude, - $colour, - $p->id, $title - ] - } @$on_map_all, @$nearby; - - # render templates to get the html - my $on_map_list_html = $c->render_fragment( - 'around/on_map_list_items.html', - { on_map => $on_map_list, around_map => $nearby } - ); + my %valid_categories = map { $_ => 1 } $c->get_param_list('filter_category', 1); + $c->stash->{filter_category} = \%valid_categories; - # JSON encode the response - my $json = { pins => \@pins }; - $json->{current} = $on_map_list_html if $on_map_list_html; - my $body = encode_json($json); - $c->res->body($body); + $c->forward('map_features', [ { bbox => $bbox } ]); + $c->forward('/reports/ajax', [ 'around/on_map_list_items.html' ]); } - sub location_autocomplete : Path('/ajax/geocode') { my ( $self, $c ) = @_; $c->res->content_type('application/json; charset=utf-8'); diff --git a/perllib/FixMyStreet/App/Controller/Report.pm b/perllib/FixMyStreet/App/Controller/Report.pm index acc6f9a09..520efa4dc 100644 --- a/perllib/FixMyStreet/App/Controller/Report.pm +++ b/perllib/FixMyStreet/App/Controller/Report.pm @@ -504,7 +504,7 @@ sub nearby_json : Private { ); my $json = { pins => \@pins }; - $json->{current} = $on_map_list_html if $on_map_list_html; + $json->{reports_list} = $on_map_list_html if $on_map_list_html; my $body = encode_json($json); $c->res->content_type('application/json; charset=utf-8'); $c->res->body($body); diff --git a/templates/web/base/around/tabbed_lists.html b/templates/web/base/around/tabbed_lists.html index b0d46444d..5418ef914 100755 --- a/templates/web/base/around/tabbed_lists.html +++ b/templates/web/base/around/tabbed_lists.html @@ -1,5 +1,5 @@ [% INCLUDE "reports/_list-filters.html" %] -<ul id="current" class="item-list item-list--reports"> +<ul id="js-reports-list" class="item-list item-list--reports"> [% INCLUDE "around/on_map_list_items.html" %] </ul> diff --git a/web/cobrands/fixmystreet/staff.js b/web/cobrands/fixmystreet/staff.js index 95fbad81a..ec71ae20f 100644 --- a/web/cobrands/fixmystreet/staff.js +++ b/web/cobrands/fixmystreet/staff.js @@ -13,7 +13,7 @@ $.extend(fixmystreet.set_up, { var nearby_url = '/report/'+report_id+'/nearby.json'; $.getJSON(nearby_url, args, function(data) { var duplicate_of = $("#report_inspect_form [name=duplicate_of]").val(); - var $reports = $(data.current) + var $reports = $(data.reports_list) .filter("li") .not("[data-report-id="+report_id+"]") .slice(0, 5); diff --git a/web/js/map-OpenLayers.js b/web/js/map-OpenLayers.js index e7702e764..53535b146 100644 --- a/web/js/map-OpenLayers.js +++ b/web/js/map-OpenLayers.js @@ -861,10 +861,6 @@ OpenLayers.Format.FixMyStreet = OpenLayers.Class(OpenLayers.Format.JSON, { } else { obj = json; } - var current; - if (typeof(obj.current) != 'undefined' && (current = document.getElementById('current'))) { - current.innerHTML = obj.current; - } var reports_list; if (typeof(obj.reports_list) != 'undefined' && (reports_list = document.getElementById('js-reports-list'))) { reports_list.innerHTML = obj.reports_list; diff --git a/web/js/map-google.js b/web/js/map-google.js index 8819697eb..596e4f8ee 100644 --- a/web/js/map-google.js +++ b/web/js/map-google.js @@ -114,12 +114,9 @@ fixmystreet.maps = {}; /* Pan data handler */ function read_pin_json(obj) { - var current, current_near; - if (typeof(obj.current) != 'undefined' && (current = document.getElementById('current'))) { - current.innerHTML = obj.current; - } - if (typeof(obj.current_near) != 'undefined' && (current_near = document.getElementById('current_near'))) { - current_near.innerHTML = obj.current_near; + var reports_list; + if (typeof(obj.reports_list) != 'undefined' && (reports_list = document.getElementById('js-reports-list'))) { + reports_list.innerHTML = obj.reports_list; } fixmystreet.markers = markers_list( obj.pins, false ); } |