Skip to content

Commit 6772362

Browse files
committed
fix layer switcher's disable checkbox and label style
1 parent 057b255 commit 6772362

File tree

3 files changed

+42
-15
lines changed

3 files changed

+42
-15
lines changed

assets/css/maptalks-control.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
.maptalks-layer-switcher label {
8282
text-overflow: ellipsis;
8383
overflow: hidden;
84+
width : 90%;
8485
display: inline-block;
8586
font-size : 14px;
8687
white-space: nowrap;

src/control/Control.LayerSwitcher.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,11 @@ class LayerSwitcher extends Control {
131131
const li = createEl('li', 'layer'),
132132
label = createEl('label'),
133133
input = createEl('input'),
134-
map = this.getMap(),
135-
visible = layer.isVisible();
134+
map = this.getMap();
135+
const visible = layer.options['visible'];
136+
layer.options['visible'] = true;
137+
const enabled = layer.isVisible();
138+
layer.options['visible'] = visible;
136139
li.className = 'layer';
137140
if (isBase) {
138141
input.type = 'radio';
@@ -141,10 +144,10 @@ class LayerSwitcher extends Control {
141144
input.type = 'checkbox';
142145
}
143146

144-
input.checked = visible;
145-
// if (!visible) {
146-
// input.setAttribute('disabled', 'disabled');
147-
// }
147+
input.checked = visible && enabled;
148+
if (!enabled) {
149+
input.setAttribute('disabled', 'disabled');
150+
}
148151

149152
input.onchange = function (e) {
150153
if (e.target.type === 'radio') {

test/map/control/Control.LayerSwitcherSpec.js

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,11 @@ describe('Control.LayerSwitcher', function () {
9292
expect(radios[1].checked).not.to.be.ok();
9393
expect(group.layers[0].isVisible()).to.be.ok();
9494
expect(group.layers[1].isVisible()).not.to.be.ok();
95-
radios[1].addEventListener("click", function() {
96-
expect(radios[0].checked).not.to.be.ok();
97-
expect(radios[1].checked).to.be.ok();
98-
expect(group.layers[0].isVisible()).not.to.be.ok();
99-
expect(group.layers[1].isVisible()).to.be.ok();
100-
});
95+
happen.click(radios[1]);
96+
expect(radios[0].checked).not.to.be.ok();
97+
expect(radios[1].checked).to.be.ok();
98+
expect(group.layers[0].isVisible()).not.to.be.ok();
99+
expect(group.layers[1].isVisible()).to.be.ok();
101100
});
102101

103102
it('overlay layers switch', function () {
@@ -114,10 +113,12 @@ describe('Control.LayerSwitcher', function () {
114113
var checkbox = document.querySelector('.layer input');
115114
expect(checkbox.checked).to.be.ok();
116115
expect(tile1.isVisible()).to.be.ok();
117-
checkbox.addEventListener("click", function() {
118-
expect(checkbox.checked).not.to.be.ok();
119-
expect(tile1.isVisible()).not.to.be.ok();
116+
happen.click(checkbox, {
117+
'clientX' : 100,
118+
'clientY' : 100
120119
});
120+
expect(checkbox.checked).not.to.be.ok();
121+
expect(tile1.isVisible()).not.to.be.ok();
121122
});
122123

123124
it('show and hide', function () {
@@ -155,4 +156,26 @@ describe('Control.LayerSwitcher', function () {
155156
});
156157
control.remove();
157158
});
159+
160+
it('disable if layer is transparent or out of zoom', function () {
161+
var control = new maptalks.control.LayerSwitcher();
162+
map.addControl(control);
163+
var tile1 = new maptalks.TileLayer('tile1', {
164+
opacity : 0,
165+
urlTemplate : '/resources/tile.png'
166+
});
167+
var tile2 = new maptalks.TileLayer('tile2', {
168+
minZoom : 1,
169+
maxZoom : 1,
170+
urlTemplate : '/resources/tile.png'
171+
});
172+
map.addLayer(tile1, tile2);
173+
happen.mouseover(control.button, {
174+
'clientX' : 100,
175+
'clientY' : 100
176+
});
177+
var checkbox = document.querySelectorAll('.layer input');
178+
expect(checkbox[0].getAttribute('disabled')).to.be.eql('disabled');
179+
expect(checkbox[1].getAttribute('disabled')).to.be.eql('disabled');
180+
});
158181
});

0 commit comments

Comments
 (0)