diff options
author | Dave Arter <davea@mysociety.org> | 2018-05-21 17:16:57 +0100 |
---|---|---|
committer | Dave Arter <davea@mysociety.org> | 2018-06-26 17:20:28 +0100 |
commit | 9f7f6f482062f7f230849e7548abbc7fce79bad0 (patch) | |
tree | 0b00c9f2f26f132aa6ac61cde1908c2f6b1483d7 | |
parent | 53f3af1fd561d852346902a06ef27c110d0bd2c1 (diff) |
Add disable_pin_snapping flag to asset layers
This optional flag can be used for layers whose features should
set report extra fields when selected, without the features themselves
becoming highlighted and snapping the report pin to their location.
Useful for polygonal asset layers whose features cover a wide geographic
area where the actual location clicked by the user is important, e.g.
parks.
-rw-r--r-- | web/cobrands/fixmystreet/assets.js | 52 |
1 files changed, 40 insertions, 12 deletions
diff --git a/web/cobrands/fixmystreet/assets.js b/web/cobrands/fixmystreet/assets.js index 7e79776cd..77522fc6c 100644 --- a/web/cobrands/fixmystreet/assets.js +++ b/web/cobrands/fixmystreet/assets.js @@ -200,16 +200,7 @@ function asset_selected(e) { // its own USRN which should take precedence. $(fixmystreet).trigger('assets:selected', [ lonlat ]); - // Set the extra field to the value of the selected feature - $.each(this.fixmystreet.attributes, function (field_name, attribute_name) { - var field_value; - if (typeof attribute_name === 'function') { - field_value = attribute_name.apply(e.feature); - } else { - field_value = e.feature.attributes[attribute_name]; - } - $("#form_" + field_name).val(field_value); - }); + set_fields_from_attributes(this.fixmystreet.attributes, feature); // Hide the normal markers layer to keep things simple, but // move the green marker to the point of the click to stop @@ -227,10 +218,26 @@ function asset_selected(e) { function asset_unselected(e) { fixmystreet.markers.setVisibility(true); - $.each(this.fixmystreet.attributes, function (field_name, attribute_name) { + selected_feature = null; + clear_fields_for_attributes(this.fixmystreet.attributes); +} + +function set_fields_from_attributes(attributes, feature) { + // Set the extra fields to the value of the selected feature + $.each(attributes, function (field_name, attribute_name) { + var $field = $("#form_" + field_name); + if (typeof attribute_name === 'function') { + $field.val(attribute_name.apply(feature)); + } else { + $field.val(feature.attributes[attribute_name]); + } + }); +} + +function clear_fields_for_attributes(attributes) { + $.each(attributes, function (field_name, attribute_name) { $("#form_" + field_name).val(""); }); - selected_feature = null; } function find_matching_feature(feature, layer, asset_id_field) { @@ -528,6 +535,27 @@ fixmystreet.assets = { select_feature_control = new OpenLayers.Control.SelectFeature( asset_layer ); asset_layer.events.register( 'featureselected', asset_layer, asset_selected); asset_layer.events.register( 'featureunselected', asset_layer, asset_unselected); + if (options.disable_pin_snapping) { + // The pin is snapped to the centre of a feature by the select + // handler. We can stop this handler from running, and the pin + // being snapped, by returning false from a beforefeatureselected + // event handler. This handler does need to make sure the + // attributes of the clicked feature are applied to the extra + // details form fields first though. + asset_layer.events.register( 'beforefeatureselected', asset_layer, function(e) { + var attributes = this.fixmystreet.attributes; + set_fields_from_attributes(attributes, e.feature); + + // The next click on the map may not be on an asset - so + // clear the fields for this layer when the pin is next + // updated. If it is on an asset then the fields will be + // set by whatever feature was selected. + $(fixmystreet).one('maps:update_pin', function() { + clear_fields_for_attributes(attributes); + }); + return false; + }); + } // When panning/zooming the map check that this layer is still correctly shown // and any selected marker is preserved asset_layer.events.register( 'loadend', asset_layer, layer_loadend); |