if (!Object.keys) { Object.keys = function(obj) { var result = []; for (var prop in obj) { if (Object.prototype.hasOwnProperty.call(obj, prop)) { result.push(prop); } } return result; }; } var fixmystreet = fixmystreet || {}; fixmystreet.utils = fixmystreet.utils || {}; $.extend(fixmystreet.utils, { array_to_csv_line: function(arr) { var out = [], s; for (var i=0; i= 15) { return window.selected_problem_id ? 'small' : 'normal'; } else if (zoom >= 13) { return window.selected_problem_id ? 'mini' : 'small'; } else { return 'mini'; } }, selected_marker_size: function() { var zoom = fixmystreet.map.getZoom() + fixmystreet.zoomOffset; if (zoom >= 15) { return 'big'; } else if (zoom >= 13) { return 'normal'; } else { return 'small'; } }, // Handle a single report pin being moved by dragging it on the map. // pin_moved_callback is called with a new EPSG:4326 OpenLayers.LonLat if // the user drags the pin and confirms its new location. admin_drag: function(pin_moved_callback, confirm_change) { confirm_change = confirm_change || false; var original_lonlat; var drag = new OpenLayers.Control.DragFeatureFMS( fixmystreet.markers, { onStart: function(feature, e) { // Keep track of where the feature started, so we can put it // back if the user cancels the operation. original_lonlat = new OpenLayers.LonLat(feature.geometry.x, feature.geometry.y); }, onComplete: function(feature, e) { var lonlat = feature.geometry.clone(); lonlat.transform( fixmystreet.map.getProjectionObject(), new OpenLayers.Projection("EPSG:4326") ); if ((confirm_change && window.confirm(translation_strings.correct_position)) || !confirm_change) { // Let the callback know about the newly confirmed position pin_moved_callback(lonlat); } else { // Put it back fixmystreet.markers.features[0].move(original_lonlat); } } } ); fixmystreet.map.addControl( drag ); drag.activate(); }, // `markers.redraw()` in markers_highlight will trigger an // `overFeature` event if the mouse cursor is still over the same // marker on the map, which would then run markers_highlight // again, causing an infinite flicker while the cursor remains over // the same marker. We really only want to redraw the markers when // the cursor moves from one marker to another (ie: when there is an // overFeature followed by an outFeature followed by an overFeature). // Therefore, we keep track of the previous event in // fixmystreet.latest_map_hover_event and only call markers_highlight // if we know the previous event was different to the current one. // (See the `overFeature` and `outFeature` callbacks inside of // fixmystreet.select_feature). markers_highlight: function(problem_id) { for (var i = 0; i < fixmystreet.markers.features.length; i++) { if (typeof problem_id == 'undefined') { // There is no highlighted marker, so unfade this marker fixmystreet.markers.features[i].attributes.faded = 0; } else if (problem_id == fixmystreet.markers.features[i].attributes.id) { // This is the highlighted marker, unfade it fixmystreet.markers.features[i].attributes.faded = 0; } else { // This is not the hightlighted marker, fade it fixmystreet.markers.features[i].attributes.faded = 1; } } fixmystreet.markers.redraw(); }, /* Keep track of how many things are loading simultaneously, and only hide * the loading spinner when everything has finished. * This allows multiple layers to be loading at once without each layer * having to keep track of the others or be responsible for manipulating * the spinner in the DOM. */ loading_spinner: { count: {}, show: function() { fixmystreet.maps.loading_spinner.count[this.id] = 1; if (Object.keys(fixmystreet.maps.loading_spinner.count).length) { // Show the loading indicator over the map $('#loading-indicator').removeClass('hidden'); $('#loading-indicator').attr('aria-hidden', false); } }, hide: function() { delete fixmystreet.maps.loading_spinner.count[this.id]; if (!Object.keys(fixmystreet.maps.loading_spinner.count).length) { // Remove loading indicator $('#loading-indicator').addClass('hidden'); $('#loading-indicator').attr('aria-hidden', true); } } } }); /* Make sure pins aren't going to reload just because we're zooming out, * we already have the pins when the page loaded */ function zoomToBounds(bounds) { if (!bounds) { return; } var strategy = fixmystreet.markers.strategies[0]; strategy.deactivate(); var center = bounds.getCenterLonLat(); var z = fixmystreet.map.getZoomForExtent(bounds); fixmystreet.map.setCenter(center, z); // Reactivate the strategy and make it think it's done an update strategy.activate(); if (strategy instanceof OpenLayers.Strategy.BBOX) { strategy.calculateBounds(); strategy.resolution = fixmystreet.map.getResolution(); } } function sidebar_highlight(problem_id) { if (typeof problem_id !== 'undefined') { var $a = $('.item-list--reports a[href$="/' + problem_id + '"]'); $a.parent().addClass('hovered'); } else { $('.item-list--reports .hovered').removeClass('hovered'); } } function marker_click(problem_id, evt) { var $a = $('.item-list--reports a[href$="/' + problem_id + '"]'); if (!$a[0]) { return; } // clickFeature operates on touchstart, we do not want the map click taking place on touchend! if (fixmystreet.maps.click_control) { fixmystreet.maps.click_control.deactivate(); } // All of this, just so that ctrl/cmd-click on a pin works?! var event; if (typeof window.MouseEvent === 'function') { event = new MouseEvent('click', evt); $a[0].dispatchEvent(event); } else if (document.createEvent) { event = document.createEvent("MouseEvents"); event.initMouseEvent( 'click', true, true, window, 1, 0, 0, 0, 0, evt.ctrlKey, evt.altKey, evt.shiftKey, evt.metaKey, 0, null); $a[0].dispatchEvent(event); } else if (document.createEventObject) { event = document.createEventObject(); event.metaKey = evt.metaKey; event.ctrlKey = evt.ctrlKey; if (event.metaKey === undefined) { event.metaKey = event.ctrlKey; } $a[0].fireEvent("onclick", event); } else { $a[0].click(); } } function categories_or_status_changed() { // If the category or status has changed we need to re-fetch map markers fixmystreet.markers.refresh({force: true}); } function replace_query_parameter(qs, id, key) { var value = $('#' + id).val(); if ( $('#' + id).prop('type') == 'checkbox' ) { value = $('#' + id).prop('checked') ? '1' : ''; } else if (value) { value = (typeof value === 'string') ? value : fixmystreet.utils.array_to_csv_line(value); } if (value) { qs[key] = value; } else { delete qs[key]; } return value; } function update_url(qs) { var new_url; if ($.isEmptyObject(qs)) { new_url = location.href.replace(location.search, ""); } else if (location.search) { new_url = location.href.replace(location.search, '?' + $.param(qs)); } else { new_url = location.href + '?' + $.param(qs); } return new_url; } function page_changed_history() { if (!('pushState' in history)) { return; } var qs = fixmystreet.utils.parse_query_string(); var show_old_reports = ''; var page = $('.pagination:first').data('page'); if (page > 1) { show_old_reports = replace_query_parameter(qs, 'show_old_reports', 'show_old_reports'); qs.p = page; } else { delete qs.p; } var new_url = update_url(qs); history.pushState({ page_change: { 'page': page, 'show_old_reports': show_old_reports } }, null, new_url); } function categories_or_status_changed_history() { if (!('pushState' in history)) { return; } var qs = fixmystreet.utils.parse_query_string(); var filter_categories = replace_query_parameter(qs, 'filter_categories', 'filter_category'); var filter_statuses = replace_query_parameter(qs, 'statuses', 'status'); var sort_key = replace_query_parameter(qs, 'sort', 'sort'); var show_old_reports = replace_query_parameter(qs, 'show_old_reports', 'show_old_reports'); delete qs.p; var new_url = update_url(qs); history.pushState({ filter_change: { 'filter_categories': filter_categories, 'statuses': filter_statuses, 'sort': sort_key, 'show_old_reports': show_old_reports } }, null, new_url); } function setup_inspector_marker_drag() { // On the 'inspect report' page the pin is draggable, so we need to // update the easting/northing fields when it's dragged. if (!$('form#report_inspect_form').length) { // Not actually on the inspect report page return; } fixmystreet.maps.admin_drag(function(lonlat) { var bng = lonlat.clone().transform( new OpenLayers.Projection("EPSG:4326"), new OpenLayers.Projection("EPSG:27700") // TODO: Handle other projections ); $("#problem_northing").text(bng.y.toFixed(1)); $("#problem_easting").text(bng.x.toFixed(1)); $("#problem_latitude").text(lonlat.y.toFixed(6)); $("#problem_longitude").text(lonlat.x.toFixed(6)); $("form#report_inspect_form input[name=latitude]").val(lonlat.y); $("form#report_inspect_form input[name=longitude]").val(lonlat.x); }, false); } function onload() { if ( fixmystreet.area.length ) { var extent = new OpenLayers.Bounds(); var lr = new OpenLayers.Geometry.LinearRing([ new OpenLayers.Geometry.Point(20E6,20E6), new OpenLayers.Geometry.Point(10E6,20E6), new OpenLayers.Geometry.Point(0,20E6), new OpenLayers.Geometry.Point(-10E6,20E6), new OpenLayers.Geometry.Point(-20E6,20E6), new OpenLayers.Geometry.Point(-20E6,0), new OpenLayers.Geometry.Point(-20E6,-20E6), new OpenLayers.Geometry.Point(-10E6,-20E6), new OpenLayers.Geometry.Point(0,-20E6), new OpenLayers.Geometry.Point(10E6,-20E6), new OpenLayers.Geometry.Point(20E6,-20E6), new OpenLayers.Geometry.Point(20E6,0) ]); var loaded = 0; var new_geometry = new OpenLayers.Geometry.Polygon(lr); var style_area = function() { loaded++; var style = this.styleMap.styles['default']; if ( fixmystreet.area_format ) { style.defaultStyle = fixmystreet.area_format; } else { $.extend(style.defaultStyle, { fillColor: 'black', strokeColor: 'black' }); } var geometry = this.features[0].geometry; if (geometry.CLASS_NAME == 'OpenLayers.Geometry.Collection') { $.each(geometry.components, function(i, polygon) { new_geometry.addComponents(polygon.components); extent.extend(polygon.getBounds()); }); } else if (geometry.CLASS_NAME == 'OpenLayers.Geometry.Polygon') { new_geometry.addComponents(geometry.components); extent.extend(this.getDataExtent()); } if (loaded == fixmystreet.area.length) { var f = this.features[0].clone(); f.geometry = new_geometry; this.removeAllFeatures(); this.addFeatures([f]); var qs = fixmystreet.utils.parse_query_string(); if (!qs.bbox) { zoomToBounds(extent); } } else { fixmystreet.map.removeLayer(this); } }; for (var i=0; i