diff options
author | Matthew Somerville <matthew-github@dracos.co.uk> | 2019-01-07 17:43:11 +0000 |
---|---|---|
committer | Matthew Somerville <matthew-github@dracos.co.uk> | 2019-01-07 17:43:11 +0000 |
commit | 6423ad7b509a72e62d411eadb3e9f9d67d1b85da (patch) | |
tree | 956301b21c074069a886ff294d11799477b92169 | |
parent | 053d195abb55d8089091ce753466952fc88ed658 (diff) | |
parent | b3542314ac6480505b9939b2e46715a19dab8d49 (diff) |
Merge branch 'double-green-pin-asset'
-rw-r--r-- | web/cobrands/fixmystreet/assets.js | 69 |
1 files changed, 42 insertions, 27 deletions
diff --git a/web/cobrands/fixmystreet/assets.js b/web/cobrands/fixmystreet/assets.js index fbf40c690..0134991e9 100644 --- a/web/cobrands/fixmystreet/assets.js +++ b/web/cobrands/fixmystreet/assets.js @@ -122,6 +122,30 @@ OpenLayers.Layer.VectorAsset = OpenLayers.Class(OpenLayers.Layer.Vector, { } }, + assets_have_same_id: function(f1, f2) { + var asset_id_field = this.fixmystreet.asset_id_field; + return (f1.attributes[asset_id_field] == f2.attributes[asset_id_field]); + }, + + find_matching_feature: function(feature, layer) { + if (!layer) { + return false; + } + // When the WFS layer is reloaded the same features might be visible + // but they'll be different instances of the class so we can't use + // object identity comparisons. + // This function will find the best matching feature based on its + // attributes and distance from the original feature. + var threshold = 1; // metres + for (var i = 0; i < layer.features.length; i++) { + var candidate = layer.features[i]; + var distance = candidate.geometry.distanceTo(feature.geometry); + if (this.assets_have_same_id(feature, candidate) && distance <= threshold) { + return candidate; + } + } + }, + CLASS_NAME: 'OpenLayers.Layer.VectorAsset' }); @@ -265,9 +289,12 @@ function asset_selected(e) { close_fault_popup(); var lonlat = e.feature.geometry.getBounds().getCenterLonLat(); + var layer = e.feature.layer; + var feature = e.feature; + // Check if there is a known fault with the asset that's been clicked, // and disallow selection if so. - var fault_feature = find_matching_feature(e.feature, this.fixmystreet.fault_layer, this.fixmystreet.asset_id_field); + var fault_feature = layer.find_matching_feature(feature, this.fixmystreet.fault_layer); if (!!fault_feature) { fault_popup = new OpenLayers.Popup.FramedCloud("popup", e.feature.geometry.getBounds().getCenterLonLat(), @@ -280,9 +307,6 @@ function asset_selected(e) { return; } - var layer = e.feature.layer; - var feature = e.feature; - // Keep track of selection in case layer is reloaded or hidden etc. selected_feature = feature.clone(); @@ -336,25 +360,6 @@ function clear_fields_for_attributes(attributes) { }); } -function find_matching_feature(feature, layer, asset_id_field) { - if (!layer) { - return false; - } - // When the WFS layer is reloaded the same features might be visible - // but they'll be different instances of the class so we can't use - // object identity comparisons. - // This function will find the best matching feature based on its - // attributes and distance from the original feature. - var threshold = 1; // metres - for (var i = 0; i < layer.features.length; i++) { - var candidate = layer.features[i]; - var distance = candidate.geometry.distanceTo(feature.geometry); - if (candidate.attributes[asset_id_field] == feature.attributes[asset_id_field] && distance <= threshold) { - return candidate; - } - } -} - function check_zoom_message_visibility() { if (this.fixmystreet.non_interactive) { return; @@ -420,10 +425,20 @@ function layer_visibilitychanged() { function layer_loadend() { this.select_nearest_asset(); // Preserve the selected marker when panning/zooming, if it's still on the map - if (selected_feature !== null && !(selected_feature in this.selectedFeatures)) { - var replacement_feature = find_matching_feature(selected_feature, this, this.fixmystreet.asset_id_field); - if (!!replacement_feature) { - this.get_select_control().select(replacement_feature); + if (selected_feature !== null) { + // Can't use (selected_feature in this.selectedFeatures) as it's a clone + var found = false; + for (var i=0; i < this.selectedFeatures.length; i++) { + if (this.assets_have_same_id(selected_feature, this.selectedFeatures[i])) { + found = true; + break; + } + } + if (!found) { + var replacement_feature = this.find_matching_feature(selected_feature, this); + if (!!replacement_feature) { + this.get_select_control().select(replacement_feature); + } } } } |