aboutsummaryrefslogtreecommitdiffstats
path: root/ui/imports/ui/reducers/navigation.js
blob: de78ee5cb2697f21809a5d814d97f14d0d95d744 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2017 Koren Lev (Cisco Systems), Yaron Yogev (Cisco Systems) and others /
//                                                                                      /
// All rights reserved. This program and the accompanying materials                     /
// are made available under the terms of the Apache License, Version 2.0                /
// which accompanies this distribution, and is available at                             /
// http://www.apache.org/licenses/LICENSE-2.0                                           /
/////////////////////////////////////////////////////////////////////////////////////////
import * as R from 'ramda';

import * as actions from '/imports/ui/actions/navigation';

const defaultState = { current: [], lastActionable: [] };

function reducer(state = defaultState, action) {
  let lastActionable = null;

  switch (action.type) {
  case actions.SET_CURRENT_NODE:
    lastActionable = isActionable(action.payload.nodeChain) ? action.payload.nodeChain :
        state.lastActionable;

    return R.merge(state, {
      current: action.payload.nodeChain,
      lastActionable: lastActionable
    });

  case actions.SET_CURRENT_NODE_FROM_TREE_CONTROL:
    lastActionable = isActionable(action.payload.nodeChain) ? action.payload.nodeChain :
        state.lastActionable;

    if (contains(action.payload.nodeChain, state.current)) {
      let equalLastIndex = findEqualLastIndex(action.payload.nodeChain, state.current);
      return R.merge(state, {
        current: R.slice(0, equalLastIndex, action.payload.nodeChain),
        lastActionable: lastActionable
      });
    } else {
      return R.merge(state, {
        current: action.payload.nodeChain,
        lastActionable: lastActionable
      });
    }

  default:
    return state;
  }
}

function contains(subArray, array) {
  let equalLastIndex = findEqualLastIndex(subArray, array);

  if (subArray.length <= array.length &&
      equalLastIndex >= 0 &&
      subArray.length === equalLastIndex + 1) {

    return true;
  }

  return false;
}

function findEqualLastIndex (arrayA, arrayB) {
  let indexResult = -1;

  for (let i = 0; (i < arrayA.length) && (i < arrayB.length); i++) {
    if (equalsNodes(arrayA[i], arrayB[i])) {
      indexResult = i;
    } else {
      break;
    }
  }

  return indexResult;
}

function equalsNodes(nodeA, nodeB) {
  if (nodeA.fullIdPath !== nodeB.fullIdPath) { return false; }
  if (nodeA.fullNamePath !== nodeB.fullNamePath) { return false; }

  return true;
}

function isActionable(nodeChain) {
  let last = R.last(nodeChain);

  if (R.isNil(last)) { return false; }
  if (R.isNil(last.item)) { return false; }

  if (! R.isNil(last.item.clique)) { return true; }

  if (last.item.id === 'aggregate-WebEx-RTP-SSD-Aggregate-node-24') {
    return true;
  }

  return false;
}

export const navigation = reducer;