Remove control from existing map in onAdd (fixes #3020)
This commit is contained in:
parent
ef96884021
commit
0b86fa3266
@ -30,6 +30,7 @@
|
||||
<script type="text/javascript" src="suites/SpecHelper.js"></script>
|
||||
|
||||
<!-- /control -->
|
||||
<script type="text/javascript" src="suites/control/ControlSpec.js"></script>
|
||||
<script type="text/javascript" src="suites/control/Control.LayersSpec.js"></script>
|
||||
<script type="text/javascript" src="suites/control/Control.ScaleSpec.js"></script>
|
||||
<script type="text/javascript" src="suites/control/Control.AttributionSpec.js"></script>
|
||||
|
29
spec/suites/control/ControlSpec.js
Normal file
29
spec/suites/control/ControlSpec.js
Normal file
@ -0,0 +1,29 @@
|
||||
describe("Control", function () {
|
||||
var map;
|
||||
|
||||
beforeEach(function () {
|
||||
map = L.map(document.createElement('div'));
|
||||
});
|
||||
|
||||
function onAdd() {
|
||||
return L.DomUtil.create('div', 'leaflet-test-control');
|
||||
}
|
||||
|
||||
describe("#addTo", function () {
|
||||
it("adds the container to the map", function () {
|
||||
var control = new L.Control();
|
||||
control.onAdd = onAdd;
|
||||
control.addTo(map);
|
||||
expect(map.getContainer().querySelector('.leaflet-test-control')).to.equal(control.getContainer());
|
||||
});
|
||||
|
||||
it("removes the control from any existing map", function () {
|
||||
var control = new L.Control();
|
||||
control.onAdd = onAdd;
|
||||
control.addTo(map);
|
||||
control.addTo(map);
|
||||
expect(map.getContainer().querySelectorAll('.leaflet-test-control').length).to.equal(1);
|
||||
expect(map.getContainer().querySelector('.leaflet-test-control')).to.equal(control.getContainer());
|
||||
});
|
||||
});
|
||||
});
|
@ -37,6 +37,10 @@ L.Control = L.Class.extend({
|
||||
},
|
||||
|
||||
addTo: function (map) {
|
||||
if (this._map) {
|
||||
this.remove();
|
||||
}
|
||||
|
||||
this._map = map;
|
||||
|
||||
var container = this._container = this.onAdd(map),
|
||||
|
Loading…
Reference in New Issue
Block a user