Yaron Koren has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/377690 )

Change subject: Fixed double-click handling for OpenLayers, Leaflet
......................................................................

Fixed double-click handling for OpenLayers, Leaflet

Change-Id: Ib862cf25a5d6b72855d475cb69b4badb4b16ea7a
---
M libs/PF_maps.js
M libs/PF_maps.offline.js
2 files changed, 57 insertions(+), 14 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/PageForms 
refs/changes/90/377690/2

diff --git a/libs/PF_maps.js b/libs/PF_maps.js
index 96d2f9e..9302ad5 100644
--- a/libs/PF_maps.js
+++ b/libs/PF_maps.js
@@ -6,6 +6,8 @@
 
 function setupMapFormInput( inputDiv, mapService ) {
        var map, marker, markers, mapCanvas, mapOptions;
+       var numClicks = 0, timer = null;
+
        if ( mapService === "Google Maps" ) {
                mapCanvas = inputDiv.find('.pfMapCanvas')[0];
                mapOptions = {
@@ -14,18 +16,17 @@
                };
                map = new google.maps.Map(mapCanvas, mapOptions);
                var geocoder = new google.maps.Geocoder();
-               var update_timeout;
 
                // Let a click set the marker, while keeping the default
                // behavior (zoom and center) for double clicks.
                // Code copied from http://stackoverflow.com/a/8417447
                google.maps.event.addListener( map, 'click', function( event ) {
-                       update_timeout = setTimeout( function() {
+                       timer = setTimeout( function() {
                                googleMapsSetMarker( event.latLng );
                        }, 200 );
                });
                google.maps.event.addListener( map, 'dblclick', function( event 
) {
-                       clearTimeout( update_timeout );
+                       clearTimeout( timer );
                });
        } else if (mapService === "Leaflet") {
                mapCanvas = inputDiv.find('.pfMapCanvas').get(0);
@@ -41,7 +42,19 @@
                new 
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', 
layerOptions).addTo(map);
 
                map.on( 'click', function( event ) {
-                       leafletSetMarker( event.latlng );
+                       // Place/move the marker only on a single click, not a
+                       // double click (double clicks do a zoom).
+                       // Code based on https://stackoverflow.com/a/7845282
+                       numClicks++;
+                       if (numClicks === 1) {
+                               timer = setTimeout( function() {
+                                       leafletSetMarker( event.latlng );
+                                       numClicks = 0;
+                               });
+                       } else {
+                               clearTimeout(timer);
+                               numClicks = 0;
+                       }
                });
        } else { // if ( mapService === "OpenLayers" ) {
                var mapCanvasID = inputDiv.find('.pfMapCanvas').attr('id');
@@ -52,9 +65,18 @@
                map.addLayer( markers );
 
                map.events.register("click", map, function(e) {
-                       var opx = map.getLayerPxFromViewPortPx(e.xy) ;
-                       var loc = map.getLonLatFromPixel( opx );
-                       openLayersSetMarker( loc );
+                       numClicks++;
+                       if (numClicks === 1) {
+                               timer = setTimeout( function() {
+                                       var opx = 
map.getLayerPxFromViewPortPx(e.xy) ;
+                                       var loc = map.getLonLatFromPixel( opx );
+                                       openLayersSetMarker( loc );
+                                       numClicks = 0;
+                               });
+                       } else {
+                               clearTimeout(timer);
+                               numClicks = 0;
+                       }
                });
        }
 
diff --git a/libs/PF_maps.offline.js b/libs/PF_maps.offline.js
index 6535748..342b55c 100644
--- a/libs/PF_maps.offline.js
+++ b/libs/PF_maps.offline.js
@@ -16,6 +16,7 @@
        }
 
        var map, marker, markers, mapCanvas, mapOptions;
+       var numClicks = 0, timer = null;
 
        var coordsInput = inputDiv.find('.pfCoordsInput');
 
@@ -92,18 +93,17 @@
                };
                map = new google.maps.Map( mapCanvas, mapOptions );
                var geocoder = new google.maps.Geocoder();
-               var update_timeout;
 
                // Let a click set the marker, while keeping the default
                // behavior (zoom and center) for double clicks.
                // Code copied from http://stackoverflow.com/a/8417447
                google.maps.event.addListener( map, 'click', function( event ) {
-                       update_timeout = setTimeout( function(){
+                       timer = setTimeout( function(){
                                googleMapsSetMarker( event.latLng );
                        }, 200 );
                });
                google.maps.event.addListener( map, 'dblclick', function( event 
) {
-                       clearTimeout( update_timeout );
+                       clearTimeout( timer );
                });
        } else if (mapService === "Leaflet") {
                mapCanvas = inputDiv.find('.pfMapCanvas').get(0);
@@ -119,7 +119,19 @@
                new 
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', 
layerOptions).addTo(map);
 
                map.on( 'click', function( event ) {
-                       leafletSetMarker( event.latlng );
+                       // Place/move the marker only on a single click, not a
+                       // double click (double clicks do a zoom).
+                       // Code based on https://stackoverflow.com/a/7845282
+                       numClicks++;
+                       if (numClicks === 1) {
+                               timer = setTimeout( function() {
+                                       leafletSetMarker( event.latlng );
+                                       numClicks = 0;
+                               });
+                       } else {
+                               clearTimeout(timer);
+                               numClicks = 0;
+                       }
                });
        } else { // if ( mapService == "OpenLayers" ) {
                var mapCanvasID = inputDiv.find( '.pfMapCanvas' ).attr( 'id' );
@@ -130,9 +142,18 @@
                map.addLayer( markers );
 
                map.events.register( "click", map, function( e ) {
-                       var opx = map.getLayerPxFromViewPortPx(e.xy) ;
-                       var loc = map.getLonLatFromPixel( opx );
-                       openLayersSetMarker( loc );
+                       numClicks++;
+                       if (numClicks === 1) {
+                               timer = setTimeout( function() {
+                                       var opx = 
map.getLayerPxFromViewPortPx(e.xy) ;
+                                       var loc = map.getLonLatFromPixel( opx );
+                                       openLayersSetMarker( loc );
+                                       numClicks = 0;
+                               });
+                       } else {
+                               clearTimeout(timer);
+                               numClicks = 0;
+                       }
                } );
        }
 

-- 
To view, visit https://gerrit.wikimedia.org/r/377690
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib862cf25a5d6b72855d475cb69b4badb4b16ea7a
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/PageForms
Gerrit-Branch: master
Gerrit-Owner: Yaron Koren <yaro...@gmail.com>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to