aboutsummaryrefslogtreecommitdiffstats
path: root/framework/src/onos/utils/stc/src/main/resources/stc.js
blob: 215fd6e2e6dc3045a793287d02dea3273d26dbe8 (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
/*
 * Copyright 2015 Open Networking Laboratory
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
(function () {

    var ws, flow,
        nodes = [],
        links = [],
        nodeIndexes = {};

    var width = 2400,
        height = 2400;

    var color = d3.scale.category20();

    var force = d3.layout.force()
        .charge(-820)
        .linkDistance(50)
        .size([width, height]);

    // Process flow graph layout
    function createNode(n) {
        nodeIndexes[n.name] = nodes.push(n) - 1;
    }

    function createLink(e) {
        e.source = nodeIndexes[e.src];
        e.target = nodeIndexes[e.dst];
        links.push(e);
    }

    // Returns the newly computed bounding box of the rectangle
    function adjustRectToFitText(n) {
        var text = n.select('text'),
            box = text.node().getBBox();

        text.attr('text-anchor', 'left')
            .attr('y', 2)
            .attr('x', 4);

        // add padding
        box.x -= 4;
        box.width += 8;
        box.y -= 2;
        box.height += 4;

        n.select("rect").attr(box);
    }

    function processFlow() {
        var svg = d3.select("body").append("svg")
            .attr("width", width)
            .attr("height", height);

        flow.steps.forEach(createNode);
        flow.requirements.forEach(createLink);

        force
            .nodes(nodes)
            .links(links)
            .start();

        var link = svg.selectAll(".link")
            .data(links)
          .enter().append("line")
            .attr("class", "link")
            .style("stroke-width", function(d) { return d.isSoft ? 1 : 2; });

        var node = svg.selectAll(".node")
            .data(nodes)
          .enter().append("g")
            .attr("class", "node")
            .call(force.drag);

        node.append("rect")
            .attr({ rx: 5, ry:5, width:180, height:18 })
            .style("fill", function(d) { return color(d.group); });

        node.append("text").text( function(d) { return d.name; })
            .attr({ dy:"1.1em", width:100, height:16, x:4, y:2 });

        node.append("title")
            .text(function(d) { return d.name; });

        force.on("tick", function() {
            link.attr("x1", function(d) { return d.source.x; })
                .attr("y1", function(d) { return d.source.y; })
                .attr("x2", function(d) { return d.target.x; })
                .attr("y2", function(d) { return d.target.y; });

            node.attr("transform", function(d) { return "translate(" + (d.x - 180/2) + "," + (d.y - 18/2) + ")"; });
        });
    }


    // Web socket callbacks

    function handleOpen() {
        console.log('WebSocket open');
    }

    // Handles the specified (incoming) message using handler bindings.
    function handleMessage(msg) {
        console.log('rx: ', msg);
        evt = JSON.parse(msg.data);
        if (evt.event === 'progress') {

        } else if (evt.event === 'log') {

        } else if (evt.event === 'flow') {
            flow = evt.payload;
            processFlow();
        }
    }

    function handleClose() {
        console.log('WebSocket closed');
    }

    if (false) {
        d3.json("data.json", function (error, data) {
            flow = data;
            processFlow();
        });
        return;
    }

    // Open the web-socket
    ws = new WebSocket(document.location.href.replace('http:', 'ws:'));
    if (ws) {
        ws.onopen = handleOpen;
        ws.onmessage = handleMessage;
        ws.onclose = handleClose;
    }

})();