Skip to content

Commit

Permalink
Fix Bug: permanent & sticky tooltip (#7563)
Browse files Browse the repository at this point in the history
* Fixes bug: if tooltip is sticky and permanent it was not following the mouse

* Add tests

* Use fastframe

* fix whitespace

Co-authored-by: Vladimir Agafonkin <agafonkin@gmail.com>
  • Loading branch information
Falke-Design and mourner authored Dec 2, 2021
1 parent 4b2946c commit 30d91b1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
29 changes: 29 additions & 0 deletions spec/suites/layer/TooltipSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,35 @@ describe('Tooltip', function () {
layer.closeTooltip();
});

it("opens a tooltip and follow the mouse (sticky)", function () {
var layer = L.rectangle([[58, 39.7], [54, 35.3]]).addTo(map);
layer.bindTooltip('Sticky', {sticky: true}).openTooltip();
var tooltip = layer.getTooltip();
expect(tooltip.getLatLng().equals(layer.getCenter())).to.be(true);

happen.at('click', 120, 120);
var latlng = map.containerPointToLatLng([120, 120]);
expect(tooltip.getLatLng().equals(latlng)).to.be(true);
});

it("opens a permanent tooltip and follow the mouse (sticky)", function (done) {
var layer = L.rectangle([[58, 39.7], [54, 35.3]]).addTo(map);
layer.bindTooltip('Sticky', {sticky: true, permanent: true}).openTooltip();
var tooltip = layer.getTooltip();
expect(tooltip.getLatLng().equals(layer.getCenter())).to.be(true);

var hand = new Hand({
timing: 'fastframe',
onStop: function () {
var latlng = map.containerPointToLatLng([120, 120]);
expect(tooltip.getLatLng().equals(latlng)).to.be(true);
done();
}
});
var toucher = hand.growFinger('mouse');
toucher.wait(100).moveTo(120, 120, 1000).wait(100);
});

it("closes existent tooltip on new bindTooltip call", function () {
var layer = new L.Marker(center).addTo(map);
var eventSpy = sinon.spy(layer, "unbindTooltip");
Expand Down
6 changes: 3 additions & 3 deletions src/layer/Tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,13 +322,13 @@ Layer.include({
if (!this._tooltip.options.permanent) {
events.mouseover = this._openTooltip;
events.mouseout = this.closeTooltip;
if (this._tooltip.options.sticky) {
events.mousemove = this._moveTooltip;
}
events.click = this._openTooltip;
} else {
events.add = this._openTooltip;
}
if (this._tooltip.options.sticky) {
events.mousemove = this._moveTooltip;
}
this[onOff](events);
this._tooltipHandlersAdded = !remove;
},
Expand Down

0 comments on commit 30d91b1

Please sign in to comment.