var fixmystreet = fixmystreet || {}; (function() { fixmystreet.maps = { // This function might be passed either an OpenLayers.LonLat (so has // lon and lat), or an OpenLayers.Geometry.Point (so has x and y). update_pin: function(lonlat) { var transformedLonlat = lonlat.clone().transform( fixmystreet.map.getProjectionObject(), new OpenLayers.Projection("EPSG:4326") ); var lat = transformedLonlat.lat || transformedLonlat.y; var lon = transformedLonlat.lon || transformedLonlat.x; document.getElementById('fixmystreet.latitude').value = lat; document.getElementById('fixmystreet.longitude').value = lon; return { 'url': { 'lon': lon, 'lat': lat }, 'state': { 'lon': lonlat.lon, 'lat': lonlat.lat } }; }, display_around: function() { // Required after changing the size of the map element fixmystreet.map.updateSize(); // Dragging the map should fetch new local reports from server if (fixmystreet.bbox_strategy) { fixmystreet.bbox_strategy.activate(); } // Should not be able to drag normal pins!! drag.deactivate(); // Force a redraw to return (de)selected marker to normal size // Redraw for all pages, kick off a refresh too for around // TODO Put 'new report' pin in different layer to simplify this and elsewhere fixmystreet.maps.markers_resize(); fixmystreet.markers.refresh({force: true}); }, begin_report: function(lonlat) { if (typeof lonlat.clone !== 'function') { lonlat = new OpenLayers.LonLat(lonlat.lon, lonlat.lat); } if (fixmystreet.page == 'new') { /* Already have a pin */ fixmystreet.markers.features[0].move(lonlat); } else { var markers = fixmystreet.maps.markers_list( [ [ lonlat.lat, lonlat.lon, 'green' ] ], false ); fixmystreet.bbox_strategy.deactivate(); fixmystreet.markers.removeAllFeatures(); fixmystreet.markers.addFeatures( markers ); drag.activate(); } // check to see if markers are visible. We click the // link so that it updates the text in case they go // back if ( ! fixmystreet.markers.getVisibility() ) { $('#hide_pins_link').click(); } return lonlat; }, markers_list: function(pins, transform) { var markers = []; var size = fixmystreet.maps.marker_size_for_zoom( fixmystreet.map.getZoom() + fixmystreet.zoomOffset ); var selected_size = fixmystreet.maps.selected_marker_size_for_zoom( fixmystreet.map.getZoom() + fixmystreet.zoomOffset ); 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_for_zoom: function(zoom) { if (zoom >= 15) { return 'big'; } else if (zoom >= 13) { return 'normal'; } else { return 'small'; } } }; var drag = { activate: function() { this._drag = new OpenLayers.Control.DragFeature( fixmystreet.markers, { onComplete: function(feature, e) { fixmystreet.update_pin( feature.geometry ); } } ); fixmystreet.map.addControl( this._drag ); this._drag.activate(); }, deactivate: function() { this._drag && this._drag.deactivate(); } }; function zoomToBounds(bounds) { if (!bounds) { return; } var center = bounds.getCenterLonLat(); var z = fixmystreet.map.getZoomForExtent(bounds); if ( z < 13 && $('html').hasClass('mobile') ) { z = 13; } fixmystreet.map.setCenter(center, z); } // `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). function markers_highlight(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(); } 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; } // All of this, just so that ctrl/cmd-click on a pin works?! var event; if (window.MouseEvent) { 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 (e.metaKey === undefined) { e.metaKey = e.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 onload() { if ( fixmystreet.area.length ) { for (var i=0; i