From b23d83f46416c404874ff57d1819646340297cb7 Mon Sep 17 00:00:00 2001 From: Per Liedman Date: Fri, 2 Jun 2017 11:18:46 +0200 Subject: [PATCH] Don't turn enter keypress into clicks on map (#5507) * Don't turn enter keypress into clicks on map But still preserve functionality to open marker's popup through enter keypress when focused. Fixes #5499. * Clean code Thanks @egoroof * Change name of keypress handler to _onKeyPress * Add unit test --- spec/suites/core/GeneralSpec.js | 11 +++++++++++ spec/suites/layer/PopupSpec.js | 12 ++++++++++++ src/layer/Popup.js | 8 ++++++++ src/map/Map.js | 4 ++-- 4 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 spec/suites/core/GeneralSpec.js diff --git a/spec/suites/core/GeneralSpec.js b/spec/suites/core/GeneralSpec.js new file mode 100644 index 00000000..c955e0ab --- /dev/null +++ b/spec/suites/core/GeneralSpec.js @@ -0,0 +1,11 @@ +describe('General', function () { + describe('noConflict', function () { + var leaflet = L; + + after(function () { + L = leaflet; + }); + + expect(L.noConflict()).to.eql(leaflet); + }); +}); diff --git a/spec/suites/layer/PopupSpec.js b/spec/suites/layer/PopupSpec.js index 15ba044a..a628351b 100644 --- a/spec/suites/layer/PopupSpec.js +++ b/spec/suites/layer/PopupSpec.js @@ -248,6 +248,18 @@ describe('Popup', function () { expect(map.hasLayer(layer._popup)).to.be(true); }); + + it("can open a popup with enter keypress when marker has focus", function () { + var layer = new L.Marker([55.8, 37.6]).addTo(map); + layer.bindPopup("layer popup"); + + happen.keypress(layer._icon, { + keyCode: 13 + }); + + expect(map.hasLayer(layer._popup)).to.be(true); + }); + }); describe("L.Map#openPopup", function () { diff --git a/src/layer/Popup.js b/src/layer/Popup.js index aaac9e03..da52d693 100644 --- a/src/layer/Popup.js +++ b/src/layer/Popup.js @@ -383,6 +383,7 @@ Layer.include({ if (!this._popupHandlersAdded) { this.on({ click: this._openPopup, + keypress: this._onKeyPress, remove: this.closePopup, move: this._movePopup }); @@ -398,6 +399,7 @@ Layer.include({ if (this._popup) { this.off({ click: this._openPopup, + keypress: this._onKeyPress, remove: this.closePopup, move: this._movePopup }); @@ -515,5 +517,11 @@ Layer.include({ _movePopup: function (e) { this._popup.setLatLng(e.latlng); + }, + + _onKeyPress: function (e) { + if (e.originalEvent.keyCode === 13) { + this._openPopup(e); + } } }); diff --git a/src/map/Map.js b/src/map/Map.js index 034ff47b..f1c70c40 100644 --- a/src/map/Map.js +++ b/src/map/Map.js @@ -1307,9 +1307,9 @@ export var Map = Evented.extend({ _handleDOMEvent: function (e) { if (!this._loaded || DomEvent.skipped(e)) { return; } - var type = e.type === 'keypress' && e.keyCode === 13 ? 'click' : e.type; + var type = e.type; - if (type === 'mousedown') { + if (type === 'mousedown' || type === 'keypress') { // prevents outline when clicking on keyboard-focusable element DomUtil.preventOutline(e.target || e.srcElement); }