Add support to DivIcon for reusing a div. Fixes #1753. Still issues remain (Can't change a marker with a DivIcon to have an image Icon or vice versa)

This commit is contained in:
danzel 2013-06-12 15:55:03 +12:00
parent 9f71539b88
commit 7f2eff363b
3 changed files with 70 additions and 2 deletions

View File

@ -61,6 +61,9 @@
<script type="text/javascript" src="suites/layer/TileLayerSpec.js"></script> <script type="text/javascript" src="suites/layer/TileLayerSpec.js"></script>
<script type="text/javascript" src="suites/layer/PopupSpec.js"></script> <script type="text/javascript" src="suites/layer/PopupSpec.js"></script>
<!-- /layer/marker/ -->
<script type="text/javascript" src="suites/layer/marker/MarkerSpec.js"></script>
<!-- /layer/vector/ --> <!-- /layer/vector/ -->
<script type="text/javascript" src="suites/layer/vector/CircleSpec.js"></script> <script type="text/javascript" src="suites/layer/vector/CircleSpec.js"></script>
<script type="text/javascript" src="suites/layer/vector/CircleMarkerSpec.js"></script> <script type="text/javascript" src="suites/layer/vector/CircleMarkerSpec.js"></script>

View File

@ -0,0 +1,63 @@
describe("Marker", function () {
var map,
spy;
beforeEach(function () {
map = L.map(document.createElement('div')).setView([0, 0], 0);
});
describe("#setIcon", function () {
it("changes the icon to another image", function () {
var marker = new L.Marker([0, 0], {icon: new L.Icon({iconUrl: 'icon1.png' }) });
debugger;
map.addLayer(marker);
var beforeIcon = marker._icon;
marker.setIcon(new L.Icon({iconUrl: 'icon2.png' }));
var afterIcon = marker._icon;
expect(beforeIcon).to.be(afterIcon);
expect(afterIcon.src).to.contain('icon2.png');
});
it("changes the icon to another DivIcon", function () {
var marker = new L.Marker([0, 0], {icon: new L.DivIcon({html: 'Inner1Text' }) });
map.addLayer(marker);
var beforeIcon = marker._icon;
marker.setIcon(new L.DivIcon({html: 'Inner2Text' }));
var afterIcon = marker._icon;
expect(beforeIcon).to.be(afterIcon);
expect(afterIcon.innerHTML).to.contain('Inner2Text');
});
it("removes text when changing to a blank DivIcon", function () {
var marker = new L.Marker([0, 0], {icon: new L.DivIcon({html: 'Inner1Text' }) });
map.addLayer(marker);
marker.setIcon(new L.DivIcon());
var afterIcon = marker._icon;
expect(marker._icon.innerHTML).to.not.contain('Inner1Text');
});
it("changes a DivIcon to an image", function () {
var marker = new L.Marker([0, 0], {icon: new L.DivIcon({html: 'Inner1Text' }) });
map.addLayer(marker);
marker.setIcon(new L.Icon({iconUrl: 'icon1.png' }));
expect(marker._icon.afterIcon.src).to.contain('icon2.png');
});
it("changes an image to a DivIcon", function () {
var marker = new L.Marker([0, 0], {icon: new L.Icon({iconUrl: 'icon1.png' }) });
map.addLayer(marker);
marker.setIcon(new L.DivIcon({html: 'Inner1Text' }));
expect(marker._icon.afterIcon.innerHTML).to.contain('Inner1Text');
});
});
});

View File

@ -16,12 +16,14 @@ L.DivIcon = L.Icon.extend({
html: false html: false
}, },
createIcon: function () { createIcon: function (oldIcon) {
var div = document.createElement('div'), var div = oldIcon || document.createElement('div'),
options = this.options; options = this.options;
if (options.html !== false) { if (options.html !== false) {
div.innerHTML = options.html; div.innerHTML = options.html;
} else {
div.innerHTML = '';
} }
if (options.bgPos) { if (options.bgPos) {