summaryrefslogtreecommitdiffstats
path: root/ui/imports/ui/components/accordionTreeNodeChildren/accordionTreeNodeChildren.js
blob: a74059c3cfce790fa27301484e136d1f6feb09d5 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/////////////////////////////////////////////////////////////////////////////////////////
// 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                                           /
/////////////////////////////////////////////////////////////////////////////////////////
/*
 * Template Component: accordionTreeNodeChildren
 */

/* eslint no-undef: off */

import * as R from 'ramda';
import { Template } from 'meteor/templating';
import { ReactiveDict } from 'meteor/reactive-dict';
import { Inventory } from '/imports/api/inventories/inventories';

import './accordionTreeNodeChildren.html';

Template.accordionTreeNodeChildren.onCreated(function () {
  var instance = this;
  this.state = new ReactiveDict();
  this.state.setDefault({
    data: null,
    siblingId: null
  });

  instance.autorun(function () {
    let data = Template.currentData();
    let node = data.node;
    instance.subscribe('inventory.children',
      node.id, node.type, node.name, node.environment);

    if (R.equals('host_ref', node.type)) {
      instance.subscribe('inventory?name&env&type', 
        node.name, node.environment, 'host');

      Inventory.find({ 
        name: node.name,
        environment: node.environment,
        type: 'host'
      }).forEach((sibling) => {
        instance.state.set('siblingId', sibling.id);
      });
    }
  });

});

Template.accordionTreeNodeChildren.helpers({
  reactOnNewData: function (node) {
    let instance = Template.instance();
    instance.state.set('data', { node: node });
  },

  children: function () {
    let instance = Template.instance();
    let siblingId = instance.state.get('siblingId');

    return getChildrenQuery(instance.data.node, siblingId);
  },

  createTreeNodeArgs: function(
    node,
    selectedNode
    ) {

    var instance = Template.instance();

    let firstChild = null;
    let restOfChildren = null;  
    let showOpen = false;

    if ((! R.isNil(selectedNode)) &&
          selectedNode.length > 0
    ) {
      firstChild = selectedNode[0];
      restOfChildren = selectedNode.length > 1 ? 
        R.slice(1, Infinity, selectedNode) : null;
      showOpen = firstChild.id === node.id ? true : false;
    }

    return {
      node: node,
      showOpen: showOpen,
      selectedNode: restOfChildren,
      onClick: instance.data.onClick
    };
  },


});

Template.accordionTreeNodeChildren.events({
});

function getChildrenQuery(node, siblingId) {
  let query = 
    {
      $or: [
        {
          parent_id: node.id,
          parent_type: node.type,
          environment: node.environment,
          show_in_tree: true
        }
      ]
    };


  if (R.equals('host_ref', node.type)) {
    query = R.merge(query, {
      $or: R.append({
        parent_id: siblingId,
        show_in_tree: true
      }, query.$or)
    });
  }

  console.log('getChildrenQuery', R.toString(query));

  return Inventory.find(query);
}