Skip to content

Commit

Permalink
Fixes on Mauro's request
Browse files Browse the repository at this point in the history
  • Loading branch information
kappu committed Jan 20, 2017
1 parent b760841 commit b4f5bd2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 32 deletions.
4 changes: 2 additions & 2 deletions web/client/components/TOC/fragments/settings/General.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -39,7 +39,7 @@ const General = React.createClass({
}, []);
},
getLabelName(groupLable = "") {
return groupLable.replace(/_/g, ' ').split('.').pop();
return head(groupLable.replace(/_/g, ' ').split('.'));
},
render() {
return (<form ref="settings">
Expand Down
51 changes: 29 additions & 22 deletions web/client/reducers/layers.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ const deepRemove = (nodes, findValue) => {
}
return nodes;
};

const getNode = (nodes, name) => {
if (nodes && isArray(nodes)) {
return nodes.reduce((previous, node) => {
Expand All @@ -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: {
Expand Down Expand Up @@ -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,
Expand Down
26 changes: 18 additions & 8 deletions web/client/utils/LayersUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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({}, {
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit b4f5bd2

Please sign in to comment.