Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 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 | /* |
| 18 | ONOS GUI -- Topology Links Module. |
| 19 | Module that holds the links for a region |
| 20 | */ |
| 21 | |
| 22 | (function () { |
| 23 | 'use strict'; |
| 24 | |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 25 | var $log, Collection, Model, ts, sus, t2zs, t2vs, t2lps, |
| 26 | fn, ps, t2mss, t2ts; |
Steven Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame] | 27 | |
| 28 | var linkLabelOffset = '0.35em'; |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 29 | |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 30 | var widthRatio = 1.4, |
| 31 | linkScale = d3.scale.linear() |
| 32 | .domain([1, 12]) |
| 33 | .range([widthRatio, 12 * widthRatio]) |
| 34 | .clamp(true), |
Steven Burrows | a3fca81 | 2016-10-14 15:11:04 -0500 | [diff] [blame] | 35 | labelDim = 30; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 36 | |
| 37 | // configuration |
| 38 | var linkConfig = { |
| 39 | light: { |
| 40 | baseColor: '#939598', |
| 41 | inColor: '#66f', |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 42 | outColor: '#f00', |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 43 | }, |
| 44 | dark: { |
| 45 | // TODO : theme |
| 46 | baseColor: '#939598', |
| 47 | inColor: '#66f', |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 48 | outColor: '#f00', |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 49 | }, |
| 50 | inWidth: 12, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 51 | outWidth: 10, |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 52 | }; |
| 53 | |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 54 | function createLink() { |
| 55 | |
| 56 | var linkPoints = this.linkEndPoints(this.get('epA'), this.get('epB')); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 57 | |
| 58 | var attrs = angular.extend({}, linkPoints, { |
| 59 | key: this.get('id'), |
| 60 | class: 'link', |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 61 | position: { |
| 62 | x1: 0, |
| 63 | y1: 0, |
| 64 | x2: 0, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 65 | y2: 0, |
| 66 | }, |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 67 | // functions to aggregate dual link state |
Steven Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame] | 68 | // extra: link.extra |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 69 | }); |
| 70 | |
| 71 | this.set(attrs); |
| 72 | } |
| 73 | |
Steven Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame] | 74 | function rectAroundText(el) { |
| 75 | var text = el.select('text'), |
| 76 | box = text.node().getBBox(); |
| 77 | |
| 78 | // translate the bbox so that it is centered on [x,y] |
| 79 | box.x = -box.width / 2; |
| 80 | box.y = -box.height / 2; |
| 81 | |
| 82 | // add padding |
| 83 | box.x -= 4; |
| 84 | box.width += 8; |
| 85 | return box; |
| 86 | } |
| 87 | |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 88 | function isLinkOnline(node) { |
| 89 | return (node.get('nodeType') === 'region') ? true : node.get('online'); |
| 90 | } |
| 91 | |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 92 | function linkEndPoints(srcId, dstId) { |
| 93 | |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 94 | var findNodeById = this.region.model.findNodeById.bind(this.region), |
| 95 | allNodes = this.region.model.nodes(), |
| 96 | sourceNode = findNodeById(this, srcId), |
| 97 | targetNode = findNodeById(this, dstId); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 98 | |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 99 | if (!sourceNode || !targetNode) { |
Steven Burrows | 8f45ce2 | 2016-10-27 20:04:14 -0500 | [diff] [blame] | 100 | $log.error('Node(s) not on map for link:' + srcId + '~' + dstId); |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 101 | // logicError('Node(s) not on map for link:\n' + sMiss + dMiss); |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 102 | return null; |
| 103 | } |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 104 | |
Steven Burrows | a3fca81 | 2016-10-14 15:11:04 -0500 | [diff] [blame] | 105 | this.source = allNodes.indexOf(sourceNode); |
| 106 | this.target = allNodes.indexOf(targetNode); |
| 107 | this.sourceNode = sourceNode; |
| 108 | this.targetNode = targetNode; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 109 | |
| 110 | return { |
| 111 | source: sourceNode, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 112 | target: targetNode, |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 113 | }; |
| 114 | } |
| 115 | |
| 116 | function createLinkCollection(data, _region) { |
| 117 | |
| 118 | var LinkModel = Model.extend({ |
Steven Burrows | 68d6f95 | 2017-03-10 13:53:35 +0000 | [diff] [blame] | 119 | initialize: function () { |
| 120 | this.super = this.constructor.__super__; |
| 121 | this.super.initialize.apply(this, arguments); |
| 122 | this.createLink(); |
| 123 | }, |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 124 | region: _region, |
| 125 | createLink: createLink, |
| 126 | linkEndPoints: linkEndPoints, |
| 127 | type: function () { |
| 128 | return this.get('type'); |
| 129 | }, |
Steven Burrows | 86af435 | 2016-11-16 18:19:12 -0600 | [diff] [blame] | 130 | svgClassName: function () { |
| 131 | return fn.classNames('link', |
| 132 | this.nodeType, |
| 133 | this.get('type'), |
| 134 | { |
| 135 | enhanced: this.get('enhanced'), |
Steven Burrows | c515e60 | 2017-04-13 11:17:40 -0700 | [diff] [blame] | 136 | selected: this.get('selected'), |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 137 | suppressedmax: this.get('mastership'), |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 138 | }, |
| 139 | (this.linkLabel) ? this.linkLabel.linkLabelCSSClass() : null |
Steven Burrows | 86af435 | 2016-11-16 18:19:12 -0600 | [diff] [blame] | 140 | ); |
| 141 | }, |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 142 | expected: function () { |
Steven Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame] | 143 | // TODO: original code is: (s && s.expected) && (t && t.expected); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 144 | return true; |
| 145 | }, |
| 146 | online: function () { |
Steven Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame] | 147 | |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 148 | var source = this.get('source'), |
| 149 | target = this.get('target'), |
| 150 | sourceOnline = isLinkOnline(source), |
| 151 | targetOnline = isLinkOnline(target); |
| 152 | |
| 153 | return (sourceOnline) && (targetOnline); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 154 | }, |
Steven Burrows | 86af435 | 2016-11-16 18:19:12 -0600 | [diff] [blame] | 155 | onChange: function () { |
| 156 | // Update class names when the model changes |
| 157 | if (this.el) { |
| 158 | this.el.attr('class', this.svgClassName()); |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 159 | this.setScale(); |
Steven Burrows | 86af435 | 2016-11-16 18:19:12 -0600 | [diff] [blame] | 160 | } |
| 161 | }, |
Steven Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame] | 162 | enhance: function () { |
| 163 | var data = [], |
| 164 | point; |
| 165 | |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 166 | if (showPort()) { |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 167 | this.set('enhanced', true); |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 168 | point = this.locatePortLabel(); |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 169 | angular.extend(point, { |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 170 | id: 'topo2-port-tgt', |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 171 | num: this.get('portB'), |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 172 | }); |
| 173 | data.push(point); |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 174 | |
| 175 | if (this.get('portA')) { |
| 176 | point = this.locatePortLabel(1); |
| 177 | angular.extend(point, { |
Simon Hunt | 95f4b42 | 2017-03-03 13:49:05 -0800 | [diff] [blame] | 178 | id: 'topo2-port-src', |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 179 | num: this.get('portA'), |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 180 | }); |
| 181 | data.push(point); |
| 182 | } |
| 183 | |
Steven Burrows | bd40284 | 2017-03-08 21:30:38 +0000 | [diff] [blame] | 184 | var entering = d3.select('.topo2-portLabels') |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 185 | .selectAll('.portLabel') |
Steven Burrows | a3fca81 | 2016-10-14 15:11:04 -0500 | [diff] [blame] | 186 | .data(data) |
| 187 | .enter().append('g') |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 188 | .classed('portLabel', true) |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 189 | .attr('id', function (d) { return d.id; }); |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 190 | |
| 191 | entering.each(function (d) { |
| 192 | var el = d3.select(this), |
| 193 | rect = el.append('rect'), |
| 194 | text = el.append('text').text(d.num); |
| 195 | |
| 196 | var rectSize = rectAroundText(el); |
| 197 | |
| 198 | rect.attr(rectSize) |
| 199 | .attr('rx', 2) |
| 200 | .attr('ry', 2); |
| 201 | |
| 202 | text.attr('dy', linkLabelOffset) |
| 203 | .attr('text-anchor', 'middle'); |
| 204 | |
| 205 | el.attr('transform', sus.translate(d.x, d.y)); |
Steven Burrows | a3fca81 | 2016-10-14 15:11:04 -0500 | [diff] [blame] | 206 | |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 207 | }); |
Steven Burrows | a3fca81 | 2016-10-14 15:11:04 -0500 | [diff] [blame] | 208 | |
| 209 | this.setScale(); |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 210 | } |
Steven Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame] | 211 | }, |
| 212 | unenhance: function () { |
Steven Burrows | 86af435 | 2016-11-16 18:19:12 -0600 | [diff] [blame] | 213 | this.set('enhanced', false); |
Steven Burrows | bd40284 | 2017-03-08 21:30:38 +0000 | [diff] [blame] | 214 | d3.select('.topo2-portLabels').selectAll('.portLabel').remove(); |
Steven Burrows | 0bc6665 | 2017-04-20 13:04:10 -0400 | [diff] [blame] | 215 | this.setScale(); |
Steven Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame] | 216 | }, |
Steven Burrows | 32ce1d9 | 2017-04-13 13:18:44 -0700 | [diff] [blame] | 217 | amt: function (numLinks, index) { |
Steven Burrows | 9ce0e40 | 2017-04-20 16:39:17 -0400 | [diff] [blame] | 218 | var bbox = this.get('source').el.node().getBBox(), |
| 219 | gap = bbox.width / 4; |
Steven Burrows | 32ce1d9 | 2017-04-13 13:18:44 -0700 | [diff] [blame] | 220 | return (index - ((numLinks - 1) / 2)) * gap; |
| 221 | }, |
| 222 | defaultPosition: function () { |
| 223 | return { |
| 224 | x1: this.get('source').x, |
| 225 | y1: this.get('source').y, |
| 226 | x2: this.get('target').x, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 227 | y2: this.get('target').y, |
| 228 | }; |
Steven Burrows | 32ce1d9 | 2017-04-13 13:18:44 -0700 | [diff] [blame] | 229 | }, |
| 230 | calcMovement: function (amt, flipped) { |
| 231 | var pos = this.defaultPosition(), |
| 232 | mult = flipped ? -amt : amt, |
| 233 | dx = pos.x2 - pos.x1, |
| 234 | dy = pos.y2 - pos.y1, |
| 235 | length = Math.sqrt((dx * dx) + (dy * dy)); |
| 236 | |
| 237 | return { |
| 238 | x1: pos.x1 + (mult * dy / length), |
| 239 | y1: pos.y1 + (mult * -dx / length), |
| 240 | x2: pos.x2 + (mult * dy / length), |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 241 | y2: pos.y2 + (mult * -dx / length), |
Steven Burrows | 32ce1d9 | 2017-04-13 13:18:44 -0700 | [diff] [blame] | 242 | }; |
| 243 | }, |
Steven Burrows | a31c5b0 | 2017-04-12 10:45:08 -0700 | [diff] [blame] | 244 | setPosition: function () { |
Steven Burrows | 32ce1d9 | 2017-04-13 13:18:44 -0700 | [diff] [blame] | 245 | var multiline = this.get('multiline'); |
| 246 | if (multiline) { |
| 247 | var offsetAmt = this.amt(multiline.deviceLinks, multiline.index); |
| 248 | this.set('position', this.calcMovement(offsetAmt)); |
| 249 | } else { |
| 250 | this.set('position', this.defaultPosition()); |
| 251 | } |
Steven Burrows | a31c5b0 | 2017-04-12 10:45:08 -0700 | [diff] [blame] | 252 | |
| 253 | if (this.get('enhanced')) { |
| 254 | this.updatePortPosition(); |
| 255 | } |
Steven Burrows | 9ce0e40 | 2017-04-20 16:39:17 -0400 | [diff] [blame] | 256 | |
| 257 | if (this.el) { |
| 258 | this.el.attr(this.get('position')); |
| 259 | } |
| 260 | |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 261 | if (this.linkLabel) { |
| 262 | this.linkLabel.setPosition(); |
| 263 | } |
Steven Burrows | a31c5b0 | 2017-04-12 10:45:08 -0700 | [diff] [blame] | 264 | }, |
| 265 | updatePortPosition: function () { |
| 266 | var sourcePos = this.locatePortLabel(1), |
| 267 | targetPos = this.locatePortLabel(); |
| 268 | d3.select('#topo2-port-src') |
| 269 | .attr('transform', sus.translate(sourcePos.x, sourcePos.y)); |
| 270 | d3.select('#topo2-port-tgt') |
| 271 | .attr('transform', sus.translate(targetPos.x, targetPos.y)); |
| 272 | }, |
Steven Burrows | 1aa4f58 | 2016-12-13 15:05:41 -0500 | [diff] [blame] | 273 | getSelected: function () { |
| 274 | return this.collection.filter(function (m) { |
| 275 | return m.get('selected'); |
| 276 | }); |
| 277 | }, |
Steven Burrows | 86af435 | 2016-11-16 18:19:12 -0600 | [diff] [blame] | 278 | select: function () { |
Steven Burrows | 5fa057e | 2017-03-15 17:07:56 +0000 | [diff] [blame] | 279 | this.set({ 'selected': true }); |
Steven Burrows | 1aa4f58 | 2016-12-13 15:05:41 -0500 | [diff] [blame] | 280 | return this.getSelected(); |
| 281 | }, |
| 282 | deselect: function () { |
Steven Burrows | 5fa057e | 2017-03-15 17:07:56 +0000 | [diff] [blame] | 283 | this.set({ |
| 284 | 'selected': false, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 285 | 'enhanced': false, |
Steven Burrows | 5fa057e | 2017-03-15 17:07:56 +0000 | [diff] [blame] | 286 | }); |
Steven Burrows | 86af435 | 2016-11-16 18:19:12 -0600 | [diff] [blame] | 287 | }, |
| 288 | showDetails: function () { |
Steven Burrows | 1aa4f58 | 2016-12-13 15:05:41 -0500 | [diff] [blame] | 289 | var selected = this.getSelected(); |
Steven Burrows | 86af435 | 2016-11-16 18:19:12 -0600 | [diff] [blame] | 290 | |
| 291 | if (selected) { |
| 292 | t2lps.displayLink(this); |
| 293 | } else { |
| 294 | t2lps.hide(); |
| 295 | } |
| 296 | }, |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 297 | locatePortLabel: function (src) { |
Steven Burrows | a3fca81 | 2016-10-14 15:11:04 -0500 | [diff] [blame] | 298 | var offset = 32 / (labelDim * t2zs.scale()), |
| 299 | sourceX = this.get('position').x1, |
| 300 | sourceY = this.get('position').y1, |
| 301 | targetX = this.get('position').x2, |
| 302 | targetY = this.get('position').y2, |
| 303 | nearX = src ? sourceX : targetX, |
| 304 | nearY = src ? sourceY : targetY, |
| 305 | farX = src ? targetX : sourceX, |
| 306 | farY = src ? targetY : sourceY; |
Steven Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame] | 307 | |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 308 | function dist(x, y) { |
| 309 | return Math.sqrt(x * x + y * y); |
| 310 | } |
Steven Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame] | 311 | |
| 312 | var dx = farX - nearX, |
| 313 | dy = farY - nearY, |
Steven Burrows | 0bc6665 | 2017-04-20 13:04:10 -0400 | [diff] [blame] | 314 | k = (32 * offset) / dist(dx, dy); |
Steven Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame] | 315 | |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 316 | return { x: k * dx + nearX, y: k * dy + nearY }; |
Steven Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame] | 317 | }, |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 318 | onEnter: function (el) { |
Steven Burrows | 448468c | 2017-04-13 16:09:30 -0700 | [diff] [blame] | 319 | var link = d3.select(el), |
| 320 | th = ts.theme(), |
| 321 | delay = 1000; |
Steven Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame] | 322 | |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 323 | this.el = link; |
Steven Burrows | 448468c | 2017-04-13 16:09:30 -0700 | [diff] [blame] | 324 | |
| 325 | link.transition() |
| 326 | .duration(delay) |
| 327 | .attr('stroke', linkConfig[th].baseColor); |
| 328 | |
Steven Burrows | aea509d | 2017-04-12 14:17:47 -0700 | [diff] [blame] | 329 | this.setVisibility(); |
Steven Burrows | 448468c | 2017-04-13 16:09:30 -0700 | [diff] [blame] | 330 | this.setScale(); |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 331 | }, |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 332 | linkWidth: function () { |
| 333 | var width = widthRatio; |
| 334 | if (this.get('enhanced')) { width = 3.5; } |
| 335 | if (this.linkLabel) { |
| 336 | var scale = d3.scale.ordinal() |
| 337 | .rangeRoundPoints([4, 8]), |
| 338 | label = this.linkLabel.get('label').split(' '); |
| 339 | |
| 340 | switch (t2ts.selectedTrafficOverlay()) { |
| 341 | case 'flowStatsBytes': |
| 342 | scale.domain(['KB', 'MB', 'GB']); |
| 343 | width = scale(label[1]); |
| 344 | break; |
| 345 | case 'portStatsBitSec': |
Steven Burrows | 2b29ca4 | 2017-05-12 10:58:05 -0400 | [diff] [blame] | 346 | scale.domain(['Kbps', 'Mbps', 'Gbps']); |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 347 | width = scale(label[1]); |
| 348 | break; |
| 349 | case 'portStatsPktSec': |
| 350 | scale = d3.scale.linear() |
| 351 | .domain([1, 10, 100, 1000, 10000]) |
| 352 | .range(d3.range(3.5, 9)) |
| 353 | .clamp(true); |
| 354 | width = scale(parseInt(label[0])); |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | return width; |
| 359 | }, |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 360 | setScale: function () { |
Steven Burrows | e8a455a | 2017-03-16 16:58:59 +0000 | [diff] [blame] | 361 | |
| 362 | if (!this.el) return; |
| 363 | |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 364 | var linkWidthRatio = this.linkWidth(); |
Steven Burrows | 0bc6665 | 2017-04-20 13:04:10 -0400 | [diff] [blame] | 365 | |
| 366 | var width = linkScale(linkWidthRatio) / t2zs.scale(); |
Steven Burrows | 448468c | 2017-04-13 16:09:30 -0700 | [diff] [blame] | 367 | this.el.attr('stroke-width', width + 'px'); |
Steven Burrows | a3fca81 | 2016-10-14 15:11:04 -0500 | [diff] [blame] | 368 | |
| 369 | var labelScale = labelDim / (labelDim * t2zs.scale()); |
| 370 | |
Steven Burrows | 8909c5c | 2017-04-10 09:56:52 -0700 | [diff] [blame] | 371 | d3.select('.topo2-portLabels') |
Steven Burrows | a3fca81 | 2016-10-14 15:11:04 -0500 | [diff] [blame] | 372 | .selectAll('.portLabel') |
| 373 | .selectAll('*') |
| 374 | .style('transform', 'scale(' + labelScale + ')'); |
| 375 | |
Steven Burrows | 9ce0e40 | 2017-04-20 16:39:17 -0400 | [diff] [blame] | 376 | this.setPosition(); |
| 377 | |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 378 | if (this.linkLabel) { |
| 379 | this.linkLabel.setScale(); |
| 380 | } |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 381 | }, |
| 382 | update: function () { |
Steven Burrows | 86af435 | 2016-11-16 18:19:12 -0600 | [diff] [blame] | 383 | if (this.get('enhanced')) { |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 384 | this.enhance(); |
| 385 | } |
Steven Burrows | b15a394 | 2017-01-17 17:25:04 +0000 | [diff] [blame] | 386 | }, |
Steven Burrows | aea509d | 2017-04-12 14:17:47 -0700 | [diff] [blame] | 387 | setVisibility: function () { |
| 388 | |
| 389 | if (this.get('type') !== 'UiEdgeLink') { |
| 390 | return; |
| 391 | } |
| 392 | |
| 393 | var visible = ps.getPrefs('topo2_prefs')['hosts']; |
| 394 | this.el.style('visibility', visible ? 'visible' : 'hidden'); |
| 395 | }, |
Steven Burrows | c515e60 | 2017-04-13 11:17:40 -0700 | [diff] [blame] | 396 | displayMastership: function () { |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 397 | this.set({ mastership: t2mss.mastership() !== null }); |
Steven Burrows | c515e60 | 2017-04-13 11:17:40 -0700 | [diff] [blame] | 398 | }, |
Steven Burrows | b15a394 | 2017-01-17 17:25:04 +0000 | [diff] [blame] | 399 | remove: function () { |
| 400 | |
| 401 | var width = linkScale(widthRatio) / t2zs.scale(); |
| 402 | |
| 403 | this.el.transition() |
| 404 | .duration(300) |
| 405 | .attr('stroke', '#ff0000') |
| 406 | .style('stroke-width', width * 4) |
| 407 | .transition() |
| 408 | .delay(1000) |
| 409 | .style('opacity', 0); |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 410 | }, |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 411 | }); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 412 | |
| 413 | var LinkCollection = Collection.extend({ |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 414 | model: LinkModel, |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 415 | }); |
| 416 | |
| 417 | return new LinkCollection(data); |
| 418 | } |
| 419 | |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 420 | function showPort() { |
| 421 | return t2vs.getPortHighlighting(); |
| 422 | } |
| 423 | |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 424 | angular.module('ovTopo2') |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 425 | .factory('Topo2LinkService', [ |
| 426 | '$log', 'Topo2Collection', 'Topo2Model', |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 427 | 'ThemeService', 'SvgUtilService', 'Topo2ZoomService', |
Steven Burrows | aea509d | 2017-04-12 14:17:47 -0700 | [diff] [blame] | 428 | 'Topo2ViewService', 'Topo2LinkPanelService', 'FnService', 'PrefsService', |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 429 | 'Topo2MastershipService', 'Topo2TrafficService', |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 430 | function (_$log_, _c_, _Model_, _ts_, _sus_, |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 431 | _t2zs_, _t2vs_, _t2lps_, _fn_, _ps_, _t2mss_, _t2ts_) { |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 432 | |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 433 | $log = _$log_; |
| 434 | ts = _ts_; |
| 435 | sus = _sus_; |
| 436 | t2zs = _t2zs_; |
| 437 | t2vs = _t2vs_; |
| 438 | Collection = _c_; |
| 439 | Model = _Model_; |
| 440 | t2lps = _t2lps_; |
| 441 | fn = _fn_; |
Steven Burrows | aea509d | 2017-04-12 14:17:47 -0700 | [diff] [blame] | 442 | ps = _ps_; |
Steven Burrows | c515e60 | 2017-04-13 11:17:40 -0700 | [diff] [blame] | 443 | t2mss = _t2mss_; |
Steven Burrows | ca1a39c | 2017-05-08 17:31:08 -0400 | [diff] [blame] | 444 | t2ts = _t2ts_; |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 445 | |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 446 | return { |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 447 | createLinkCollection: createLinkCollection, |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 448 | }; |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 449 | }, |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 450 | ]); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 451 | })(); |