blob: 2cd4ba7a84ad7fbe1ff7678ea2330ae33a7b7482 [file] [log] [blame]
Steven Burrowsec1f45c2016-08-08 16:14:41 +01001/*
2* Copyright 2016-present Open Networking Laboratory
3*
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8* http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*/
16
17/*
18ONOS GUI -- Topology Layout Module.
19Module that contains the d3.force.layout logic
20*/
21
22(function () {
23 'use strict';
24
25 var sus, is, ts;
26
27 // internal state
28 var deviceLabelIndex = 0,
29 hostLabelIndex = 0;
30
31 // configuration
32 var devIconDim = 36,
33 labelPad = 4,
34 hostRadius = 14,
35 badgeConfig = {
36 radius: 12,
37 yoff: 5,
38 gdelta: 10
39 },
40 halfDevIcon = devIconDim / 2,
41 devBadgeOff = { dx: -halfDevIcon, dy: -halfDevIcon },
42 hostBadgeOff = { dx: -hostRadius, dy: -hostRadius },
43 status = {
44 i: 'badgeInfo',
45 w: 'badgeWarn',
46 e: 'badgeError'
47 };
48
49 // note: these are the device icon colors without affinity (no master)
50 var dColTheme = {
51 light: {
52 online: '#444444',
53 offline: '#cccccc'
54 },
55 dark: {
56 // TODO: theme
57 online: '#444444',
58 offline: '#cccccc'
59 }
60 };
61
62 function init() {}
63
64 function renderBadge(node, bdg, boff) {
65 var bsel,
66 bcr = badgeConfig.radius,
67 bcgd = badgeConfig.gdelta;
68
69 node.select('g.badge').remove();
70
71 bsel = node.append('g')
72 .classed('badge', true)
73 .classed(badgeStatus(bdg), true)
74 .attr('transform', sus.translate(boff.dx, boff.dy));
75
76 bsel.append('circle')
77 .attr('r', bcr);
78
79 if (bdg.txt) {
80 bsel.append('text')
81 .attr('dy', badgeConfig.yoff)
82 .attr('text-anchor', 'middle')
83 .text(bdg.txt);
84 } else if (bdg.gid) {
85 bsel.append('use')
86 .attr({
87 width: bcgd * 2,
88 height: bcgd * 2,
89 transform: sus.translate(-bcgd, -bcgd),
90 'xlink:href': '#' + bdg.gid
91 });
92 }
93 }
94
95 // TODO: Move to Device Model when working on the Exit Devices
96 function updateDeviceRendering(d) {
97 var node = d.el,
98 bdg = d.badge,
99 label = trimLabel(deviceLabel(d)),
100 labelWidth;
101
102 node.select('text').text(label);
103 labelWidth = label ? computeLabelWidth(node) : 0;
104
105 node.select('rect')
106 .transition()
107 .attr(iconBox(devIconDim, labelWidth));
108
109 if (bdg) {
110 renderBadge(node, bdg, devBadgeOff);
111 }
112 }
113
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100114 function nodeEnter(node) {
115 node.onEnter(this, node);
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100116 }
117
118 function hostLabel(d) {
119 return d.get('id');
120
121 // var idx = (hostLabelIndex < d.get('labels').length) ? hostLabelIndex : 0;
122 // return d.labels[idx];
123 }
124
125 function hostEnter(d) {
126 var node = d3.select(this),
127 gid = d.get('type') || 'unknown',
128 textDy = hostRadius + 10;
129
130 d.el = node;
131 // sus.visible(node, api.showHosts());
132
133 is.addHostIcon(node, hostRadius, gid);
134
135 node.append('text')
136 .text(hostLabel)
137 .attr('dy', textDy)
138 .attr('text-anchor', 'middle');
139 }
140
141 function linkEntering(link) {
142 link.onEnter(this);
143 }
144
145 angular.module('ovTopo2')
146 .factory('Topo2D3Service',
147 ['SvgUtilService', 'IconService', 'ThemeService',
148
149 function (_sus_, _is_, _ts_) {
150 sus = _sus_;
151 is = _is_;
152 ts = _ts_;
153
154 return {
155 init: init,
Steven Burrows6deb4ce2016-08-26 16:06:23 +0100156 nodeEnter: nodeEnter,
Steven Burrowsec1f45c2016-08-08 16:14:41 +0100157 hostEnter: hostEnter,
158 linkEntering: linkEntering
159 }
160 }
161 ]
162);
163})();