aboutsummaryrefslogtreecommitdiffstats
path: root/ui/imports/ui/actions/navigation.js
blob: f9c86b5fb05621c773d8490532d3805f13a0f513 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import * as R from 'ramda';

const SET_CURRENT_NODE = 'SET_CURRENT_NODE';
const SET_CURRENT_NODE_FROM_TREE_CONTROL = 'SET_CURRENT_NODE_FROM_TREE_CONTROL';

function setCurrentNode(item) {
  let nodeChain = convertToNodeChain(item.id_path, item.name_path);
  R.last(nodeChain).item = item;

  return {
    type: SET_CURRENT_NODE,
    payload: {
      nodeChain: nodeChain
    }
  };
}

function setCurrentNodeFromTreeControl (item) {
  let nodeChain = convertToNodeChain(item.id_path, item.name_path);
  R.last(nodeChain).item = item;

  return {
    type: SET_CURRENT_NODE_FROM_TREE_CONTROL,
    payload: {
      nodeChain: nodeChain
    }
  };
}

function convertToNodeChain(idPath, namePath) {
  let convert = R.pipe(R.split(), R.slice(1, Infinity));
  let paths = convert('/', idPath);
  let names = convert('/', namePath);
  let nodesData = R.zip(paths, names);
  let nodeChain = R.map((nodeData) => {
    return { 
      id: nodeData[0],
      name: nodeData[1]
    };
  }, nodesData); 

  let parent = null;

  for (let i = 0; i < nodeChain.length; i++) {
    let node = nodeChain[i];
    node.parent = parent;
    node.fullIdPath = calcFullIdPath(node); 
    node.fullNamePath = calcFullNamePath(node);
    parent = node;
  }

  return nodeChain; 
}

function calcFullIdPath (node) {
  if (R.isNil(node)) { return null; }
  if (R.isNil(node.parent)) { return '/' + node.id; }

  let parentFullPath = calcFullIdPath(node.parent);
  return parentFullPath + '/' + node.id; 
} 

function calcFullNamePath (node) {
  if (R.isNil(node)) { return null; }
  if (R.isNil(node.parent)) { return '/' + node.name; }

  let parentFullPath = calcFullNamePath(node.parent);
  return parentFullPath + '/' + node.name; 
}

export {
  SET_CURRENT_NODE,
  SET_CURRENT_NODE_FROM_TREE_CONTROL,
  setCurrentNode,
  setCurrentNodeFromTreeControl
};