aboutsummaryrefslogtreecommitdiffstats
path: root/web
diff options
context:
space:
mode:
authorMatthew Somerville <matthew-github@dracos.co.uk>2018-08-09 17:02:58 +0100
committerMatthew Somerville <matthew-github@dracos.co.uk>2018-08-28 13:16:39 +0100
commit22c12ab78128788bb44b8567d8735671b73b02e6 (patch)
tree11fa75561358f257accc3f321aec352cf0c6f124 /web
parent60629c93d6f6ebb48ba00e16abffd8df4df79abe (diff)
Remove need for category_extras ajax call.
Add by_category output to the /report/new/ajax call, containing all the data that /report/new/category_extras returns for one category. Then alter the JS to use that data immediately when needed.
Diffstat (limited to 'web')
-rw-r--r--web/cobrands/fixmystreet/assets.js41
-rw-r--r--web/cobrands/fixmystreet/fixmystreet.js70
2 files changed, 68 insertions, 43 deletions
diff --git a/web/cobrands/fixmystreet/assets.js b/web/cobrands/fixmystreet/assets.js
index 280fe001c..c27d19211 100644
--- a/web/cobrands/fixmystreet/assets.js
+++ b/web/cobrands/fixmystreet/assets.js
@@ -19,9 +19,7 @@ var fixmystreet = fixmystreet || {};
OpenLayers.Layer.VectorAsset = OpenLayers.Class(OpenLayers.Layer.Vector, {
initialize: function(name, options) {
OpenLayers.Layer.Vector.prototype.initialize.apply(this, arguments);
- // Do in both locations so fixmystreet.bodies is up to date. Otherwise
- // e.g. a layer can disappear the category change after it should.
- $(fixmystreet).on('report_new:category_change:extras_received', this.update_layer_visibility.bind(this));
+ // Update layer based upon new data from category change
$(fixmystreet).on('report_new:category_change', this.update_layer_visibility.bind(this));
},
@@ -74,9 +72,7 @@ OpenLayers.Layer.VectorNearest = OpenLayers.Class(OpenLayers.Layer.VectorAsset,
OpenLayers.Layer.VectorAsset.prototype.initialize.apply(this, arguments);
$(fixmystreet).on('maps:update_pin', this.checkFeature.bind(this));
$(fixmystreet).on('assets:selected', this.checkFeature.bind(this));
- // Might only be able to fill in fields once they've been returned from the server
- $(fixmystreet).on('report_new:category_change:extras_received', this.changeCategory.bind(this));
- // But also want to do it immediately in case it's hiding the form or something
+ // Update fields/etc from data now available from category change
$(fixmystreet).on('report_new:category_change', this.changeCategory.bind(this));
},
@@ -332,6 +328,13 @@ function check_zoom_message_visibility() {
}
function layer_visibilitychanged() {
+ if (this.fixmystreet.road) {
+ if (!this.getVisibility()) {
+ this.road_not_found();
+ }
+ return;
+ }
+
check_zoom_message_visibility.call(this);
var layers = fixmystreet.map.getLayersBy('assets', true);
var visible = 0;
@@ -624,7 +627,8 @@ fixmystreet.assets = {
}
});
}
- if (!options.always_visible) {
+
+ if (!options.always_visible || options.road) {
asset_layer.events.register( 'visibilitychanged', asset_layer, layer_visibilitychanged);
}
@@ -804,22 +808,25 @@ return {
})();
$(fixmystreet).on('body_overrides:change', function() {
- var councils_text = $('#js-councils_text').html();
+ var single_body_only = $('#single_body_only').val(),
+ do_not_send = $('#do_not_send').val(),
+ bodies = fixmystreet.bodies;
- var single_body_only = $('#single_body_only').val();
if (single_body_only) {
- councils_text = councils_text.replace(/<strong>.*<\/strong>/, '<strong>' + single_body_only + '</strong>');
+ bodies = [ single_body_only ];
}
- var do_not_send = $('#do_not_send').val();
if (do_not_send) {
do_not_send = fixmystreet.utils.csv_to_array(do_not_send);
- for (var i=0; i<do_not_send.length; i++) {
- // XXX Translations
- councils_text = councils_text.replace(new RegExp('or <strong>' + do_not_send[i] + '</strong>'), '');
- councils_text = councils_text.replace(new RegExp('<strong>' + do_not_send[i] + '</strong> or '), '');
- }
+ var lookup = {};
+ $.map(do_not_send, function(val) {
+ lookup[val] = 1;
+ });
+ bodies = OpenLayers.Array.filter(bodies, function(b) {
+ return !lookup[b];
+ });
}
- $('#js-councils_text').html(councils_text);
+ fixmystreet.update_public_councils_text(
+ $('#js-councils_text').html(), bodies);
});
diff --git a/web/cobrands/fixmystreet/fixmystreet.js b/web/cobrands/fixmystreet/fixmystreet.js
index 63f4c16b4..a5267d4f2 100644
--- a/web/cobrands/fixmystreet/fixmystreet.js
+++ b/web/cobrands/fixmystreet/fixmystreet.js
@@ -387,34 +387,42 @@ $.extend(fixmystreet.set_up, {
// On the new report form, does this by asking for details from the server.
// Delegation is necessary because #form_category may be replaced during the lifetime of the page
$("#problem_form").on("change.category", "select#form_category", function(){
- var args = {
- category: $(this).val(),
- latitude: $('input[name="latitude"]').val(),
- longitude: $('input[name="longitude"]').val()
- };
-
- $.getJSON('/report/new/category_extras', args, function(data) {
- var $category_meta = $('#category_meta');
+ // If we haven't got any reporting data (e.g. came straight to
+ // /report/new), fetch it first. That will then automatically call this
+ // function again, due to it calling change() on the category if set.
+ if (!fixmystreet.reporting_data) {
+ fixmystreet.update_pin(fixmystreet.map.getCenter(), false);
+ return;
+ }
+
+ var category = $(this).val(),
+ data = fixmystreet.reporting_data.by_category[category],
+ $category_meta = $('#category_meta');
+
+ fixmystreet.bodies = data.bodies || [];
+ if (fixmystreet.body_overrides) {
+ fixmystreet.body_overrides.clear();
+ }
+
+ if (data.councils_text) {
fixmystreet.update_councils_text(data);
- if ( data.category_extra ) {
- if ( $category_meta.length ) {
- $category_meta.replaceWith( data.category_extra );
- // Preserve any existing values
- $category_meta.find("[name]").each(function() {
- $('#category_meta').find("[name="+this.name+"]").val(this.value);
- });
- } else {
- $('#form_category_row').after( data.category_extra );
- }
+ } else {
+ // Use the original returned texts
+ fixmystreet.update_councils_text(fixmystreet.reporting_data);
+ }
+ if ( data.category_extra ) {
+ if ( $category_meta.length ) {
+ $category_meta.replaceWith( data.category_extra );
+ // Preserve any existing values
+ $category_meta.find("[name]").each(function() {
+ $category_meta.find("[name="+this.name+"]").val(this.value);
+ });
} else {
- $category_meta.empty();
+ $('#form_category_row').after( data.category_extra );
}
- fixmystreet.bodies = data.bodies || [];
- if (fixmystreet.body_overrides) {
- fixmystreet.body_overrides.clear();
- }
- $(fixmystreet).trigger('report_new:category_change:extras_received');
- });
+ } else {
+ $category_meta.empty();
+ }
$(fixmystreet).trigger('report_new:category_change', [ $(this) ]);
});
@@ -940,8 +948,15 @@ fixmystreet.update_report_a_problem_btn = function() {
$('.report-a-problem-btn').attr('href', href).text(text).toggle(visible);
};
+fixmystreet.update_public_councils_text = function(text, bodies) {
+ bodies = bodies.join('</strong> ' + translation_strings.or + ' <strong>');
+ text = text.replace(/<strong>.*<\/strong>/, '<strong>' + bodies + '</strong>');
+ $('#js-councils_text').html(text);
+};
+
fixmystreet.update_councils_text = function(data) {
- $('#js-councils_text').html(data.councils_text);
+ fixmystreet.update_public_councils_text(
+ data.councils_text, fixmystreet.bodies);
$('#js-councils_text_private').html(data.councils_text_private);
$(fixmystreet).trigger('body_overrides:change');
};
@@ -975,6 +990,9 @@ fixmystreet.update_pin = function(lonlat, savePushState) {
}
$('#side-form, #site-logo').show();
var old_category = $("select#form_category").val();
+
+ fixmystreet.reporting_data = data;
+
fixmystreet.update_councils_text(data);
$('#js-top-message').html(data.top_message || '');
$('#form_category_row').html(data.category);