Remove control from existing map in onAdd (fixes #3020)

This commit is contained in:
John Firebaugh 2014-11-13 10:49:43 -08:00
parent ef96884021
commit 0b86fa3266
3 changed files with 34 additions and 0 deletions

View File

@ -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>

View 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());
});
});
});

View File

@ -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),