Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
34 changes: 33 additions & 1 deletion web/client/utils/LayersUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,38 @@ export const normalizeMap = (rawMap = {}) =>
* @return function that filter by group
*/
export const belongsToGroup = (gid) => l => (l.group || DEFAULT_GROUP_ID) === gid || (l.group || "").indexOf(`${gid}.`) === 0;

/**
* Retrieves the store index of a group from a list of map layers.
*
* @param {Object} cur - The current group object whose store index is being searched.
* @param {Array} mapLayers - An array of layer objects, each containing a `group` and `storeIndex` property.
* @returns {number} - The store index of the group if found, otherwise `-1`.
*/
const getGroupStoreIndex = (cur, mapLayers) => {
// Create a map for quick lookup of group to storeIndex
const groupMap = new Map(mapLayers.map(el => [el.group, el.storeIndex]));
// Check direct match
if (groupMap.has(cur.id)) {
return groupMap.get(cur.id);
}
// Checks if a layer belongs to the descendants of the current group.
// If yes, return the storeIndex of the first matching layer as the storeIndex for the current group.
// Ensures that groups without direct child layers are assigned a storeIndex
// preventing them from always being placed at the end of the TOC.
for (const el of mapLayers) {
const groupParts = el.group.split('.');
for (let i = groupParts.length - 1; i > 0; i--) {
const parentGroup = groupParts.slice(0, i).join('.');
if (parentGroup === cur.id) {
return el.storeIndex;
}
}
}
// Return -1 if no match is found
return -1;
};

export const getLayersByGroup = (configLayers, configGroups) => {
let i = 0;
let mapLayers = configLayers.map((layer) => assign({}, layer, {storeIndex: i++}));
Expand All @@ -488,7 +520,7 @@ export const getLayersByGroup = (configLayers, configGroups) => {
group.nodes = getLayersId(groupId, mapLayers).concat(group.nodes)
.reduce((arr, cur) => {
isObject(cur)
? arr.push({node: cur, order: mapLayers.find((el) => el.group === cur.id)?.storeIndex})
? arr.push({node: cur, order: getGroupStoreIndex(cur, mapLayers)})
: arr.push({node: cur, order: mapLayers.find((el) => el.id === cur)?.storeIndex});
return arr;
}, []).sort((a, b) => b.order - a.order).map(e => e.node);
Expand Down
81 changes: 81 additions & 0 deletions web/client/utils/__tests__/LayersUtils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,87 @@ describe('LayersUtils', () => {
]);
});

it('getLayersByGroup should return correctly ordered groups even without direct child layers', () => {
// Group Default.root.test does not have direct child layers, but it has child groups
const groups = [
{id: 'Default'},
{id: 'Default.root.test.childGroup001'},
{id: 'Default.root.test.childGroup002'},
{id: 'Default.root.test'},
{id: 'Default.root.custom'},
{id: 'Default.root'}
];
const layers = [
{id: 'layer007', group: 'Default.root'},
{id: 'layer006', group: 'Default.root'},
{id: 'layer005', group: 'Default.root.custom'},
{id: 'layer004', group: 'Default.root.test.childGroup002'},
{id: 'layer003', group: 'Default.root.test.childGroup001'},
{id: 'layer002', group: 'Default.root'},
{id: 'layer001', group: 'Default.root'}
];

const result = LayersUtils.getLayersByGroup(layers, groups);
const expectedGroups = [
{
"expanded": true,
"id": "Default",
"name": "Default",
"nodes": [
{
"expanded": true,
"id": "Default.root",
"name": "root",
"nodes": [
"layer001",
"layer002",
{
"expanded": true,
"id": "Default.root.test",
"name": "test",
"nodes": [
{
"expanded": true,
"id": "Default.root.test.childGroup001",
"name": "childGroup001",
"nodes": [
"layer003"
],
"title": "childGroup001"
},
{
"expanded": true,
"id": "Default.root.test.childGroup002",
"name": "childGroup002",
"nodes": [
"layer004"
],
"title": "childGroup002"
}
],
"title": "test"
},
{
"expanded": true,
"id": "Default.root.custom",
"name": "custom",
"nodes": [
"layer005"
],
"title": "custom"
},
"layer006",
"layer007"
],
"title": "root"
}
],
"title": "Default"
}
];
expect(result).toEqual(expectedGroups);
});

it('deep change in nested group', () => {

const nestedGroups = [
Expand Down