summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/pipelines/components/dag/interactions.js
blob: c9008730c900533273d372ab4f053b24a8a011f8 (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
import * as d3 from 'd3';
import { LINK_SELECTOR, NODE_SELECTOR, IS_HIGHLIGHTED } from './constants';

export const highlightIn = 1;
export const highlightOut = 0.2;

const getCurrent = (idx, collection) => d3.select(collection[idx]);
const currentIsLive = (idx, collection) => getCurrent(idx, collection).classed(IS_HIGHLIGHTED);
const getOtherLinks = () => d3.selectAll(`.${LINK_SELECTOR}:not(.${IS_HIGHLIGHTED})`);
const getNodesNotLive = () => d3.selectAll(`.${NODE_SELECTOR}:not(.${IS_HIGHLIGHTED})`);

const backgroundLinks = selection => selection.style('stroke-opacity', highlightOut);
const backgroundNodes = selection => selection.attr('stroke', '#f2f2f2');
const foregroundLinks = selection => selection.style('stroke-opacity', highlightIn);
const foregroundNodes = selection => selection.attr('stroke', d => d.color);
const renewLinks = (selection, baseOpacity) => selection.style('stroke-opacity', baseOpacity);
const renewNodes = selection => selection.attr('stroke', d => d.color);

const getAllLinkAncestors = node => {
  if (node.targetLinks) {
    return node.targetLinks.flatMap(n => {
      return [n.uid, ...getAllLinkAncestors(n.source)];
    });
  }

  return [];
};

const getAllNodeAncestors = node => {
  let allNodes = [];

  if (node.targetLinks) {
    allNodes = node.targetLinks.flatMap(n => {
      return getAllNodeAncestors(n.source);
    });
  }

  return [...allNodes, node.uid];
};

export const highlightLinks = (d, idx, collection) => {
  const currentLink = getCurrent(idx, collection);
  const currentSourceNode = d3.select(`#${d.source.uid}`);
  const currentTargetNode = d3.select(`#${d.target.uid}`);

  /* Higlight selected link, de-emphasize others */
  backgroundLinks(getOtherLinks());
  foregroundLinks(currentLink);

  /* Do the same to related nodes */
  backgroundNodes(getNodesNotLive());
  foregroundNodes(currentSourceNode);
  foregroundNodes(currentTargetNode);
};

const highlightPath = (parentLinks, parentNodes) => {
  /* de-emphasize everything else */
  backgroundLinks(getOtherLinks());
  backgroundNodes(getNodesNotLive());

  /* highlight correct links */
  parentLinks.forEach(id => {
    foregroundLinks(d3.select(`#${id}`)).classed(IS_HIGHLIGHTED, true);
  });

  /* highlight correct nodes */
  parentNodes.forEach(id => {
    foregroundNodes(d3.select(`#${id}`)).classed(IS_HIGHLIGHTED, true);
  });
};

const restorePath = (parentLinks, parentNodes, baseOpacity) => {
  parentLinks.forEach(id => {
    renewLinks(d3.select(`#${id}`), baseOpacity).classed(IS_HIGHLIGHTED, false);
  });

  parentNodes.forEach(id => {
    d3.select(`#${id}`).classed(IS_HIGHLIGHTED, false);
  });

  if (d3.selectAll(`.${IS_HIGHLIGHTED}`).empty()) {
    renewLinks(getOtherLinks(), baseOpacity);
    renewNodes(getNodesNotLive());
    return;
  }

  backgroundLinks(getOtherLinks());
  backgroundNodes(getNodesNotLive());
};

export const restoreLinks = (baseOpacity, d, idx, collection) => {
  /* in this case, it has just been clicked */
  if (currentIsLive(idx, collection)) {
    return;
  }

  /*
    if there exist live links, reset to highlight out / pale
    otherwise, reset to base
  */

  if (d3.selectAll(`.${IS_HIGHLIGHTED}`).empty()) {
    renewLinks(d3.selectAll(`.${LINK_SELECTOR}`), baseOpacity);
    renewNodes(d3.selectAll(`.${NODE_SELECTOR}`));
    return;
  }

  backgroundLinks(getOtherLinks());
  backgroundNodes(getNodesNotLive());
};

export const toggleLinkHighlight = (baseOpacity, d, idx, collection) => {
  if (currentIsLive(idx, collection)) {
    restorePath([d.uid], [d.source.uid, d.target.uid], baseOpacity);
    return;
  }

  highlightPath([d.uid], [d.source.uid, d.target.uid]);
};

export const togglePathHighlights = (baseOpacity, d, idx, collection) => {
  const parentLinks = getAllLinkAncestors(d);
  const parentNodes = getAllNodeAncestors(d);
  const currentNode = getCurrent(idx, collection);

  /* if this node is already live, make it unlive and reset its path */
  if (currentIsLive(idx, collection)) {
    currentNode.classed(IS_HIGHLIGHTED, false);
    restorePath(parentLinks, parentNodes, baseOpacity);
    return;
  }

  highlightPath(parentLinks, parentNodes);
};