Allow to set interactive from Polygon.setStyle

Instead of only at init.
This commit is contained in:
Yohan Boniface
2024-09-19 10:33:03 +02:00
parent b20dbe1c5f
commit 8c9bd93561
2 changed files with 41 additions and 4 deletions

View File

@ -1,6 +1,8 @@
import {expect} from 'chai';
import {LineUtil, Map, latLng, polygon} from 'leaflet';
import {createContainer, removeMapContainer} from '../../SpecHelper.js';
import sinon from 'sinon';
import UIEventSimulator from 'ui-event-simulator';
describe('Polygon', () => {
let map, container;
@ -364,4 +366,37 @@ describe('Polygon', () => {
}
});
});
describe('#events(interactive=false)', () => {
function p2ll(x, y) {
return map.layerPointToLatLng([x, y]);
}
it('should not fire click when not interactive at init', () => {
const latLngs = [p2ll(0, 0), p2ll(0, 100), p2ll(100, 100), p2ll(100, 0)];
const layer = polygon(latLngs, {interactive: false}).addTo(map);
const spy = sinon.spy();
layer.on('click', spy);
UIEventSimulator.fireAt('click', 50, 50); // Click on the layer.
expect(spy.callCount).to.eql(0);
UIEventSimulator.fireAt('click', 150, 150); // Click outside layer.
expect(spy.callCount).to.eql(0);
});
it('should not fire click when setting interactive=false with setStyle', () => {
const latLngs = [p2ll(0, 0), p2ll(0, 100), p2ll(100, 100), p2ll(100, 0)];
const layer = polygon(latLngs).addTo(map);
const spy = sinon.spy();
layer.on('click', spy);
UIEventSimulator.fireAt('click', 50, 50); // Click on the layer.
expect(spy.callCount).to.eql(1);
UIEventSimulator.fireAt('click', 150, 150); // Click outside layer.
expect(spy.callCount).to.eql(1);
layer.setStyle({interactive: false});
UIEventSimulator.fireAt('click', 50, 50); // Click on the layer.
expect(spy.callCount).to.eql(1);
});
});
});

View File

@ -88,10 +88,6 @@ export const SVG = Renderer.extend({
path.classList.add(...splitWords(layer.options.className));
}
if (layer.options.interactive) {
path.classList.add('leaflet-interactive');
}
this._updateStyle(layer);
this._layers[stamp(layer)] = layer;
},
@ -148,6 +144,12 @@ export const SVG = Renderer.extend({
} else {
path.setAttribute('fill', 'none');
}
if (options.interactive) {
path.classList.add('leaflet-interactive');
} else {
path.classList.remove('leaflet-interactive');
}
},
_updatePoly(layer, closed) {