Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: new group options #1910

Merged
merged 7 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/controls/legend.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,7 @@ const Legend = function Legend(options = {}) {
},
getuseGroupIndication() { return useGroupIndication; },
getOverlaysCollapse() { return overlaysCmp.overlaysCollapse; },
getOverlays() { return overlaysCmp; },
setVisibleLayersViewActive,
addButtonToTools(button, buttonGroup) {
if (buttonGroup === 'addLayerButton') {
Expand Down
51 changes: 49 additions & 2 deletions src/controls/legend/group.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Component, Button, Collapse, CollapseHeader, dom } from '../../ui';
import GroupList from './grouplist';
import createMoreInfoButton from './moreinfobutton';
import LayerProperties from './overlayproperties';

/**
* The Group component can be a group or a subgroup,
Expand All @@ -22,7 +24,10 @@ const Group = function Group(viewer, options = {}) {
exclusive = false,
toggleAll = true,
draggable = false,
zIndexStart = 0.1
zIndexStart = 0.1,
opacityControl = false,
zoomToExtent = false,
description
} = options;

const stateCls = {
Expand All @@ -40,6 +45,8 @@ const Group = function Group(viewer, options = {}) {
const groupList = GroupList({ viewer, cls: listCls, abstract });
visibleState = groupList.getVisible();

const thisGroup = viewer.getGroup(name);

const getEl = () => groupEl;

const getCheckIcon = (visible) => {
Expand Down Expand Up @@ -70,6 +77,9 @@ const Group = function Group(viewer, options = {}) {
}
}) : false;

const moreInfoButton = (opacityControl || zoomToExtent || description) ? createMoreInfoButton({ viewer,
group: thisGroup }) : false;

const SubGroupHeader = function SubGroupHeader() {
const expandButton = Button({
cls: 'icon-small compact round',
Expand All @@ -86,6 +96,9 @@ const Group = function Group(viewer, options = {}) {
if (tickButton) {
this.addComponent(tickButton);
}
if (moreInfoButton) {
this.addComponent(moreInfoButton);
}
},
onRender() {
this.dispatch('render');
Expand All @@ -99,12 +112,14 @@ const Group = function Group(viewer, options = {}) {
});
},
render() {
return `<div class="flex row align-center padding-left text-smaller pointer collapse-header" style="width: 100%; padding-right: 1.875rem">
const padding = moreInfoButton ? '0.275rem' : '1.875rem';
return `<div class="flex row align-center padding-left text-smaller pointer collapse-header item wrap" style="width: 100%; padding-right: ${padding}">
<div id="${this.getId()}" class="flex row align-center grow">
${expandButton.render()}
<span class="grow padding-x-small" style="word-break: break-all;">${title}</span>
</div>
${tickButton ? tickButton.render() : ''}
${moreInfoButton ? moreInfoButton.render() : ''}
</div>`;
}
});
Expand Down Expand Up @@ -348,6 +363,38 @@ const Group = function Group(viewer, options = {}) {
e.stopPropagation();
}
});
groupEl.addEventListener('overlayproperties', (evt) => {
const overlaysCmp = viewer.getControlByName('legend').getOverlays();
const slidenav = overlaysCmp.slidenav;
if (evt.detail.group) {
const group = evt.detail.group;
const thisParent = this;
const label = group.labelOpacitySlider ? group.labelOpacitySlider : '';
const layerProperties = LayerProperties({
group, viewer, thisParent, labelOpacitySlider: label
});
slidenav.setSecondary(layerProperties);
slidenav.slideToSecondary();
// Include back btn and opacity slider in tab order when opened and remove when closed
const secondaryEl = document.getElementById(slidenav.getId()).querySelector('.secondary');
const backBtn = secondaryEl.getElementsByTagName('button')[0];
const opacityInput = secondaryEl.getElementsByTagName('input')[0];
backBtn.tabIndex = 0;
backBtn.focus();
if (opacityInput) {
opacityInput.tabIndex = 0;
}
backBtn.addEventListener('click', () => {
backBtn.tabIndex = -99;
if (opacityInput) {
opacityInput.tabIndex = -99;
}
}, false);
slidenav.on('slide', () => {
groupEl.classList.remove('width-100');
});
}
});
}
this.dispatch('render');
},
Expand Down
189 changes: 189 additions & 0 deletions src/controls/legend/moreinfobutton.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
import { Component, Button, dom } from '../../ui';
import PopupMenu from '../../ui/popupmenu';
import exportToFile from '../../utils/exporttofile';

export default function createMoreInfoButton(params) {
const {
layer,
group = {},
viewer
} = params;
const popupMenuItems = [];
let moreInfoButton;
let popupMenu;
const showPopup = group.zoomToExtent && group.extent; // In case of zoomToExtent we always want to show popupmenu

const eventOverlayProps = new CustomEvent('overlayproperties', {
bubbles: true,
detail: {
layer,
group
}
});

if (layer || group.opacityControl || group.description) {
const layerInfoMenuItem = Component({
onRender() {
const labelEl = document.getElementById(this.getId());
labelEl.addEventListener('click', (e) => {
popupMenu.setVisibility(false);
document.getElementById(moreInfoButton.getId()).dispatchEvent(eventOverlayProps);
e.preventDefault();
});
},
render() {
const labelCls = 'text-smaller padding-x-small grow pointer no-select overflow-hidden';
return `<li id="${this.getId()}" class="${labelCls}">Visa ${layer ? 'lagerinformation' : 'gruppinformation'}</li>`;
}
});
popupMenuItems.push(layerInfoMenuItem);
}

if ((layer && layer.get('zoomToExtent')) || (group.zoomToExtent && group.extent)) {
const zoomToExtentMenuItem = Component({
onRender() {
const labelEl = document.getElementById(this.getId());
labelEl.addEventListener('click', (e) => {
if (layer) {
const extent = typeof layer.getSource !== 'undefined' && typeof layer.getSource().getExtent !== 'undefined' ? layer.getSource().getExtent() : layer.getExtent();
if (layer.getVisible()) {
viewer.getMap().getView().fit(extent, {
padding: [50, 50, 50, 50],
duration: 1000
});
e.preventDefault();
}
} else if (group.zoomToExtent) {
const extent = group.extent;
viewer.getMap().getView().fit(extent, {
padding: [50, 50, 50, 50],
duration: 1000
});
e.preventDefault();
}
});
},
render() {
const labelCls = 'text-smaller padding-x-small grow pointer no-select overflow-hidden';
return `<li id="${this.getId()}" class="${labelCls}">Zooma till</li>`;
}
});
popupMenuItems.push(zoomToExtentMenuItem);
}

if (layer && layer.get('exportable')) {
const exportFormat = layer.get('exportFormat') || layer.get('exportformat');
let exportFormatArray = [];
if (exportFormat && typeof exportFormat === 'string') {
exportFormatArray.push(exportFormat);
} else if (exportFormat && Array.isArray(exportFormat)) {
exportFormatArray = exportFormat;
}
const formats = exportFormatArray.map(format => format.toLowerCase()).filter(format => format === 'geojson' || format === 'gpx' || format === 'kml');
if (formats.length === 0) { formats.push('geojson'); }
formats.forEach((format) => {
const exportLayerMenuItem = Component({
onRender() {
const labelEl = document.getElementById(this.getId());
labelEl.addEventListener('click', (e) => {
const features = layer.getSource().getFeatures();
exportToFile(features, format, {
featureProjection: viewer.getProjection().getCode(),
filename: layer.get('title') || 'export'
});
e.preventDefault();
});
},
render() {
let exportLabel;
if (exportFormatArray.length > 1) {
exportLabel = `Spara lager (.${format})`;
} else { exportLabel = 'Spara lager'; }
const labelCls = 'text-smaller padding-x-small grow pointer no-select overflow-hidden';
return `<li id="${this.getId()}" class="${labelCls}">${exportLabel}</li>`;
}
});
popupMenuItems.push(exportLayerMenuItem);
});
}

if (layer && layer.get('removable')) {
const removeLayerMenuItem = Component({
onRender() {
const labelEl = document.getElementById(this.getId());
labelEl.addEventListener('click', (e) => {
const doRemove = (layer.get('promptlessRemoval') === true) || window.confirm('Vill du radera lagret?');
if (doRemove) {
viewer.getMap().removeLayer(layer);
e.preventDefault();
}
});
},
render() {
const labelCls = 'text-smaller padding-x-small grow pointer no-select overflow-hidden';
return `<li id="${this.getId()}" class="${labelCls}">Ta bort lager</li>`;
}
});
popupMenuItems.push(removeLayerMenuItem);
}

const popupMenuList = Component({
onInit() {
this.addComponents(popupMenuItems);
},
render() {
let html = `<ul id="${this.getId()}">`;
popupMenuItems.forEach((item) => {
html += `${item.render()}`;
});
html += '</ul>';
return html;
}
});

const createPopupMenu = function createPopupMenu() {
const moreInfoButtonEl = document.getElementById(moreInfoButton.getId());
const onUnfocus = (e) => {
if (!moreInfoButtonEl.contains(e.target)) {
popupMenu.setVisibility(false);
}
};
popupMenu = PopupMenu({ onUnfocus, cls: 'overlay-popup' });
const newDiv = document.createElement('div');
newDiv.classList.add('justify-end', 'flex', 'relative', 'basis-100');
moreInfoButtonEl.insertAdjacentElement('afterend', newDiv);
newDiv.appendChild(dom.html(popupMenu.render()));
popupMenu.setContent(popupMenuList.render());
popupMenuList.dispatch('render');
popupMenu.setVisibility(true);
};

const togglePopupMenu = function togglePopupMenu() {
if (!popupMenu) {
createPopupMenu();
} else {
popupMenu.toggleVisibility();
}
};

if (popupMenuItems.length > 0) {
moreInfoButton = Button({
cls: 'round small icon-smaller no-shrink',
click() {
if (popupMenuItems.length > 1 || showPopup) {
togglePopupMenu();
} else {
document.getElementById(this.getId()).dispatchEvent(eventOverlayProps);
}
},
style: {
'align-self': 'center'
},
icon: '#ic_more_vert_24px',
ariaLabel: 'Visa lagerinfo',
tabIndex: -1
});
return moreInfoButton;
}
return false;
}
Loading
Loading