From 2b2d93f40e27584002de2d8c050c894f399158fd Mon Sep 17 00:00:00 2001 From: Jesse V Griffis Date: Wed, 3 Jun 2015 16:14:55 -0400 Subject: [PATCH 01/15] prevent singleclick from firing on drag events --- singleclick.js | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/singleclick.js b/singleclick.js index 319b428..0218d78 100644 --- a/singleclick.js +++ b/singleclick.js @@ -1,27 +1,33 @@ L.Map.addInitHook( function () { - + var that = this , h + , ignoreDragging = false ; - + if (that.on) { that.on( 'click', check_later ); that.on( 'dblclick', function () { setTimeout( clear_h, 0 ); } ); + that.on( 'dragstart', function () { ignoreDragging = true; } ); } - + function check_later( e ) { clear_h(); - + h = setTimeout( check, 500 ); - + function check() { - that.fire( 'singleclick', L.Util.extend( e, { type : 'singleclick' } ) ); + if (!ignoreDragging) { + that.fire( 'singleclick', L.Util.extend( e, { type : 'singleclick' } ) ); + } else { + ignoreDragging = false; + } } } - + function clear_h() { if (h != null) @@ -30,5 +36,5 @@ L.Map.addInitHook( function () { h = null; } } - + }); From 30646efb9cae18758571a1799ad7787a0e09eda5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20S=C3=A1nchez=20Ortega?= Date: Tue, 29 Sep 2015 10:41:05 +0200 Subject: [PATCH 02/15] Adapt to 1.0.0-beta1, add bower.json --- .gitignore | 1 + README.md | 13 +++++++---- bower.json | 25 ++++++++++++++++++++ index.html | 62 +++++++++++++++++++++++--------------------------- singleclick.js | 59 +++++++++++++++++++++-------------------------- 5 files changed, 89 insertions(+), 71 deletions(-) create mode 100644 bower.json diff --git a/.gitignore b/.gitignore index 23141bb..4a12ed2 100644 --- a/.gitignore +++ b/.gitignore @@ -10,3 +10,4 @@ DIRECTIONS.org *.exe *.obj *.out +*.bower_components diff --git a/README.md b/README.md index 2150caa..aacf83b 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,14 @@ -# singleclick for Leaflet 0.7.* +# singleclick for Leaflet 1.0.0-beta1 and greater -This plugin extends `L.Map` to fire the `singleclick` event. The `singleclick` event is not part of Leaflet's core, and most likely never will be: [#108](https://github.com/Leaflet/Leaflet/issues/108) +This plugin extends `L.Evented` to fire the `singleclick` event. A `singleclick` happens when clicking on something but not double-clicking for 500msec. +The timeout can be configured by setting the `singleClickTimeout` option on the relevant `L.Evented`, like so: - -Note: For Leaflet 0.8 it could be possible to extend `L.Evented` instead, to have also Marker etc. support the `singleclick` event. +```js +marker.options.singleClickTimeout = 250; +marker.on('singleclick', function(ev){ ... } ); +``` ## live example -http://alpstein.github.io/leaflet-singleclick_0.7/ +http://mazemap.github.io/Leaflet.singleclick/ diff --git a/bower.json b/bower.json new file mode 100644 index 0000000..d9399cc --- /dev/null +++ b/bower.json @@ -0,0 +1,25 @@ +{ + "name": "Leaflet.singleclick", + "version": "0.1.0", + "homepage": "https://github.com/MazeMap/Leaflet.singleclick", + "authors": [ + "Iván Sánchez Ortega " + ], + "description": "Allows Leaflet layers to fire 'singleclick' events", + "main": "singleclick.js", + "keywords": [ + "leaflet", + "events" + ], + "license": "Apache", + "ignore": [ + "**/.*", + "node_modules", + "bower_components", + "test", + "tests" + ], + "dependencies": { + "leaflet": "~1.0.0-beta.1" + } +} diff --git a/index.html b/index.html index ea56652..927e3f6 100644 --- a/index.html +++ b/index.html @@ -1,45 +1,41 @@ - - singleclick example - - - - - - -
- - - - - - + + +
+ + + + - + + + diff --git a/singleclick.js b/singleclick.js index 319b428..a980d72 100644 --- a/singleclick.js +++ b/singleclick.js @@ -1,34 +1,27 @@ -L.Map.addInitHook( function () { - - var that = this - , h - ; - - if (that.on) - { - that.on( 'click', check_later ); - that.on( 'dblclick', function () { setTimeout( clear_h, 0 ); } ); - } - - function check_later( e ) - { - clear_h(); - - h = setTimeout( check, 500 ); - - function check() - { - that.fire( 'singleclick', L.Util.extend( e, { type : 'singleclick' } ) ); - } - } - - function clear_h() - { - if (h != null) - { - clearTimeout( h ); - h = null; - } - } - + + +L.Evented.addInitHook( function () { + this.on('click', this._scheduleSingleClick, this); + this.on('dblclick', this._cancelSingleClick, this); + + this._singleClickTimeout = null; }); + +L.Evented.include({ + + _scheduleSingleClick: function(ev) { + this._cancelSingleClick(); + this._singleClickTimeout = window.setTimeout( + L.bind(this._fireSingleClick, this, ev), + this.options.singleClickTimeout || 500); + }, + + _cancelSingleClick: function() { + window.clearTimeout(this._singleClickTimeout); + }, + + _fireSingleClick: function(ev) { + this.fire('singleclick', L.Util.extend(ev, {type: 'singleclick'})); + } +}) + From 7d9cd5ea5be7a40d2df49072f72057b5c710b1dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20S=C3=A1nchez=20Ortega?= Date: Tue, 29 Sep 2015 10:44:11 +0200 Subject: [PATCH 03/15] Fix readme --- README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index aacf83b..1631c4e 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ -# singleclick for Leaflet 1.0.0-beta1 and greater +# Leaflet.singleclick + + This plugin extends `L.Evented` to fire the `singleclick` event. A `singleclick` happens when clicking on something but not double-clicking for 500msec. @@ -9,6 +11,8 @@ marker.options.singleClickTimeout = 250; marker.on('singleclick', function(ev){ ... } ); ``` -## live example +Works with Leaflet 1.0.0-beta1 and greater. Does **not** work with 0.7.x. + +## Live example http://mazemap.github.io/Leaflet.singleclick/ From 9dd5f92e2558240233daea9c773c6ea05444cbe3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20S=C3=A1nchez=20Ortega?= Date: Tue, 29 Sep 2015 13:07:06 +0200 Subject: [PATCH 04/15] Improve demo, do not fire event if original event has been stopped. --- index.html | 15 +++++++++++++-- singleclick.js | 4 +++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/index.html b/index.html index 927e3f6..6d88da9 100644 --- a/index.html +++ b/index.html @@ -29,12 +29,23 @@ map.options.singleClickTimeout = 250; map.on('singleclick',function ( e ) { - console.log( 'singleclick', e ); + console.log( 'map singleclick', e ); L.popup().setLatLng( e.latlng ) - .setContent( '

singleclick at ' + e.latlng ) + .setContent( '

Basemap singleclick at ' + e.latlng ) .openOn( map ); } ); +var group = L.featureGroup().addTo(map); + +var circle = L.circle([51.505, -0.09], 1000).addTo(group).on('singleclick', function(ev) { + console.log( 'circle singleclick', ev ); + L.DomEvent.stop(ev); + L.popup().setLatLng( ev.latlng ) + .setContent( '

Circle singleclick at ' + ev.latlng ) + .openOn( map ); +}); +circle.options.singleClickTimeout = 250; + diff --git a/singleclick.js b/singleclick.js index a980d72..54ff047 100644 --- a/singleclick.js +++ b/singleclick.js @@ -21,7 +21,9 @@ L.Evented.include({ }, _fireSingleClick: function(ev) { - this.fire('singleclick', L.Util.extend(ev, {type: 'singleclick'})); + if (!ev.originalEvent._stopped) { + this.fire('singleclick', L.Util.extend(ev, {type: 'singleclick'})); + } } }) From 16769c103a4c898daea37644eb04d4bb7b91b4a5 Mon Sep 17 00:00:00 2001 From: Dag Jomar Mersland Date: Mon, 19 Oct 2015 04:01:23 +0200 Subject: [PATCH 05/15] Workaround for click-click-dblclick event order 'bug' using timeout logic from 'smartClick' controller. Fixes #1 --- singleclick.js | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/singleclick.js b/singleclick.js index 54ff047..b144606 100644 --- a/singleclick.js +++ b/singleclick.js @@ -1,28 +1,42 @@ L.Evented.addInitHook( function () { + this.on('click', this._scheduleSingleClick, this); this.on('dblclick', this._cancelSingleClick, this); this._singleClickTimeout = null; + this._cancel = false; + this._defaultTimeout = 500; }); L.Evented.include({ _scheduleSingleClick: function(ev) { - this._cancelSingleClick(); + this._cancelSingleClick(false); + this._singleClickTimeout = window.setTimeout( L.bind(this._fireSingleClick, this, ev), - this.options.singleClickTimeout || 500); + this.options.singleClickTimeout || this._defaultTimeout); }, - _cancelSingleClick: function() { + _cancelSingleClick: function(t) { window.clearTimeout(this._singleClickTimeout); + if(t !== false){ + this._cancel = true; + setTimeout(function(){ this._cancel=false; }.bind(this), (this.options.singleClickTimeout ? this.options.singleClickTimeout + 50 : this._defaultTimeout + 50) ); + } }, _fireSingleClick: function(ev) { - if (!ev.originalEvent._stopped) { - this.fire('singleclick', L.Util.extend(ev, {type: 'singleclick'})); + if (!ev.originalEvent._stopped && !this._cancel) { + + setTimeout(function(){ + if(!this._cancel){ + this.fire('singleclick', L.Util.extend(ev, {type: 'singleclick'})); + } + }.bind(this), 50); + } } }) From ce5d4a88a2d6df1636fd1c6133d109f960715392 Mon Sep 17 00:00:00 2001 From: Dag Jomar Mersland Date: Mon, 19 Oct 2015 04:34:49 +0200 Subject: [PATCH 06/15] Fixing it even better by just implementing the original singleclick-code which was not broken --- singleclick.js | 50 ++++++++++++++++++++------------------------------ 1 file changed, 20 insertions(+), 30 deletions(-) diff --git a/singleclick.js b/singleclick.js index b144606..0b05493 100644 --- a/singleclick.js +++ b/singleclick.js @@ -1,43 +1,33 @@ - - L.Evented.addInitHook( function () { + this.h = null; + this.on( 'click', this.check_later, this ); + this.on( 'dblclick', this.timeoutClear, this ); - this.on('click', this._scheduleSingleClick, this); - this.on('dblclick', this._cancelSingleClick, this); - - this._singleClickTimeout = null; - this._cancel = false; - this._defaultTimeout = 500; }); L.Evented.include({ - _scheduleSingleClick: function(ev) { - this._cancelSingleClick(false); - - this._singleClickTimeout = window.setTimeout( - L.bind(this._fireSingleClick, this, ev), - this.options.singleClickTimeout || this._defaultTimeout); + timeoutClear : function(){ + setTimeout( this.clear_h.bind(this), 0 ); }, - _cancelSingleClick: function(t) { - window.clearTimeout(this._singleClickTimeout); - if(t !== false){ - this._cancel = true; - setTimeout(function(){ this._cancel=false; }.bind(this), (this.options.singleClickTimeout ? this.options.singleClickTimeout + 50 : this._defaultTimeout + 50) ); - } - }, + check_later: function(e) { + this.clear_h(); + + this.h = setTimeout( this.check.bind(this, e), (this.options.singleClickTimeout || 500) ); + }, - _fireSingleClick: function(ev) { - if (!ev.originalEvent._stopped && !this._cancel) { + check: function(e){ + this.fire( 'singleclick', L.Util.extend( e, { type : 'singleclick' } ) ); + }, - setTimeout(function(){ - if(!this._cancel){ - this.fire('singleclick', L.Util.extend(ev, {type: 'singleclick'})); - } - }.bind(this), 50); + clear_h: function(){ + if (this.h != null) + { + clearTimeout( this.h ); + this.h = null; + } + } - } - } }) From 6bd1ce0457fc870a1aa34742f069f013b981e5be Mon Sep 17 00:00:00 2001 From: Dag Jomar Mersland Date: Mon, 19 Oct 2015 04:47:23 +0200 Subject: [PATCH 07/15] Rewriting functions to more human-readable names again --- singleclick.js | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/singleclick.js b/singleclick.js index 0b05493..9cbb228 100644 --- a/singleclick.js +++ b/singleclick.js @@ -1,31 +1,32 @@ L.Evented.addInitHook( function () { - this.h = null; - this.on( 'click', this.check_later, this ); - this.on( 'dblclick', this.timeoutClear, this ); + this._timerId = null; + this.on( 'click', this._scheduleSingleClick, this ); + this.on( 'dblclick', this._cancelSingleClick, this ); }); L.Evented.include({ - timeoutClear : function(){ - setTimeout( this.clear_h.bind(this), 0 ); + _cancelSingleClick : function(){ + //This timeout is key to workaround an issue where double-click events are fired in this order on touch browsers: ['click', 'dblclick', 'click'] instead of ['click', 'click', 'dblclick'] + setTimeout( this._clearTimeout.bind(this), 0 ); }, - check_later: function(e) { - this.clear_h(); + _scheduleSingleClick: function(e) { + this._clearTimeout(); - this.h = setTimeout( this.check.bind(this, e), (this.options.singleClickTimeout || 500) ); + this._timerId = setTimeout( this._fireSingleClick.bind(this, e), (this.options.singleClickTimeout || 500) ); }, - check: function(e){ + _fireSingleClick: function(e){ this.fire( 'singleclick', L.Util.extend( e, { type : 'singleclick' } ) ); }, - clear_h: function(){ - if (this.h != null) + _clearTimeout: function(){ + if (this._timerId != null) { - clearTimeout( this.h ); - this.h = null; + clearTimeout( this._timerId ); + this._timerId = null; } } From d9c102302b690ea5a03172cee5ee27effd75d268 Mon Sep 17 00:00:00 2001 From: Dag Jomar Mersland Date: Mon, 19 Oct 2015 05:12:56 +0200 Subject: [PATCH 08/15] re-adding the check for event._stopped --- singleclick.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/singleclick.js b/singleclick.js index 9cbb228..5f92b14 100644 --- a/singleclick.js +++ b/singleclick.js @@ -19,7 +19,9 @@ L.Evented.include({ }, _fireSingleClick: function(e){ - this.fire( 'singleclick', L.Util.extend( e, { type : 'singleclick' } ) ); + if ( !e.originalEvent._stopped ) { + this.fire( 'singleclick', L.Util.extend( e, { type : 'singleclick' } ) ); + } }, _clearTimeout: function(){ From 52eb3168d0b759ce7580487bc1a575f515f0248c Mon Sep 17 00:00:00 2001 From: Dag Jomar Mersland Date: Mon, 19 Oct 2015 05:21:12 +0200 Subject: [PATCH 09/15] update version --- bower.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bower.json b/bower.json index d9399cc..e0ce0f2 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "Leaflet.singleclick", - "version": "0.1.0", + "version": "0.3.0", "homepage": "https://github.com/MazeMap/Leaflet.singleclick", "authors": [ "Iván Sánchez Ortega " From f432483f5960357e5345507002d79d778baea9be Mon Sep 17 00:00:00 2001 From: Dag Jomar Mersland Date: Mon, 19 Oct 2015 05:02:16 +0200 Subject: [PATCH 10/15] Ignore singleclick on interaction events such as dragstart and zoomstart Inspired by https://github.com/jvgriffis/leaflet-singleclick_0.7/commit/2b2d93f40e27584002de2d8c050c894f399158fd --- singleclick.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/singleclick.js b/singleclick.js index 5f92b14..15014ee 100644 --- a/singleclick.js +++ b/singleclick.js @@ -2,7 +2,8 @@ L.Evented.addInitHook( function () { this._timerId = null; this.on( 'click', this._scheduleSingleClick, this ); this.on( 'dblclick', this._cancelSingleClick, this ); - + this.on( 'dragstart', this._cancelSingleClick, this ); + this.on( 'zoomstart', this._cancelSingleClick, this ); }); L.Evented.include({ From 669313e194042a6f8a28c9b43e77862d54eee456 Mon Sep 17 00:00:00 2001 From: Dag Jomar Mersland Date: Mon, 19 Oct 2015 05:32:57 +0200 Subject: [PATCH 11/15] Update version --- bower.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bower.json b/bower.json index e0ce0f2..917741f 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "Leaflet.singleclick", - "version": "0.3.0", + "version": "0.4.0", "homepage": "https://github.com/MazeMap/Leaflet.singleclick", "authors": [ "Iván Sánchez Ortega " From 41b423c90d27889ee151396925fcef2dbacaeeb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20S=C3=A1nchez=20Ortega?= Date: Mon, 19 Oct 2015 11:39:05 +0200 Subject: [PATCH 12/15] Beautify code: unique-r method names, joint event declaration, line width --- singleclick.js | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/singleclick.js b/singleclick.js index 15014ee..97a59dd 100644 --- a/singleclick.js +++ b/singleclick.js @@ -1,22 +1,25 @@ L.Evented.addInitHook( function () { - this._timerId = null; + this._singleClickTimeout = null; this.on( 'click', this._scheduleSingleClick, this ); - this.on( 'dblclick', this._cancelSingleClick, this ); - this.on( 'dragstart', this._cancelSingleClick, this ); - this.on( 'zoomstart', this._cancelSingleClick, this ); + this.on( 'dblclick dragstart zoomstart', this._cancelSingleClick, this ); }); L.Evented.include({ - _cancelSingleClick : function(){ - //This timeout is key to workaround an issue where double-click events are fired in this order on touch browsers: ['click', 'dblclick', 'click'] instead of ['click', 'click', 'dblclick'] - setTimeout( this._clearTimeout.bind(this), 0 ); - }, + _cancelSingleClick : function(){ + // This timeout is key to workaround an issue where double-click events + // are fired in this order on some touch browsers: ['click', 'dblclick', 'click'] + // instead of ['click', 'click', 'dblclick'] + setTimeout( this._clearSingleClickTimeout.bind(this), 0 ); + }, _scheduleSingleClick: function(e) { - this._clearTimeout(); + this._clearSingleClickTimeout(); - this._timerId = setTimeout( this._fireSingleClick.bind(this, e), (this.options.singleClickTimeout || 500) ); + this._singleClickTimeout = setTimeout( + this._fireSingleClick.bind(this, e), + (this.options.singleClickTimeout || 500) + ); }, _fireSingleClick: function(e){ @@ -25,13 +28,12 @@ L.Evented.include({ } }, - _clearTimeout: function(){ - if (this._timerId != null) - { - clearTimeout( this._timerId ); - this._timerId = null; + _clearSingleClickTimeout: function(){ + if (this._singleClickTimeout != null) { + clearTimeout( this._singleClickTimeout ); + this._singleClickTimeout = null; } } -}) +}); From a9964d876577cd1c510a0ad780ab20bba4d7818b Mon Sep 17 00:00:00 2001 From: Lizzi Slivinski Date: Mon, 8 Feb 2016 10:48:13 -0500 Subject: [PATCH 13/15] reset ignore dragging on dragend --- singleclick.js | 1 + 1 file changed, 1 insertion(+) diff --git a/singleclick.js b/singleclick.js index 0218d78..db87d72 100644 --- a/singleclick.js +++ b/singleclick.js @@ -10,6 +10,7 @@ L.Map.addInitHook( function () { that.on( 'click', check_later ); that.on( 'dblclick', function () { setTimeout( clear_h, 0 ); } ); that.on( 'dragstart', function () { ignoreDragging = true; } ); + that.on( 'dragend', function () { ignoreDragging = false; } ); } function check_later( e ) From bd2a3e28f2baa33461e4579ed6555d638095fb33 Mon Sep 17 00:00:00 2001 From: Jesse G Date: Wed, 30 Aug 2017 14:26:36 -0400 Subject: [PATCH 14/15] added package.json --- package.json | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 package.json diff --git a/package.json b/package.json new file mode 100644 index 0000000..d1ef70c --- /dev/null +++ b/package.json @@ -0,0 +1,4 @@ +{ + "name": "leaflet-singleclick", + "version": "0.5.0" +} From c3745916c5f8ded86f8c9a15fb28054264fd23e8 Mon Sep 17 00:00:00 2001 From: jvgriffis Date: Wed, 30 Aug 2017 14:29:45 -0400 Subject: [PATCH 15/15] Update package.json --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index d1ef70c..c065ee6 100644 --- a/package.json +++ b/package.json @@ -1,4 +1,5 @@ { "name": "leaflet-singleclick", - "version": "0.5.0" + "version": "0.5.0", + "main": "singleclick.js" }