(function() { // Store a reference to the "duplicate" report pins so we can // quickly remove them when we’re finished showing duplicates. var current_duplicate_markers; // keep track of whether the suggestion UI has already been dismissed // for this category var dismissed = false; var dismissed_category = null; // Report ID will be available on report inspect page, // but undefined on new report page. var report_id = $("#report_inspect_form .js-report-id").text() || undefined; // Don't make another call whilst one is in progress var in_progress = false; function refresh_duplicate_list(evt, params, category) { if (params && params.skip_duplicates) { return; } if (in_progress) { return; } if (!category) { category = $('select[name="category"]').val(); } if (category === '-- Pick a category --') { return; } var nearby_url; var url_params = { filter_category: category, latitude: $('input[name="latitude"]').val(), longitude: $('input[name="longitude"]').val() }; if ( report_id ) { nearby_url = '/report/' + report_id + '/nearby.json'; url_params.distance = 1000; // Inspectors might want to see reports fairly far away (1000 metres) url_params.pin_size = 'small'; // How it's always been } else { nearby_url = '/around/nearby'; url_params.distance = 250; // Only want to bother public with very nearby reports (250 metres) url_params.pin_size = 'normal'; } if ($('html').hasClass('mobile')) { url_params.inline_maps = 1; } if (category && params && params.check_duplicates_dismissal ) { dismissed = category === dismissed_category; dismissed_category = category; if (!take_effect()) { remove_duplicate_pins(); remove_duplicate_list(); return; } } in_progress = true; $.ajax({ url: nearby_url, data: url_params, dataType: 'json' }).done(function(response) { if (response.pins.length && take_effect()) { render_duplicate_list(response); render_duplicate_pins(response); } else { remove_duplicate_pins(); remove_duplicate_list(); } }).fail(function(){ remove_duplicate_pins(); remove_duplicate_list(); }); } function render_duplicate_list(api_response) { var $reports = $( api_response.reports_list ); var duplicate_of = $('#report_inspect_form [name="duplicate_of"]').val(); if ( duplicate_of ) { $reports.filter('[data-report-id="' + duplicate_of + '"]') .addClass("item-list__item--selected"); } $("#js-duplicate-reports ul").empty().prepend( $reports ); fixmystreet.set_up.fancybox_images(); $('#js-duplicate-reports').hide().removeClass('hidden').slideDown(function(){ in_progress = false; }); if ( $('#problem_form').length ) { $('.js-hide-if-invalid-category').slideUp(); $('.js-hide-if-invalid-category_extras').slideUp(); } if (!fixmystreet.map.events.extensions.buttonclick.isDeviceTouchCapable) { // Highlight map pin when hovering associated list item. // (not on touchscreens though because a) the 'mouseenter' handler means // two taps are required on the 'read more' button - one to highlight // the list item and another to activate the button- and b) the pins // might be scrolled off the top of the screen anyway e.g. on phones) var timeout; $reports.on('mouseenter', function(){ var id = parseInt( $(this).data('reportId'), 10 ); clearTimeout( timeout ); fixmystreet.maps.markers_highlight( id ); }).on('mouseleave', function(){ timeout = setTimeout( fixmystreet.maps.markers_highlight, 50 ); }); } // Add a "select this report" button, when on the report inspect form. if ( $('#report_inspect_form').length ) { $reports.each(function(){ var $button = $('