aboutsummaryrefslogtreecommitdiffstats
path: root/web
diff options
context:
space:
mode:
Diffstat (limited to 'web')
-rw-r--r--web/cobrands/fixmystreet/assets.js96
-rw-r--r--web/cobrands/fixmystreet/fixmystreet.js3
-rw-r--r--web/cobrands/fixmystreet/map.js5
-rw-r--r--web/js/map-OpenLayers.js42
4 files changed, 110 insertions, 36 deletions
diff --git a/web/cobrands/fixmystreet/assets.js b/web/cobrands/fixmystreet/assets.js
index 7ce12cbe3..11ea1c80e 100644
--- a/web/cobrands/fixmystreet/assets.js
+++ b/web/cobrands/fixmystreet/assets.js
@@ -16,12 +16,59 @@ var fixmystreet = fixmystreet || {};
};
})();
+OpenLayers.Layer.VectorAsset = OpenLayers.Class(OpenLayers.Layer.Vector, {
+ initialize: function(name, options) {
+ OpenLayers.Layer.Vector.prototype.initialize.apply(this, arguments);
+ $(fixmystreet).on('report_new:category_change', this.update_layer_visibility.bind(this));
+ },
+
+ update_layer_visibility: function() {
+ if (!fixmystreet.map) {
+ return;
+ }
+
+ if (!this.fixmystreet.always_visible) {
+ // Show/hide the asset layer when the category is chosen
+ var category = $('#problem_form select#form_category').val();
+ if (fixmystreet.assets.check_layer_relevant(this.fixmystreet, category)) {
+ this.setVisibility(true);
+ if (this.fixmystreet.fault_layer) {
+ this.fixmystreet.fault_layer.setVisibility(true);
+ }
+ this.zoom_to_assets();
+ } else {
+ this.setVisibility(false);
+ if (this.fixmystreet.fault_layer) {
+ this.fixmystreet.fault_layer.setVisibility(false);
+ }
+ }
+ } else {
+ if (this.fixmystreet.body) {
+ this.setVisibility(OpenLayers.Util.indexOf(fixmystreet.bodies, this.fixmystreet.body) != -1 );
+ }
+ }
+ },
+
+ zoom_to_assets: function() {
+ // This function is called when the asset category is
+ // selected, and will zoom the map in to the first level that
+ // makes the asset layer visible if it's not already shown.
+ if (!this.inRange) {
+ var firstVisibleResolution = this.resolutions[0];
+ var zoomLevel = fixmystreet.map.getZoomForResolution(firstVisibleResolution);
+ fixmystreet.map.zoomTo(zoomLevel);
+ }
+ },
+
+ CLASS_NAME: 'OpenLayers.Layer.VectorAsset'
+});
+
// Handles layers such as USRN, TfL roads, and the like
-OpenLayers.Layer.VectorNearest = OpenLayers.Class(OpenLayers.Layer.Vector, {
+OpenLayers.Layer.VectorNearest = OpenLayers.Class(OpenLayers.Layer.VectorAsset, {
selected_feature: null,
initialize: function(name, options) {
- OpenLayers.Layer.Vector.prototype.initialize.apply(this, arguments);
+ 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
@@ -31,10 +78,13 @@ OpenLayers.Layer.VectorNearest = OpenLayers.Class(OpenLayers.Layer.Vector, {
},
checkFeature: function(evt, lonlat) {
+ if (!this.getVisibility()) {
+ return;
+ }
this.getNearest(lonlat);
this.updateUSRNField();
if (this.fixmystreet.road) {
- var valid_category = this.fixmystreet.all_categories || (this.fixmystreet.asset_category && this.fixmystreet.asset_category.indexOf($('select#form_category').val()) != -1);
+ var valid_category = this.fixmystreet.all_categories || (this.fixmystreet.asset_category && fixmystreet.assets.check_layer_relevant( this.fixmystreet, $('select#form_category').val() ) );
if (!valid_category || !this.selected_feature) {
this.road_not_found();
} else {
@@ -111,6 +161,7 @@ var fault_popup = null;
* Called as part of fixmystreet.assets.init for each asset layer on the map.
*/
function init_asset_layer(layer, pins_layer) {
+ layer.update_layer_visibility();
fixmystreet.map.addLayer(layer);
if (layer.fixmystreet.asset_category) {
fixmystreet.map.events.register( 'zoomend', layer, check_zoom_message_visibility);
@@ -140,28 +191,8 @@ function init_asset_layer(layer, pins_layer) {
layer.events.register( 'loadend', layer, layer.one_time_select );
}
- if (!layer.fixmystreet.always_visible) {
- // Show/hide the asset layer when the category is chosen
- $("#problem_form").on("change.category", "select#form_category", function(){
- var category = $(this).val();
- if (layer.fixmystreet.asset_category.indexOf(category) != -1) {
- layer.setVisibility(true);
- if (layer.fixmystreet.fault_layer) {
- layer.fixmystreet.fault_layer.setVisibility(true);
- }
- zoom_to_assets(layer);
- } else {
- layer.setVisibility(false);
- if (layer.fixmystreet.fault_layer) {
- layer.fixmystreet.fault_layer.setVisibility(false);
- }
- }
- });
- }
-
}
-
function close_fault_popup() {
if (!!fault_popup) {
fixmystreet.map.removePopup(fault_popup);
@@ -267,7 +298,7 @@ function check_zoom_message_visibility() {
prefix = category.replace(/[^a-z]/gi, ''),
id = "category_meta_message_" + prefix,
$p = $('#' + id);
- if (this.fixmystreet.asset_category.indexOf(category) != -1) {
+ if (fixmystreet.assets.check_layer_relevant(this.fixmystreet, category)) {
if ($p.length === 0) {
$p = $("<p>").prop("id", id).prop('class', 'category_meta_message');
$p.insertAfter('#form_category_row');
@@ -313,16 +344,6 @@ function layer_visibilitychanged() {
}
}
-function zoom_to_assets(layer) {
- // This function is called when the asset category is
- // selected, and will zoom the map in to the first level that
- // makes the asset layer visible if it's not already shown.
- if (!layer.inRange) {
- var firstVisibleResolution = layer.resolutions[0];
- var zoomLevel = fixmystreet.map.getZoomForResolution(firstVisibleResolution);
- fixmystreet.map.zoomTo(zoomLevel);
- }
-}
function get_select_control(layer) {
var controls = fixmystreet.map.getControlsByClass('OpenLayers.Control.SelectFeature');
@@ -503,7 +524,7 @@ fixmystreet.assets = {
}
}
- var layer_class = OpenLayers.Layer.Vector;
+ var layer_class = OpenLayers.Layer.VectorAsset;
if (options.usrn || options.road) {
layer_class = OpenLayers.Layer.VectorNearest;
}
@@ -631,6 +652,11 @@ fixmystreet.assets = {
fixmystreet.map.addControl(fixmystreet.assets.controls[i]);
fixmystreet.assets.controls[i].activate();
}
+ },
+
+ check_layer_relevant: function(layer, category) {
+ return OpenLayers.Util.indexOf(layer.asset_category, category) != -1 &&
+ ( !layer.body || OpenLayers.Util.indexOf(fixmystreet.bodies, layer.body) != -1 );
}
};
diff --git a/web/cobrands/fixmystreet/fixmystreet.js b/web/cobrands/fixmystreet/fixmystreet.js
index 26b0b293a..5f9dd9699 100644
--- a/web/cobrands/fixmystreet/fixmystreet.js
+++ b/web/cobrands/fixmystreet/fixmystreet.js
@@ -409,6 +409,7 @@ $.extend(fixmystreet.set_up, {
} else {
$category_meta.empty();
}
+ fixmystreet.bodies = data.bodies || [];
$(fixmystreet).trigger('report_new:category_change:extras_received');
});
@@ -940,6 +941,8 @@ fixmystreet.update_pin = function(lonlat, savePushState) {
lb.before(data.extra_name_info);
}
+ fixmystreet.bodies = data.bodies || [];
+
// If the category filter appears on the map and the user has selected
// something from it, then pre-fill the category field in the report,
// if it's a value already present in the drop-down.
diff --git a/web/cobrands/fixmystreet/map.js b/web/cobrands/fixmystreet/map.js
index 9303c22b7..7c3aeb55e 100644
--- a/web/cobrands/fixmystreet/map.js
+++ b/web/cobrands/fixmystreet/map.js
@@ -3,7 +3,7 @@ var fixmystreet = fixmystreet || {};
(function(){
var map_data = document.getElementById('js-map-data'),
- map_keys = [ 'area', 'latitude', 'longitude', 'zoomToBounds', 'zoom', 'pin_prefix', 'pin_new_report_colour', 'numZoomLevels', 'zoomOffset', 'map_type', 'key' ],
+ map_keys = [ 'area', 'latitude', 'longitude', 'zoomToBounds', 'zoom', 'pin_prefix', 'pin_new_report_colour', 'numZoomLevels', 'zoomOffset', 'map_type', 'key', 'bodies' ],
numeric = { zoom: 1, numZoomLevels: 1, zoomOffset: 1, id: 1 },
pin_keys = [ 'lat', 'lon', 'colour', 'id', 'title', 'type' ];
@@ -18,6 +18,9 @@ var fixmystreet = fixmystreet || {};
}
});
+
+ fixmystreet.bodies = fixmystreet.bodies ? fixmystreet.utils.csv_to_array(fixmystreet.bodies)[0] : [];
+
fixmystreet.area = fixmystreet.area ? fixmystreet.area.split(',') : [];
if (fixmystreet.map_type) {
var s = fixmystreet.map_type.split('.');
diff --git a/web/js/map-OpenLayers.js b/web/js/map-OpenLayers.js
index 3c9e3fb91..07acf248c 100644
--- a/web/js/map-OpenLayers.js
+++ b/web/js/map-OpenLayers.js
@@ -27,6 +27,48 @@ $.extend(fixmystreet.utils, {
return out.join(',');
},
+ // https://stackoverflow.com/questions/1293147/javascript-code-to-parse-csv-data/1293163#1293163
+ csv_to_array: function( strData, strDelimiter ) {
+ strDelimiter = (strDelimiter || ",");
+
+ var objPattern = new RegExp(
+ (
+ "(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +
+ "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +
+ "([^\"\\" + strDelimiter + "\\r\\n]*))"
+ ),
+ "gi"
+ );
+
+ var arrData = [[]];
+
+ var arrMatches = objPattern.exec( strData );
+ while (arrMatches) {
+
+ var strMatchedDelimiter = arrMatches[ 1 ];
+
+ if ( strMatchedDelimiter.length &&
+ strMatchedDelimiter !== strDelimiter) {
+ arrData.push( [] );
+ }
+
+ var strMatchedValue;
+ if (arrMatches[ 2 ]) {
+ strMatchedValue = arrMatches[ 2 ].replace(
+ new RegExp( "\"\"", "g" ),
+ "\""
+ );
+ } else {
+ strMatchedValue = arrMatches[ 3 ];
+ }
+
+ arrData[ arrData.length - 1 ].push( strMatchedValue );
+ arrMatches = objPattern.exec( strData );
+ }
+
+ return( arrData );
+ },
+
parse_query_string: function() {
var qs = {};
if (!location.search) {