From b4f5bd277d1d4060656c6066b3f99e84bd1c7c94 Mon Sep 17 00:00:00 2001 From: kappu Date: Fri, 20 Jan 2017 11:20:04 +0100 Subject: [PATCH] Fixes on Mauro's request --- .../TOC/fragments/settings/General.jsx | 4 +- web/client/reducers/layers.js | 51 +++++++++++-------- web/client/utils/LayersUtils.js | 26 +++++++--- 3 files changed, 49 insertions(+), 32 deletions(-) diff --git a/web/client/components/TOC/fragments/settings/General.jsx b/web/client/components/TOC/fragments/settings/General.jsx index 352f9c2f88..059c40c6bb 100644 --- a/web/client/components/TOC/fragments/settings/General.jsx +++ b/web/client/components/TOC/fragments/settings/General.jsx @@ -10,7 +10,7 @@ var React = require('react'); const {Input} = require('react-bootstrap'); const Message = require('../../../I18N/Message'); const {SimpleSelect} = require('react-selectize'); -const {isObject} = require('lodash'); +const {isObject, head} = require('lodash'); require('react-selectize/themes/index.css'); @@ -39,7 +39,7 @@ const General = React.createClass({ }, []); }, getLabelName(groupLable = "") { - return groupLable.replace(/_/g, ' ').split('.').pop(); + return head(groupLable.replace(/_/g, ' ').split('.')); }, render() { return (
diff --git a/web/client/reducers/layers.js b/web/client/reducers/layers.js index 583617c7b6..a51455ca00 100644 --- a/web/client/reducers/layers.js +++ b/web/client/reducers/layers.js @@ -44,7 +44,6 @@ const deepRemove = (nodes, findValue) => { } return nodes; }; - const getNode = (nodes, name) => { if (nodes && isArray(nodes)) { return nodes.reduce((previous, node) => { @@ -62,6 +61,33 @@ const getNode = (nodes, name) => { } return null; }; + +const moveNode = (groups, node, groupId, newLayers) => { + // Remove node from old group + let newGroups = deepRemove(groups, node); + // Check if group to move to exists + let group = getNode(newGroups, groupId); + if (!group) { + // Create missing group + group = head(LayersUtils.getLayersByGroup([getNode(newLayers, node)])); + // check for parent group if exist + const parentGroup = groupId.split('.').reduce((tree, gName, idx) => { + const gId = groupId.split(".", idx + 1).join('.'); + const parent = getNode(newGroups, gId); + return parent ? tree.concat(parent) : tree; + }, []).pop(); + if (parentGroup) { + group = getNode([group], parentGroup.id).nodes[0]; + newGroups = deepChange(newGroups, parentGroup.id, 'nodes', parentGroup.nodes.concat(group)); + }else { + newGroups.push(group); + } + }else { + newGroups = deepChange(newGroups, group.id, 'nodes', group.nodes.concat(node)); + } + return LayersUtils.removeEmptyGroups(newGroups); +}; + function layers(state = [], action) { switch (action.type) { case LAYER_LOADING: { @@ -151,27 +177,8 @@ function layers(state = [], action) { if (!sameGroup && originalNode ) { // Remove layers from old group const groupId = (action.options.group || 'Default'); - let newGroups = deepRemove(state.groups, action.node); - // Check if new group exist - let group = getNode(state.groups, groupId); - if (!group) { - // create missing group - const groups = LayersUtils.getLayersByGroup([getNode(newLayers, action.node)]); - // check for parent group if exist - const parentGroup = groupId.split('.').reduce((tree, gName, idx) => { - const gId = groupId.split(".", idx + 1).join('.'); - const parent = getNode(state.groups, gId); - return parent ? tree.concat(parent) : tree; - }, []).pop(); - if (parentGroup) { - group = getNode(groups, groupId); - newGroups = deepChange(newGroups, parentGroup.id, 'nodes', parentGroup.nodes.concat(group)); - }else { - newGroups.push(groups.pop()); - } - }else { - newGroups = deepChange(newGroups, group.id, 'nodes', group.nodes.concat(action.node)); - } + const newGroups = moveNode(state.groups, action.node, groupId, newLayers); + let orderedNewLayers = LayersUtils.sortLayers ? LayersUtils.sortLayers(newGroups, newLayers) : newLayers; return assign({}, state, { flat: orderedNewLayers, diff --git a/web/client/utils/LayersUtils.js b/web/client/utils/LayersUtils.js index a3c90a7956..0699d7c29b 100644 --- a/web/client/utils/LayersUtils.js +++ b/web/client/utils/LayersUtils.js @@ -7,13 +7,13 @@ */ const assign = require('object-assign'); -const {isObject, isArray} = require('lodash'); +const {isObject, isArray, head} = require('lodash'); const getGroup = (groupId, groups) => { - return groups.filter((subGroup) => isObject(subGroup) && subGroup.id === groupId).pop(); + return head(groups.filter((subGroup) => isObject(subGroup) && subGroup.id === groupId)); }; const getLayer = (layerName, allLayers) => { - return allLayers.filter((layer) => layer.id === layerName).pop(); + return head(allLayers.filter((layer) => layer.id === layerName)); }; const getLayersId = (groupId, allLayers) => { return allLayers.filter((layer) => (layer.group || 'Default') === groupId).map((layer) => layer.id).reverse(); @@ -33,7 +33,6 @@ const initialReorderLayers = (groups, allLayers) => { const reorderLayers = (groups, allLayers) => { return initialReorderLayers(groups, allLayers); }; - const createGroup = (groupId, groupName, layers, addLayers) => { const title = groupName.replace(/_/g, ' '); return assign({}, { @@ -44,6 +43,7 @@ const createGroup = (groupId, groupName, layers, addLayers) => { expanded: true }); }; + var LayersUtils = { getLayerId: (layerObj, layers) => { return layerObj && layerObj.id || (layerObj.name + "__" + layers.length); @@ -72,17 +72,27 @@ var LayersUtils = { return groups; }, []); }, - + removeEmptyGroups: (groups) => { + return groups.reduce((acc, group) => { + return acc.concat(LayersUtils.getNotEmptyGroup(group)); + }, []); + }, + getNotEmptyGroup: (group) => { + const nodes = group.nodes.reduce((gNodes, node) => { + return node.nodes ? gNodes.concat(LayersUtils.getNotEmptyGroup(node)) : gNodes.concat(node); + }, []); + return nodes.length > 0 ? assign({}, group, {nodes: nodes}) : []; + }, reorder: (groups, allLayers) => { return allLayers.filter((layer) => layer.group === 'background') .concat(reorderLayers(groups, allLayers)); }, denormalizeGroups: (allLayers, groups) => { let getGroupVisibility = (nodes) => { - let visibility = false; + let visibility = true; nodes.forEach((node) => { - if (node && node.visibility) { - visibility = true; + if (!node.visibility) { + visibility = false; } }); return visibility;