Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +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 | /* |
| 18 | ONOS GUI -- Topology Devices Module. |
| 19 | Module that holds the devices for a region |
| 20 | */ |
| 21 | |
| 22 | (function () { |
| 23 | 'use strict'; |
| 24 | |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame^] | 25 | var Collection, Model, is, sus, ts; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 26 | |
| 27 | var remappedDeviceTypes = { |
| 28 | virtual: 'cord' |
| 29 | }; |
| 30 | |
| 31 | // configuration |
| 32 | var devIconDim = 36, |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame^] | 33 | halfDevIcon = devIconDim / 2; |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 34 | |
| 35 | function createDeviceCollection(data, region) { |
| 36 | |
| 37 | var DeviceCollection = Collection.extend({ |
| 38 | model: Model, |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame^] | 39 | comparator: function (a, b) { |
| 40 | var order = region.get('layerOrder'), |
| 41 | aLayer = a.get('layer'), |
| 42 | bLayer = b.get('layer'); |
| 43 | return order.indexOf(aLayer - order.indexOf(bLayer)); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 44 | } |
| 45 | }); |
| 46 | |
| 47 | var devices = []; |
| 48 | data.forEach(function (deviceLayer) { |
| 49 | deviceLayer.forEach(function (device) { |
| 50 | devices.push(device); |
| 51 | }); |
| 52 | }); |
| 53 | |
| 54 | var deviceCollection = new DeviceCollection(devices); |
| 55 | deviceCollection.sort(); |
| 56 | |
| 57 | return deviceCollection; |
| 58 | } |
| 59 | |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 60 | function mapDeviceTypeToGlyph(type) { |
| 61 | return remappedDeviceTypes[type] || type || 'unknown'; |
| 62 | } |
| 63 | |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 64 | function iconBox(dim, labelWidth) { |
| 65 | return { |
| 66 | x: -dim / 2, |
| 67 | y: -dim / 2, |
| 68 | width: dim + labelWidth, |
| 69 | height: dim |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame^] | 70 | }; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 71 | } |
| 72 | |
Steven Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame] | 73 | // note: these are the device icon colors without affinity (no master) |
| 74 | var dColTheme = { |
| 75 | light: { |
| 76 | online: '#444444', |
| 77 | offline: '#cccccc' |
| 78 | }, |
| 79 | dark: { |
| 80 | // TODO: theme |
| 81 | online: '#444444', |
| 82 | offline: '#cccccc' |
| 83 | } |
| 84 | }; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 85 | |
Steven Burrows | 9edc7e0 | 2016-08-29 11:52:07 +0100 | [diff] [blame] | 86 | function deviceGlyphColor(d) { |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 87 | var o = this.node.online, |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 88 | id = this.node.master, // TODO: This should be from node.master |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 89 | otag = o ? 'online' : 'offline'; |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame^] | 90 | |
| 91 | return o ? sus.cat7().getColor(id, 0, ts.theme()) : |
| 92 | dColTheme[ts.theme()][otag]; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | function setDeviceColor() { |
| 96 | this.el.select('use') |
| 97 | .style('fill', this.deviceGlyphColor()); |
| 98 | } |
| 99 | |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 100 | angular.module('ovTopo2') |
| 101 | .factory('Topo2DeviceService', |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame^] | 102 | ['Topo2Collection', 'Topo2NodeModel', 'IconService', |
| 103 | 'SvgUtilService', 'ThemeService', |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 104 | |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame^] | 105 | function (_c_, _nm_, _is_, _sus_, _ts_, classnames) { |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 106 | |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 107 | is = _is_; |
| 108 | sus = _sus_; |
| 109 | ts = _ts_; |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame^] | 110 | Collection = _c_; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 111 | |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame^] | 112 | Model = _nm_.extend({ |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 113 | initialize: function () { |
| 114 | this.set('weight', 0); |
| 115 | this.constructor.__super__.initialize.apply(this, arguments); |
| 116 | }, |
| 117 | nodeType: 'device', |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 118 | deviceGlyphColor: deviceGlyphColor, |
| 119 | mapDeviceTypeToGlyph: mapDeviceTypeToGlyph, |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 120 | setDeviceColor: setDeviceColor, |
| 121 | onEnter: function (el) { |
| 122 | |
| 123 | var node = d3.select(el), |
| 124 | glyphId = mapDeviceTypeToGlyph(this.get('type')), |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 125 | label = this.trimLabel(this.label()), |
| 126 | glyph, labelWidth; |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 127 | |
| 128 | this.el = node; |
| 129 | |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 130 | // Label |
| 131 | var labelElements = this.addLabelElements(label); |
| 132 | labelWidth = label ? this.computeLabelWidth(node) : 0; |
| 133 | labelElements.rect.attr(iconBox(devIconDim, labelWidth)); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 134 | |
Steven Burrows | 6deb4ce | 2016-08-26 16:06:23 +0100 | [diff] [blame] | 135 | // Icon |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 136 | glyph = is.addDeviceIcon(node, glyphId, devIconDim); |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 137 | glyph.attr(iconBox(devIconDim, 0)); |
| 138 | |
| 139 | node.attr('transform', sus.translate(-halfDevIcon, -halfDevIcon)); |
| 140 | this.render(); |
| 141 | }, |
Steven Burrows | dfa52b0 | 2016-09-02 13:50:43 +0100 | [diff] [blame^] | 142 | onExit: function () { |
| 143 | var node = this.el; |
| 144 | node.select('use') |
| 145 | .style('opacity', 0.5) |
| 146 | .transition() |
| 147 | .duration(800) |
| 148 | .style('opacity', 0); |
| 149 | |
| 150 | node.selectAll('rect') |
| 151 | .style('stroke-fill', '#555') |
| 152 | .style('fill', '#888') |
| 153 | .style('opacity', 0.5); |
| 154 | }, |
Steven Burrows | ec1f45c | 2016-08-08 16:14:41 +0100 | [diff] [blame] | 155 | render: function () { |
| 156 | this.setDeviceColor(); |
| 157 | } |
| 158 | }); |
Steven Burrows | 57e24e9 | 2016-08-04 18:38:24 +0100 | [diff] [blame] | 159 | |
| 160 | return { |
| 161 | createDeviceCollection: createDeviceCollection |
| 162 | }; |
| 163 | } |
| 164 | ]); |
| 165 | |
| 166 | })(); |