Steven Burrows | 9edc7e0 | 2016-08-29 11:52:07 +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 | 9edc7e0 | 2016-08-29 11:52:07 +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 Select Module. |
| 19 | */ |
| 20 | |
| 21 | (function () { |
| 22 | 'use strict'; |
| 23 | |
Steven Burrows | 5fa057e | 2017-03-15 17:07:56 +0000 | [diff] [blame] | 24 | var t2zs, t2ddp; |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 25 | |
Steven Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame] | 26 | // internal state |
Steven Burrows | 5fa057e | 2017-03-15 17:07:56 +0000 | [diff] [blame] | 27 | var instance, |
| 28 | consumeClick, |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 29 | zoomer, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 30 | previousNearestLink; // previous link to mouse position |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 31 | |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 32 | function mouseClickHandler() { |
Steven Burrows | 5fa057e | 2017-03-15 17:07:56 +0000 | [diff] [blame] | 33 | if (d3.event.defaultPrevented) return; |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 34 | |
| 35 | if (!d3.event.shiftKey) { |
Steven Burrows | 5fa057e | 2017-03-15 17:07:56 +0000 | [diff] [blame] | 36 | this.clearSelection(); |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Steven Burrows | 5fa057e | 2017-03-15 17:07:56 +0000 | [diff] [blame] | 39 | if (!this.clickConsumed()) { |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 40 | if (previousNearestLink) { |
Steven Burrows | 5fa057e | 2017-03-15 17:07:56 +0000 | [diff] [blame] | 41 | this.selectObject(previousNearestLink, true); |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 42 | } |
| 43 | } |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | // Select Links |
Steven Burrows | 5fa057e | 2017-03-15 17:07:56 +0000 | [diff] [blame] | 47 | function mouseMoveHandler(ev) { |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 48 | var mp = getLogicalMousePosition(this), |
| 49 | link = computeNearestLink(mp); |
| 50 | |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 51 | if (link) { |
| 52 | if (previousNearestLink && previousNearestLink !== link) { |
| 53 | previousNearestLink.unenhance(); |
| 54 | } |
| 55 | link.enhance(); |
| 56 | } else if (previousNearestLink) { |
| 57 | previousNearestLink.unenhance(); |
| 58 | } |
| 59 | |
| 60 | previousNearestLink = link; |
| 61 | } |
| 62 | |
| 63 | function getLogicalMousePosition(container) { |
| 64 | var m = d3.mouse(container), |
| 65 | sc = zoomer.scale(), |
| 66 | tr = zoomer.translate(), |
| 67 | mx = (m[0] - tr[0]) / sc, |
| 68 | my = (m[1] - tr[1]) / sc; |
| 69 | return { x: mx, y: my }; |
| 70 | } |
| 71 | |
| 72 | function sq(x) { |
| 73 | return x * x; |
| 74 | } |
| 75 | |
| 76 | function mdist(p, m) { |
| 77 | return Math.sqrt(sq(p.x - m.x) + sq(p.y - m.y)); |
| 78 | } |
| 79 | |
| 80 | function prox(dist) { |
| 81 | return dist / zoomer.scale(); |
| 82 | } |
| 83 | |
| 84 | function computeNearestLink(mouse) { |
| 85 | var proximity = prox(30), |
| 86 | nearest = null, |
| 87 | minDist; |
| 88 | |
| 89 | function pdrop(line, mouse) { |
| 90 | var x1 = line.x1, |
| 91 | y1 = line.y1, |
| 92 | x2 = line.x2, |
| 93 | y2 = line.y2, |
| 94 | x3 = mouse.x, |
| 95 | y3 = mouse.y, |
| 96 | k = ((y2 - y1) * (x3 - x1) - (x2 - x1) * (y3 - y1)) / |
| 97 | (sq(y2 - y1) + sq(x2 - x1)), |
| 98 | x4 = x3 - k * (y2 - y1), |
| 99 | y4 = y3 + k * (x2 - x1); |
| 100 | return { x: x4, y: y4 }; |
| 101 | } |
| 102 | |
| 103 | function lineHit(line, p, m) { |
| 104 | if (p.x < line.x1 && p.x < line.x2) return false; |
| 105 | if (p.x > line.x1 && p.x > line.x2) return false; |
| 106 | if (p.y < line.y1 && p.y < line.y2) return false; |
| 107 | if (p.y > line.y1 && p.y > line.y2) return false; |
| 108 | // line intersects, but are we close enough? |
| 109 | return mdist(p, m) <= proximity; |
| 110 | } |
| 111 | |
Steven Burrows | b11a8b8 | 2017-03-10 16:00:31 +0000 | [diff] [blame] | 112 | var links = []; |
| 113 | |
Steven Burrows | 5fa057e | 2017-03-15 17:07:56 +0000 | [diff] [blame] | 114 | if (instance.region.model.get('links')) { |
| 115 | links = instance.region.regionLinks(); |
Steven Burrows | b11a8b8 | 2017-03-10 16:00:31 +0000 | [diff] [blame] | 116 | } |
Steven Burrows | af96a21 | 2016-12-28 12:57:02 +0000 | [diff] [blame] | 117 | |
| 118 | if (links.length) { |
| 119 | minDist = proximity * 2; |
| 120 | |
| 121 | links.forEach(function (d) { |
| 122 | var line = d.get('position'), |
| 123 | point, |
| 124 | hit, |
| 125 | dist; |
| 126 | |
| 127 | // TODO: Reinstate when showHost() is implemented |
| 128 | // if (!api.showHosts() && d.type() === 'hostLink') { |
| 129 | // return; // skip hidden host links |
| 130 | // } |
| 131 | |
| 132 | if (line) { |
| 133 | point = pdrop(line, mouse); |
| 134 | hit = lineHit(line, point, mouse); |
| 135 | if (hit) { |
| 136 | dist = mdist(point, mouse); |
| 137 | if (dist < minDist) { |
| 138 | minDist = dist; |
| 139 | nearest = d; |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | }); |
| 144 | } |
| 145 | |
| 146 | return nearest; |
| 147 | } |
| 148 | |
Steven Burrows | 5fa057e | 2017-03-15 17:07:56 +0000 | [diff] [blame] | 149 | var SelectionService = function () { |
| 150 | instance = this; |
| 151 | this.selectedNodes = []; |
Steven Burrows | 247ab15 | 2017-03-29 13:55:54 +0100 | [diff] [blame] | 152 | this.selectionOrder = []; |
Steven Burrows | 5fa057e | 2017-03-15 17:07:56 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | SelectionService.prototype = { |
| 156 | init: function () { |
| 157 | zoomer = t2zs.getZoomer(); |
| 158 | |
| 159 | var svg = d3.select('#topo2'); |
| 160 | svg.on('mousemove', mouseMoveHandler); |
| 161 | svg.on('click', mouseClickHandler.bind(this)); |
| 162 | }, |
| 163 | updateDetails: function () { |
| 164 | |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 165 | var nodeCount = this.selectedNodes.length; |
Steven Burrows | 5fa057e | 2017-03-15 17:07:56 +0000 | [diff] [blame] | 166 | |
| 167 | if (nodeCount === 1) { |
| 168 | this.selectedNodes[0].showDetails(); |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 169 | } else if (nodeCount > 1) { |
Steven Burrows | 5fa057e | 2017-03-15 17:07:56 +0000 | [diff] [blame] | 170 | t2ddp.showMulti(this.selectedNodes); |
| 171 | } else { |
| 172 | t2ddp.hide(); |
| 173 | } |
| 174 | }, |
| 175 | selectObject: function (node, multiSelectEnabled) { |
| 176 | |
Steven Burrows | 247ab15 | 2017-03-29 13:55:54 +0100 | [diff] [blame] | 177 | var event = d3.event, |
| 178 | nodeIndex = _.indexOf(this.selectionOrder, node.get('id')); |
Steven Burrows | 5fa057e | 2017-03-15 17:07:56 +0000 | [diff] [blame] | 179 | |
| 180 | if (nodeIndex < 0) { |
Steven Burrows | 247ab15 | 2017-03-29 13:55:54 +0100 | [diff] [blame] | 181 | |
| 182 | if (multiSelectEnabled && !event.shiftKey || !multiSelectEnabled) { |
| 183 | this.clearSelection(); |
| 184 | } |
| 185 | |
Steven Burrows | 5fa057e | 2017-03-15 17:07:56 +0000 | [diff] [blame] | 186 | this.selectedNodes.push(node); |
Steven Burrows | 247ab15 | 2017-03-29 13:55:54 +0100 | [diff] [blame] | 187 | this.selectionOrder.push(node.get('id')); |
| 188 | |
Steven Burrows | 5fa057e | 2017-03-15 17:07:56 +0000 | [diff] [blame] | 189 | node.select(); |
| 190 | } else { |
| 191 | this.removeNode(node, nodeIndex); |
| 192 | } |
| 193 | |
| 194 | this.updateDetails(); |
| 195 | }, |
| 196 | removeNode: function (node, index) { |
| 197 | this.selectedNodes.splice(index, 1); |
Steven Burrows | 247ab15 | 2017-03-29 13:55:54 +0100 | [diff] [blame] | 198 | this.selectionOrder.splice(index, 1); |
Steven Burrows | 5fa057e | 2017-03-15 17:07:56 +0000 | [diff] [blame] | 199 | node.deselect(); |
| 200 | }, |
| 201 | clearSelection: function () { |
| 202 | _.each(this.selectedNodes, function (node) { |
| 203 | node.deselect(); |
| 204 | }); |
| 205 | |
| 206 | this.selectedNodes = []; |
Steven Burrows | 247ab15 | 2017-03-29 13:55:54 +0100 | [diff] [blame] | 207 | this.selectionOrder = []; |
Steven Burrows | 5fa057e | 2017-03-15 17:07:56 +0000 | [diff] [blame] | 208 | this.updateDetails(); |
| 209 | }, |
| 210 | clickConsumed: function (x) { |
| 211 | var cc = consumeClick; |
| 212 | consumeClick = Boolean(x); |
| 213 | return cc; |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 214 | }, |
Steven Burrows | 5fa057e | 2017-03-15 17:07:56 +0000 | [diff] [blame] | 215 | }; |
| 216 | |
Steven Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame] | 217 | angular.module('ovTopo2') |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 218 | .factory('Topo2SelectService', [ |
Steven Burrows | 5fa057e | 2017-03-15 17:07:56 +0000 | [diff] [blame] | 219 | 'Topo2ZoomService', 'Topo2DeviceDetailsPanel', |
| 220 | function (_t2zs_, _t2ddp_) { |
Steven Burrows | bd40284 | 2017-03-08 21:30:38 +0000 | [diff] [blame] | 221 | t2zs = _t2zs_; |
Steven Burrows | 5fa057e | 2017-03-15 17:07:56 +0000 | [diff] [blame] | 222 | t2ddp = _t2ddp_; |
| 223 | return instance || new SelectionService(); |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 224 | }, |
Steven Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame] | 225 | ]); |
| 226 | |
| 227 | })(); |