Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make tooltips interactive themselves #7532

Merged
merged 4 commits into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions spec/suites/layer/TooltipSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,29 @@ describe('Tooltip', function () {
expect(map.hasLayer(layer._tooltip)).to.be(false);
});

it("is not interactive by default", function () {
var layer = new L.Marker(center).addTo(map);
var spy = sinon.spy();

layer.bindTooltip('Tooltip', {permanent: true});
layer._tooltip.on('click', spy);
happen.click(layer._tooltip._container);
expect(spy.called).to.be(false);
});

it("can be made interactive", function () {
var layer = new L.Marker(center).addTo(map);
var spy = sinon.spy();

layer.bindTooltip('Tooltip', {permanent: true, interactive: true});
layer._tooltip.on('click', spy);
happen.click(layer._tooltip._container);
expect(spy.calledOnce).to.be(true);
});

it("events are propagated to bound layer", function () {
var layer = new L.Marker(center).addTo(map);
var spy = sinon.spy();
layer.on('click', spy);

layer.bindTooltip('Tooltip', {permanent: true, interactive: true});
Expand Down Expand Up @@ -291,11 +311,14 @@ describe('Tooltip', function () {
});

it("map.openTooltip considers interactive option", function () {
if (!window.getComputedStyle) { this.skip(); } // IE9+

var tooltip = L.tooltip({interactive: true}).setContent('Tooltip');
var spy = sinon.spy();
var tooltip = L.tooltip({interactive: true, permanent: true})
.setContent('Tooltip')
.on('click', spy);
map.openTooltip(tooltip, center);
expect(getComputedStyle(tooltip._container).pointerEvents).to.equal('auto');

happen.click(tooltip._container);
expect(spy.calledOnce).to.be(true);
});

it("can call closeTooltip while not on the map", function () {
Expand Down
16 changes: 15 additions & 1 deletion src/layer/DivOverlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as DomUtil from '../dom/DomUtil';

/*
* @class DivOverlay
* @inherits Layer
* @inherits Interactive layer
* @aka L.DivOverlay
* Base model for L.Popup and L.Tooltip. Inherit from it for custom overlays like plugins.
*/
Expand All @@ -19,6 +19,10 @@ export var DivOverlay = Layer.extend({
// @section
// @aka DivOverlay options
options: {
// @option interactive: Boolean = false
// If true, the popup/tooltip will listen to the mouse events.
interactive: false,

// @option offset: Point = Point(0, 0)
// The offset of the overlay position.
offset: [0, 0],
Expand Down Expand Up @@ -101,6 +105,11 @@ export var DivOverlay = Layer.extend({
}

this.bringToFront();

if (this.options.interactive) {
DomUtil.addClass(this._container, 'leaflet-interactive');
this.addInteractiveTarget(this._container);
}
},

onRemove: function (map) {
Expand All @@ -110,6 +119,11 @@ export var DivOverlay = Layer.extend({
} else {
DomUtil.remove(this._container);
}

if (this.options.interactive) {
DomUtil.removeClass(this._container, 'leaflet-interactive');
this.removeInteractiveTarget(this._container);
}
},

// @namespace DivOverlay
Expand Down
22 changes: 4 additions & 18 deletions src/layer/Tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ export var Tooltip = DivOverlay.extend({
// If true, the tooltip will follow the mouse instead of being fixed at the feature center.
sticky: false,

// @option interactive: Boolean = false
// If true, the tooltip will listen to the feature events.
interactive: false,

// @option opacity: Number = 0.9
// Tooltip container opacity.
opacity: 0.9
Expand All @@ -67,20 +63,15 @@ export var Tooltip = DivOverlay.extend({
DivOverlay.prototype.onAdd.call(this, map);
this.setOpacity(this.options.opacity);

if (this.options.interactive) {
DomUtil.addClass(this._container, 'leaflet-interactive');
if (this._source) {
this._source.addInteractiveTarget(this._container);
}
}

// @namespace Map
// @section Tooltip events
// @event tooltipopen: TooltipEvent
// Fired when a tooltip is opened in the map.
map.fire('tooltipopen', {tooltip: this});

if (this._source) {
this.addEventParent(this._source);

// @namespace Layer
// @section Tooltip events
// @event tooltipopen: TooltipEvent
Expand All @@ -92,20 +83,15 @@ export var Tooltip = DivOverlay.extend({
onRemove: function (map) {
DivOverlay.prototype.onRemove.call(this, map);

if (this.options.interactive) {
DomUtil.removeClass(this._container, 'leaflet-interactive');
if (this._source) {
this._source.removeInteractiveTarget(this._container);
}
}

// @namespace Map
// @section Tooltip events
// @event tooltipclose: TooltipEvent
// Fired when a tooltip in the map is closed.
map.fire('tooltipclose', {tooltip: this});

if (this._source) {
this.removeEventParent(this._source);

// @namespace Layer
// @section Tooltip events
// @event tooltipclose: TooltipEvent
Expand Down