diff --git a/pkg/interface/src/logic/reducers/graph-update.js b/pkg/interface/src/logic/reducers/graph-update.js index 5b8dbcb5cd..c8253f229c 100644 --- a/pkg/interface/src/logic/reducers/graph-update.js +++ b/pkg/interface/src/logic/reducers/graph-update.js @@ -1,20 +1,14 @@ import _ from 'lodash'; -const OrderedMap = (arr = []) => { - let map = new Map(arr); -// map[Symbol.iterator] = function* () { -// yield* [...this.entries()].sort((a, b) => a[1] - b[1]); -// }; - return map; -}; - export const GraphReducer = (json, state) => { const data = _.get(json, 'graph-update', false); if (data) { keys(data, state); addGraph(data, state); + removeGraph(data, state); addNodes(data, state); + removeNodes(data, state); } }; @@ -25,7 +19,7 @@ const keys = (json, state) => { let resource = res.ship + '/' + res.name; if (!(resource in state.graphs)) { - state.graphs[resource] = new OrderedMap(); + state.graphs[resource] = new Map(); } return resource; @@ -34,39 +28,16 @@ const keys = (json, state) => { }; const addGraph = (json, state) => { - const data = _.get(json, 'add-graph', false); - if (data) { - if (!('graphs' in state)) { - state.graphs = {}; - } - let resource = data.resource.ship + '/' + data.resource.name; - state.graphs[resource] = new OrderedMap(); - - for (let i in data.graph) { - let item = data.graph[i]; - let index = item[0].split('/').slice(1).map((ind) => { - return parseInt(ind, 10); - }); - - if (index.length === 0) { break; } - - let node = _processNode(item[1]); - state.graphs[resource].set(index[index.length - 1], node); - } - state.graphKeys.add(resource); - } -}; - -const _processNode = (node) => { + const _processNode = (node) => { // is empty if (!node.children) { - node.children = new OrderedMap(); + node.children = new Map(); return node; } // is graph - let converted = new OrderedMap(); + let converted = new Map(); for (let i in node.children) { let item = node.children[i]; let index = item[0].split('/').slice(1).map((ind) => { @@ -82,8 +53,33 @@ const _processNode = (node) => { } node.children = converted; return node; + }; + + const data = _.get(json, 'add-graph', false); + if (data) { + if (!('graphs' in state)) { + state.graphs = {}; + } + + let resource = data.resource.ship + '/' + data.resource.name; + state.graphs[resource] = new Map(); + + for (let i in data.graph) { + let item = data.graph[i]; + let index = item[0].split('/').slice(1).map((ind) => { + return parseInt(ind, 10); + }); + + if (index.length === 0) { break; } + + let node = _processNode(item[1]); + state.graphs[resource].set(index[index.length - 1], node); + } + state.graphKeys.add(resource); } +}; + const removeGraph = (json, state) => { const data = _.get(json, 'remove-graph', false); if (data) { @@ -96,6 +92,24 @@ const removeGraph = (json, state) => { }; const addNodes = (json, state) => { + const _addNode = (graph, index, node) => { + // set child of graph + if (index.length === 1) { + graph.set(index[0], node); + return graph; + } + + // set parent of graph + let parNode = graph.get(index[0]); + if (!parNode) { + console.error('parent node does not exist, cannot add child'); + return; + } + parNode.children = _addNode(parNode.children, index.slice(1), node); + graph.set(index[0], parNode); + return graph; + }; + const data = _.get(json, 'add-nodes', false); if (data) { if (!('graphs' in state)) { return; } @@ -125,21 +139,26 @@ const addNodes = (json, state) => { } }; -const _addNode = (graph, index, node) => { - // set child of graph - if (index.length === 1) { - graph.set(index[0], node); - return graph; - } +const removeNodes = (json, state) => { + const data = _.get(json, 'remove-nodes', false); + if (data) { + console.log(data); + if (!(data.resource in state.graphs)) { return; } - // set parent of graph - let parNode = graph.get(index[0]); - if (!parNode) { - console.error('parent node does not exist, cannot add child'); - return; + data.indices.forEach((index) => { + console.log(index); + if (index.split('/').length === 0) { return; } + let indexArr = index.split('/').slice(1).map((ind) => { + return parseInt(ind, 10); + }); + + if (indexArr.length === 1) { + state.graphs[data.resource].delete(indexArr[0]); + } else { + // TODO: recursive + } + + }); } - parNode.children = _addNode(parNode.children, index.slice(1), node); - graph.set(index[0], parNode); - return graph; };