diff options
author | Matthew Somerville <matthew-github@dracos.co.uk> | 2018-03-12 16:52:44 +0000 |
---|---|---|
committer | Matthew Somerville <matthew-github@dracos.co.uk> | 2018-03-13 16:46:33 +0000 |
commit | 3cd4bb6e70d193f678e482835688f53c6ec68632 (patch) | |
tree | 4f4eabd2fce57d19de71aa8f34d64c2aa3be0510 | |
parent | 77d9804e958be13d7995d130fd6e4cfda2527a03 (diff) |
Update some tight JS coupling to use events.
Remove 'hooks' functions, and Split out USRN handling to its own object.
-rw-r--r-- | web/cobrands/fixmystreet/assets.js | 77 | ||||
-rw-r--r-- | web/cobrands/fixmystreet/fixmystreet.js | 14 | ||||
-rw-r--r-- | web/cobrands/fixmystreet/staff.js | 46 | ||||
-rw-r--r-- | web/js/map-OpenLayers.js | 7 |
4 files changed, 72 insertions, 72 deletions
diff --git a/web/cobrands/fixmystreet/assets.js b/web/cobrands/fixmystreet/assets.js index 804932f63..4854b902e 100644 --- a/web/cobrands/fixmystreet/assets.js +++ b/web/cobrands/fixmystreet/assets.js @@ -1,12 +1,54 @@ var fixmystreet = fixmystreet || {}; +/* Special USRN handling */ + (function(){ -var selected_feature = null; -var fault_popup = null; var selected_usrn = null; var usrn_field = null; +fixmystreet.usrn = { + select: function(evt, lonlat) { + var usrn_providers = fixmystreet.map.getLayersBy('fixmystreet', { + test: function(options) { + return options && options.usrn; + } + }); + if (usrn_providers.length) { + var usrn_layer = usrn_providers[0]; + usrn_field = usrn_layer.fixmystreet.usrn.field; + var point = new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat); + var feature = usrn_layer.getFeatureAtPoint(point); + if (feature == null) { + // The click wasn't directly over a road, try and find one + // nearby + feature = usrn_layer.getNearestFeature(point, 10); + } + if (feature !== null) { + selected_usrn = feature.attributes[usrn_layer.fixmystreet.usrn.attribute]; + } else { + selected_usrn = null; + } + fixmystreet.usrn.update_field(); + } + }, + + update_field: function() { + $("input[name="+usrn_field+"]").val(selected_usrn); + } +}; + +$(fixmystreet).on('maps:update_pin', fixmystreet.usrn.select); +$(fixmystreet).on('assets:selected', fixmystreet.usrn.select); +$(fixmystreet).on('report_new:category_change:extras_received', fixmystreet.usrn.update_field); + +})(); + +(function(){ + +var selected_feature = null; +var fault_popup = null; + function close_fault_popup() { if (!!fault_popup) { fixmystreet.map.removePopup(fault_popup); @@ -37,7 +79,7 @@ function asset_selected(e) { // Pick up the USRN for the location of this asset. NB we do this *before* // handling the attributes on the selected feature in case the feature has // its own USRN which should take precedence. - fixmystreet.assets.select_usrn(lonlat); + $(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) { @@ -442,35 +484,6 @@ fixmystreet.assets = { fixmystreet.map.addControl(fixmystreet.assets.controls[i]); fixmystreet.assets.controls[i].activate(); } - }, - - select_usrn: function(lonlat) { - var usrn_providers = fixmystreet.map.getLayersBy('fixmystreet', { - test: function(options) { - return options && options.usrn; - } - }); - if (usrn_providers.length) { - var usrn_layer = usrn_providers[0]; - usrn_field = usrn_layer.fixmystreet.usrn.field; - var point = new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat); - var feature = usrn_layer.getFeatureAtPoint(point); - if (feature == null) { - // The click wasn't directly over a road, try and find one - // nearby - feature = usrn_layer.getNearestFeature(point, 10); - } - if (feature !== null) { - selected_usrn = feature.attributes[usrn_layer.fixmystreet.usrn.attribute]; - } else { - selected_usrn = null; - } - fixmystreet.assets.update_usrn_field(); - } - }, - - update_usrn_field: function() { - $("input[name="+usrn_field+"]").val(selected_usrn); } }; diff --git a/web/cobrands/fixmystreet/fixmystreet.js b/web/cobrands/fixmystreet/fixmystreet.js index 1253af6b6..aae275ede 100644 --- a/web/cobrands/fixmystreet/fixmystreet.js +++ b/web/cobrands/fixmystreet/fixmystreet.js @@ -151,8 +151,6 @@ function isR2L() { }); })(jQuery); -fixmystreet.hooks = fixmystreet.hooks || {}; - fixmystreet.mobile_reporting = { apply_ui: function() { // Creates the "app-like" mobile reporting UI with full screen map @@ -417,18 +415,10 @@ $.extend(fixmystreet.set_up, { } else { $category_meta.empty(); } - if (fixmystreet.assets) { - fixmystreet.assets.update_usrn_field(); - } + $(fixmystreet).trigger('report_new:category_change:extras_received'); }); - if (fixmystreet.hooks.update_problem_fields) { - args.prefill_reports = $(this).data('prefill'); - args.role = $(this).data('role'); - args.body = $(this).data('body'); - - fixmystreet.hooks.update_problem_fields(args); - } + $(fixmystreet).trigger('report_new:category_change', [ $(this) ]); }); }, diff --git a/web/cobrands/fixmystreet/staff.js b/web/cobrands/fixmystreet/staff.js index 4577ab65e..dd65bc3ce 100644 --- a/web/cobrands/fixmystreet/staff.js +++ b/web/cobrands/fixmystreet/staff.js @@ -428,31 +428,33 @@ $.extend(fixmystreet.set_up, { }); -$.extend(fixmystreet.hooks, { - update_problem_fields: function(args) { - if (args.prefill_reports && args.role == 'inspector') { - var title = 'A ' + args.category + ' problem has been found'; - var description = 'A ' + args.category + ' problem has been found by ' + args.body; - - var $title_field = $('#form_title'); - var $description_field = $('#form_detail'); - - if ($title_field.val().length === 0 || $title_field.data('autopopulated') === true) { - $title_field.val(title); - $title_field.data('autopopulated', true); - } - - if ($description_field.val().length === 0 || $description_field.data('autopopulated') === true) { - $description_field.val(description); - $description_field.data('autopopulated', true); - } +$(fixmystreet).on('report_new:category_change', function(evt, $this) { + var category = $this.val(); + var prefill_reports = $this.data('prefill'); + var role = $this.data('role'); + var body = $this.data('body'); + + if (prefill_reports && role == 'inspector') { + var title = 'A ' + category + ' problem has been found'; + var description = 'A ' + category + ' problem has been found by ' + body; + + var $title_field = $('#form_title'); + var $description_field = $('#form_detail'); + + if ($title_field.val().length === 0 || $title_field.data('autopopulated') === true) { + $title_field.val(title); + $title_field.data('autopopulated', true); + } - $('#form_title, #form_detail').on('keyup', function() { - $(this).data('autopopulated', false); - }); + if ($description_field.val().length === 0 || $description_field.data('autopopulated') === true) { + $description_field.val(description); + $description_field.data('autopopulated', true); } - } + $('#form_title, #form_detail').on('keyup', function() { + $(this).data('autopopulated', false); + }); + } }); fixmystreet.maps = fixmystreet.maps || {}; diff --git a/web/js/map-OpenLayers.js b/web/js/map-OpenLayers.js index 5ebb9a18e..0a1c947a0 100644 --- a/web/js/map-OpenLayers.js +++ b/web/js/map-OpenLayers.js @@ -59,12 +59,7 @@ $.extend(fixmystreet.utils, { document.getElementById('fixmystreet.latitude').value = lat; document.getElementById('fixmystreet.longitude').value = lon; - // This tight coupling isn't ideal. A better solution would be for the - // asset code to register an event handler somewhere, but the correct - // place isn't apparent. - if (fixmystreet.assets) { - fixmystreet.assets.select_usrn(lonlat); - } + $(fixmystreet).trigger('maps:update_pin', [ lonlat ]); return { 'url': { 'lon': lon, 'lat': lat }, |