aboutsummaryrefslogtreecommitdiffstats
path: root/graphgraph-fe/src/GraphSettings.js
blob: 94b3e314d2313024ac2065141a6f8fe0ab9a6915 (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import React from 'react'
import _ from 'underscore'
import { DropdownButton, MenuItem, Label } from 'react-bootstrap'
import './GraphSettings.css'
import Popup from './PopupSettings'
import { pathGraph, basicGraph, schemas, nodeNames } from './requests'

var emptyState = {
  nodeNames: [],
  fromNode: '',
  graph: {
    nodeNames: [],
    edges: []
  },
  showHops: false,
  toNode: '',
  hops: {
    parents: 1,
    cousin: 1,
    child: 1
  },
  selectedSchema: ''
}

class GraphSettings extends React.Component {
  constructor (props, context) {
    super(props, context)

    this.onChangeStartNode = this.onChangeStartNode.bind(this)
    this.selectSchema = this.selectSchema.bind(this)
    this.onChangeToNode = this.onChangeToNode.bind(this)
    this.loadInitialGraph = this.loadInitialGraph.bind(this)
    this.updateHops = this.updateHops.bind(this)
    this.graphFingerprint = this.graphFingerprint.bind(this)
    this.state = emptyState
  }
  // this serves as a config 'fingerprint' to know if the d3 visualisation should be redrawn from scratch or just updated
  graphFingerprint (schema, from, to, parents, cousin, child) {
    return `${schema}:${from}:${to}:${parents}:${cousin}:${child}`
  }

  loadInitialGraph (startNode, endNode, parentHops, cousinHops, childHops) {
    let requestUri = endNode === 'none' ? basicGraph(this.state.selectedSchema, startNode, 0, 0, 1) : pathGraph(this.state.selectedSchema, startNode, endNode)

    fetch(requestUri)
      .then(response => response.json())
      .then(g => {
        let schema = this.state.selectedSchema

        let f = this.graphFingerprint(schema, startNode, endNode, parentHops, cousinHops, childHops)
        this.props.graphData(g, this.state.selectedSchema, f)
      })
      .then(g => {
        var s = this.state
        s['hops']['parents'] = parentHops
        s['hops']['cousin'] = cousinHops
        s['hops']['child'] = childHops
        s['fromNode'] = startNode
        s['toNode'] = endNode
        s['graph'] = g
        s['showHops'] = endNode === 'none' && startNode !== 'none'
        this.setState(s)
      })
  }

  selectSchema (schema) {
    var s = this.state
    s['selectedSchema'] = schema
    fetch(nodeNames(schema))
      .then(response => response.json())
      .then(nodeNames => {
        s['fromNode'] = s['toNode'] = 'none'
        s['nodeNames'] = nodeNames
        this.setState(s)
      })
  }

  updateHops (parentHops, cousinHops, childHops) {
    this.loadInitialGraph(
      this.state.fromNode,
      this.state.toNode,
      parentHops,
      cousinHops,
      childHops)
  }

  onChangeToNode (eventKey) {
    this.loadInitialGraph(this.state.fromNode,
      eventKey,
      this.state.hops.parents,
      this.state.hops.cousin,
      this.state.hops.child)
  }

  onChangeStartNode (eventKey) {
    this.loadInitialGraph(eventKey, this.state.toNode,
      this.state.hops.parents,
      this.state.hops.cousin,
      this.state.hops.child)
  }

  componentDidMount () {
    fetch(schemas())
      .then(response => response.json())
      .then(schemas => {
        let s = this.state
        s['schemas'] = schemas
        this.setState(s)
      })
  }

  render () {
    var schemas = _.map(this.state.schemas, (x, k) => <MenuItem key={k} eventKey={x}>{x}</MenuItem>)

    var items = _.map(this.state.nodeNames, (x, k) => <MenuItem key={k} eventKey={x.id}>{x.id}</MenuItem>)

    var fromItems = items.slice()
    fromItems.unshift(<MenuItem key='divider' divider />)
    fromItems.unshift(<MenuItem key='none' eventKey='none'>none</MenuItem>)

    items.unshift(<MenuItem key='anotherdivider' divider />)
    items.unshift(<MenuItem key='none' eventKey='none'>none</MenuItem>)

    return (
      <div>
        <div className="graph-menu">
          <div className="startendnode-dropdown">
            <div>
              <Label>Schemas</Label>
              <DropdownButton className="schemas-dropdown" onSelect={this.selectSchema} id="schemas" title={this.state.selectedSchema}>
                {schemas}
              </DropdownButton>
            </div>
            <div className="source-dropdown-div">
              <Label>Source Node</Label>
              <DropdownButton className="node-dropdown" onSelect={this.onChangeStartNode} id="namesFrom" title={this.state.fromNode}>
                {fromItems}
              </DropdownButton>
            </div>
            <div>
              <Label>Destination Node</Label>
              <DropdownButton className="node-dropdown" onSelect={this.onChangeToNode} id="namesTo" title={this.state.toNode}>
                {items}
              </DropdownButton>

            </div>
            <Popup isDisabled={!this.state.showHops} parentHops={this.state.hops.parents} childHops={this.state.hops.child} cousinHops={this.state.hops.cousin} updateHops={this.updateHops}/>
          </div>

        </div>
      </div>
    )
  }
}

export default GraphSettings