Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 1 | /* |
| 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 | /* |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 18 | ONOS GUI -- Topology Node Module. |
| 19 | Module that contains model for nodes within the topology |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | (function () { |
| 23 | 'use strict'; |
| 24 | |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 25 | var ps, sus, is, ts, t2mcs, t2nps, fn; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 26 | |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 27 | var devIconDim = 36, |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 28 | devIconDimMin = 20, |
| 29 | devIconDimMax = 40, |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 30 | labelPad = 5, |
| 31 | textPad = 5, |
| 32 | halfDevIcon = devIconDim / 2; |
| 33 | |
| 34 | // note: these are the device icon colors without affinity (no master) |
| 35 | var dColTheme = { |
| 36 | light: { |
| 37 | online: '#444444', |
| 38 | offline: '#cccccc' |
| 39 | }, |
| 40 | dark: { |
| 41 | // TODO: theme |
| 42 | online: '#444444', |
| 43 | offline: '#cccccc' |
| 44 | } |
| 45 | }; |
| 46 | |
| 47 | function devGlyphColor(d) { |
| 48 | var o = this.get('online'), |
| 49 | id = this.get('master'), |
| 50 | otag = o ? 'online' : 'offline'; |
| 51 | return o ? sus.cat7().getColor(id, 0, ts.theme()) : |
| 52 | dColTheme[ts.theme()][otag]; |
| 53 | } |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 54 | |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 55 | angular.module('ovTopo2') |
| 56 | .factory('Topo2NodeModel', |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 57 | ['Topo2Model', 'FnService', 'Topo2PrefsService', |
Steven Burrows | 482d950 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 58 | 'SvgUtilService', 'IconService', 'ThemeService', |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 59 | 'Topo2MapConfigService', 'Topo2ZoomService', 'Topo2NodePositionService', |
| 60 | function (Model, _fn_, _ps_, _sus_, _is_, _ts_, |
| 61 | _t2mcs_, zoomService, _t2nps_) { |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 62 | |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 63 | ts = _ts_; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 64 | fn = _fn_; |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 65 | ps = _ps_; |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 66 | sus = _sus_; |
| 67 | is = _is_; |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 68 | t2mcs = _t2mcs_; |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 69 | t2nps = _t2nps_; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 70 | |
| 71 | return Model.extend({ |
| 72 | initialize: function () { |
| 73 | this.node = this.createNode(); |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 74 | this._events = { |
| 75 | 'mouseover': 'mouseoverHandler', |
| 76 | 'mouseout': 'mouseoutHandler' |
| 77 | }; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 78 | }, |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 79 | createNode: function () { |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 80 | this.set('svgClass', this.svgClassName()); |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 81 | t2nps.positionNode(this); |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 82 | return this; |
| 83 | }, |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 84 | setUpEvents: function () { |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 85 | var _this = this, |
| 86 | events = angular.extend({}, this._events, this.events); |
| 87 | angular.forEach(events, function (handler, key) { |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 88 | _this.el.on(key, _this[handler].bind(_this)); |
| 89 | }); |
| 90 | }, |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 91 | mouseoverHandler: function () { |
| 92 | this.set('hovered', true); |
| 93 | }, |
| 94 | mouseoutHandler: function () { |
| 95 | this.set('hovered', false); |
| 96 | }, |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 97 | icon: function () { |
| 98 | return 'unknown'; |
| 99 | }, |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame^] | 100 | labelIndex: function () { |
| 101 | return ps.get('dlbls'); |
| 102 | }, |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 103 | label: function () { |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 104 | var props = this.get('props'), |
| 105 | id = this.get('id'), |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 106 | friendlyName = props && props.name ? props.name : id, |
| 107 | labels = ['', friendlyName || id, id], |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame^] | 108 | nli = this.labelIndex(), |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 109 | idx = (nli < labels.length) ? nli : 0; |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 110 | |
| 111 | return labels[idx]; |
| 112 | }, |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 113 | trimLabel: function (label) { |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 114 | return (label && label.trim()) || ''; |
| 115 | }, |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 116 | computeLabelWidth: function (el) { |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 117 | var text = el.select('text'), |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 118 | box = text.node().getBBox(); |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 119 | return box.width + labelPad * 2; |
| 120 | }, |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 121 | addLabelElements: function (label) { |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 122 | var rect = this.el.append('rect') |
| 123 | .attr('class', 'node-container'); |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 124 | var glythRect = this.el.append('rect') |
| 125 | .attr('y', -halfDevIcon) |
| 126 | .attr('x', -halfDevIcon) |
| 127 | .attr('width', devIconDim) |
| 128 | .attr('height', devIconDim) |
| 129 | .style('fill', devGlyphColor.bind(this)); |
| 130 | |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 131 | var text = this.el.append('text').text(label) |
| 132 | .attr('text-anchor', 'left') |
| 133 | .attr('y', '0.3em') |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 134 | .attr('x', halfDevIcon + labelPad + textPad); |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 135 | |
| 136 | return { |
| 137 | rect: rect, |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 138 | glythRect: glythRect, |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 139 | text: text |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 140 | }; |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 141 | }, |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 142 | labelBox: function (dim, labelWidth) { |
| 143 | var _textPad = (textPad * 2) - labelPad; |
| 144 | |
| 145 | if (labelWidth === 0) { |
| 146 | _textPad = 0; |
| 147 | } |
| 148 | |
| 149 | return { |
| 150 | x: -dim / 2 - labelPad, |
| 151 | y: -dim / 2 - labelPad, |
| 152 | width: dim + labelWidth + (labelPad * 2) + _textPad, |
| 153 | height: dim + (labelPad * 2) |
| 154 | }; |
| 155 | }, |
| 156 | iconBox: function (dim, labelWidth) { |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 157 | return { |
| 158 | x: -dim / 2, |
| 159 | y: -dim / 2, |
| 160 | width: dim + labelWidth, |
| 161 | height: dim |
| 162 | }; |
| 163 | }, |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 164 | svgClassName: function () { |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 165 | return fn.classNames('node', |
| 166 | this.nodeType, |
| 167 | this.get('type'), |
| 168 | { |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 169 | online: this.get('online'), |
| 170 | selected: this.get('selected') |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame] | 171 | } |
| 172 | ); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 173 | }, |
Steven Burrows | e7cc308 | 2016-09-27 11:24:58 -0700 | [diff] [blame] | 174 | lngLatFromCoord: function (coord) { |
| 175 | var p = t2mcs.projection(); |
| 176 | return p ? p.invert(coord) : [0, 0]; |
| 177 | }, |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 178 | resetPosition: function () { |
| 179 | t2nps.setLongLat(this); |
| 180 | }, |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 181 | update: function () { |
| 182 | this.updateLabel(); |
| 183 | }, |
| 184 | updateLabel: function () { |
| 185 | var node = this.el, |
| 186 | label = this.trimLabel(this.label()), |
| 187 | labelWidth; |
| 188 | |
| 189 | node.select('text').text(label); |
| 190 | labelWidth = label ? this.computeLabelWidth(node) : 0; |
| 191 | |
| 192 | node.select('rect') |
| 193 | .transition() |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 194 | .attr(this.labelBox(devIconDim, labelWidth)); |
Steven Burrows | 37549ee | 2016-09-21 14:41:39 +0100 | [diff] [blame] | 195 | }, |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 196 | onEnter: function (el) { |
| 197 | this.el = d3.select(el); |
| 198 | this.render(); |
| 199 | }, |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 200 | setScale: function () { |
| 201 | |
| 202 | var dim = devIconDim, |
| 203 | multipler = 1; |
| 204 | |
| 205 | if (dim * zoomService.scale() < devIconDimMin) { |
| 206 | multipler = devIconDimMin / (dim * zoomService.scale()); |
| 207 | } else if (dim * zoomService.scale() > devIconDimMax) { |
| 208 | multipler = devIconDimMax / (dim * zoomService.scale()); |
| 209 | } |
| 210 | |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 211 | this.el.selectAll('*') |
| 212 | .style('transform', 'scale(' + multipler + ')'); |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 213 | }, |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 214 | render: function () { |
| 215 | var node = this.el, |
| 216 | glyphId = this.icon(this.get('type')), |
| 217 | label = this.trimLabel(this.label()), |
| 218 | glyph, labelWidth; |
| 219 | |
| 220 | // Label |
| 221 | var labelElements = this.addLabelElements(label); |
| 222 | labelWidth = label ? this.computeLabelWidth(node) : 0; |
Steven Burrows | 1c5c861 | 2016-10-05 13:45:13 -0500 | [diff] [blame] | 223 | labelElements.rect |
| 224 | .attr(this.labelBox(devIconDim, labelWidth)); |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 225 | |
| 226 | // Icon |
| 227 | glyph = is.addDeviceIcon(node, glyphId, devIconDim); |
| 228 | glyph.attr(this.iconBox(devIconDim, 0)); |
| 229 | glyph.style('fill', 'white'); |
| 230 | |
Steven Burrows | 583f4be | 2016-11-04 14:06:50 +0100 | [diff] [blame^] | 231 | node.attr('transform', |
| 232 | sus.translate(-halfDevIcon, -halfDevIcon)); |
Steven Burrows | bbe3dda | 2016-09-26 14:41:59 -0700 | [diff] [blame] | 233 | |
| 234 | if (this.events) { |
| 235 | this.setUpEvents(); |
| 236 | } |
Steven Burrows | 0616e80 | 2016-10-06 21:45:07 -0500 | [diff] [blame] | 237 | |
| 238 | this.setScale(); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 239 | } |
| 240 | }); |
| 241 | }] |
| 242 | ); |
| 243 | })(); |