Skip to content

Commit

Permalink
fix: make cyclebuttons work with legend search
Browse files Browse the repository at this point in the history
  • Loading branch information
Grammostola committed Mar 12, 2024
1 parent ffd8b7a commit 4de56c9
Showing 1 changed file with 49 additions and 23 deletions.
72 changes: 49 additions & 23 deletions src/controls/legend.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,36 @@ const Legend = function Legend(options = {}) {
};

const addCycleButtons = (cycleGroups) => {
const cycleAButton = function cycleAButton(button, layer) {
const theButton = button;
theButton.data.activeLayer[0].setVisible(false);

const newIndex = theButton.data.layers.indexOf(layer);
theButton.data.activeLayer[0] = theButton.data.layers[newIndex];
theButton.data.activeLayer[0].setVisible(true);

const styleName = theButton.data.activeLayer[0].get('styleName') || 'default';
const icon = viewer.getStyle(styleName) ? imageSource(viewer.getStyle(styleName)) : 'img/png/farg.png';
theButton.setIcon(icon, theButton.data.activeLayer[0].get('title'));
const element = document.getElementById(theButton.getId());

if (theButton.data.isEven) {
element.classList.remove('cycle-even');
element.classList.add('cycle-odd');
theButton.data.isEven = false;
} else {
element.classList.add('cycle-even');
element.classList.remove('cycle-odd');
theButton.data.isEven = true;
}
};
Object.values(cycleGroups).forEach((cycleGroup) => {
let activeLayer = cycleGroup.find((layer) => layer.get('visible') === true);
activeLayer = activeLayer || cycleGroup[0];
activeLayer = [activeLayer];

let styleName = activeLayer[0].get('styleName') || 'default';
let icon = viewer.getStyle(styleName) ? imageSource(viewer.getStyle(styleName)) : 'img/png/farg.png';
const styleName = activeLayer[0].get('styleName') || 'default';
const icon = viewer.getStyle(styleName) ? imageSource(viewer.getStyle(styleName)) : 'img/png/farg.png';
const title = activeLayer[0].get('title');
const cycleButton = Button({
icon,
Expand All @@ -106,36 +129,33 @@ const Legend = function Legend(options = {}) {
},
methods: {
active: () => {
activeLayer[0].setVisible(true);
if (activeLayer[0].getVisible() === false) activeLayer[0].setVisible(true);
},
initial: () => {
activeLayer[0].setVisible(false);
}
},
click() {
if (this.getState() === 'active') {
this.data.activeLayer[0].setVisible(false);
const currIndex = this.data.layers.indexOf(this.data.activeLayer[0]);
const newIndex = (currIndex + 1) % this.data.numberOfLayers;
this.data.activeLayer[0] = this.data.layers[newIndex];
this.data.activeLayer[0].setVisible(true);

styleName = this.data.activeLayer[0].get('styleName') || 'default';
icon = viewer.getStyle(styleName) ? imageSource(viewer.getStyle(styleName)) : 'img/png/farg.png';
this.setIcon(icon, this.data.activeLayer[0].get('title'));
const element = document.getElementById(this.getId());

if (this.data.isEven) {
element.classList.remove('cycle-even');
element.classList.add('cycle-odd');
this.data.isEven = false;
cycleAButton(this, this.data.layers[newIndex]);
}
}
});
cycleGroup.forEach(layer => {
layer.on('change:visible', () => {
if (layer.getVisible() === true) {
if (layer === cycleButton.data.activeLayer[0]) {
cycleButton.setState('active');
} else {
element.classList.add('cycle-even');
element.classList.remove('cycle-odd');
this.data.isEven = true;
cycleAButton(cycleButton, layer);
cycleButton.setState('active');
}
} else if (!(cycleGroup.some(aLayer => aLayer.getVisible()))) {
cycleButton.setState('initial');
}
}
});
});
cycleButtons.push(cycleButton);
});
Expand All @@ -150,8 +170,12 @@ const Legend = function Legend(options = {}) {
title: layer.get('title'),
state: layer.get('visible') ? 'active' : undefined,
methods: {
active: () => layer.setVisible(true),
initial: () => layer.setVisible(false)
active: () => {
layer.setVisible(true);
},
initial: () => {
layer.setVisible(false);
}
},
click() {
const overlayComponent = visibleLayersControl && visibleLayersViewActive ? visibleOverlaysCmp : overlaysCmp;
Expand Down Expand Up @@ -443,7 +467,9 @@ const Legend = function Legend(options = {}) {
const groupExclusive = (viewer.getGroup(layerGroup) && (viewer.getGroup(layerGroup).exclusive || viewer.getGroup(layerGroup).name === 'background'));
if (groupExclusive) {
const layers = viewer.getLayersByProperty('group', layerGroup);
layers.forEach(l => l.setVisible(false));
layers.forEach(l => {
l.setVisible(false);
});
}
layer.setVisible(true);
document.getElementsByClassName('o-search-layer-field')[0].value = '';
Expand Down

0 comments on commit 4de56c9

Please sign in to comment.