Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 1 | /* |
Jian Li | 1f54473 | 2015-12-30 23:36:37 -0800 | [diff] [blame] | 2 | * Copyright 2015,2016 Open Networking Laboratory |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [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 -- SVG -- Icon Service |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 19 | */ |
| 20 | (function () { |
| 21 | 'use strict'; |
| 22 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 23 | var $log, fs, gs, sus; |
Simon Hunt | 7f172cc | 2015-01-16 17:43:00 -0800 | [diff] [blame] | 24 | |
Simon Hunt | 9722538 | 2015-01-19 13:33:09 -0800 | [diff] [blame] | 25 | var vboxSize = 50, |
| 26 | cornerSize = vboxSize / 10, |
| 27 | viewBox = '0 0 ' + vboxSize + ' ' + vboxSize; |
Simon Hunt | 7f172cc | 2015-01-16 17:43:00 -0800 | [diff] [blame] | 28 | |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 29 | // Maps icon ID to the glyph ID it uses. |
| 30 | // NOTE: icon ID maps to a CSS class for styling that icon |
Simon Hunt | 7f172cc | 2015-01-16 17:43:00 -0800 | [diff] [blame] | 31 | var glyphMapping = { |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 32 | active: 'checkMark', |
| 33 | inactive: 'xMark', |
Thomas Vachuska | 0fa583c | 2015-03-30 23:07:41 -0700 | [diff] [blame] | 34 | |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 35 | plus: 'plus', |
| 36 | minus: 'minus', |
| 37 | play: 'play', |
| 38 | stop: 'stop', |
| 39 | |
Simon Hunt | 4e41273 | 2015-10-27 15:25:39 -0700 | [diff] [blame] | 40 | topo: 'topo', |
| 41 | |
Bri Prebilic Cole | 43f17c0 | 2015-05-01 10:43:38 -0700 | [diff] [blame] | 42 | refresh: 'refresh', |
Bri Prebilic Cole | bd0bc77 | 2015-05-13 13:02:26 -0700 | [diff] [blame] | 43 | garbage: 'garbage', |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame] | 44 | |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 45 | upArrow: 'triangleUp', |
| 46 | downArrow: 'triangleDown', |
| 47 | |
| 48 | appInactive: 'unknown', |
| 49 | |
Bri Prebilic Cole | dea0974 | 2015-02-12 15:33:50 -0800 | [diff] [blame] | 50 | devIcon_SWITCH: 'switch', |
Thomas Vachuska | 58eded6 | 2015-03-31 12:04:03 -0700 | [diff] [blame] | 51 | devIcon_ROADM: 'roadm', |
Rimon Ashkenazy | 8ebfff0 | 2016-02-01 11:56:36 +0200 | [diff] [blame] | 52 | devIcon_OTN: 'otn', |
Simon Hunt | 864333a | 2015-11-16 17:08:08 -0800 | [diff] [blame] | 53 | deviceTable: 'switch', |
Bri Prebilic Cole | 9b1fb9a | 2015-07-01 13:57:11 -0700 | [diff] [blame] | 54 | flowTable: 'flowTable', |
| 55 | portTable: 'portTable', |
| 56 | groupTable: 'groupTable', |
Jian Li | 1f54473 | 2015-12-30 23:36:37 -0800 | [diff] [blame] | 57 | meterTable: 'meterTable', |
Thomas Vachuska | 58eded6 | 2015-03-31 12:04:03 -0700 | [diff] [blame] | 58 | |
| 59 | hostIcon_endstation: 'endstation', |
| 60 | hostIcon_router: 'router', |
Simon Hunt | 20e1679 | 2015-04-24 14:29:39 -0700 | [diff] [blame] | 61 | hostIcon_bgpSpeaker: 'bgpSpeaker', |
| 62 | |
| 63 | nav_apps: 'bird', |
Thomas Vachuska | aa8b0eb | 2015-05-22 09:54:15 -0700 | [diff] [blame] | 64 | nav_settings: 'chain', |
Simon Hunt | 20e1679 | 2015-04-24 14:29:39 -0700 | [diff] [blame] | 65 | nav_cluster: 'node', |
Bri Prebilic Cole | 6ed04eb | 2015-04-27 16:26:03 -0700 | [diff] [blame] | 66 | nav_topo: 'topo', |
Simon Hunt | 20e1679 | 2015-04-24 14:29:39 -0700 | [diff] [blame] | 67 | nav_devs: 'switch', |
| 68 | nav_links: 'ports', |
| 69 | nav_hosts: 'endstation', |
Thomas Vachuska | 3ece373 | 2015-09-22 23:58:50 -0700 | [diff] [blame] | 70 | nav_intents: 'relatedIntents', |
| 71 | nav_processors: 'allTraffic' |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 72 | }; |
| 73 | |
Simon Hunt | 9722538 | 2015-01-19 13:33:09 -0800 | [diff] [blame] | 74 | function ensureIconLibDefs() { |
| 75 | var body = d3.select('body'), |
Simon Hunt | 24d0c5c | 2016-03-03 00:05:08 -0800 | [diff] [blame] | 76 | svg = body.select('svg#IconLibDefs'); |
Simon Hunt | 9722538 | 2015-01-19 13:33:09 -0800 | [diff] [blame] | 77 | |
| 78 | if (svg.empty()) { |
| 79 | svg = body.append('svg').attr('id', 'IconLibDefs'); |
Simon Hunt | 24d0c5c | 2016-03-03 00:05:08 -0800 | [diff] [blame] | 80 | svg.append('defs'); |
Simon Hunt | 9722538 | 2015-01-19 13:33:09 -0800 | [diff] [blame] | 81 | } |
| 82 | return svg.select('defs'); |
| 83 | } |
| 84 | |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 85 | // div is a D3 selection of the <DIV> element into which icon should load |
Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 86 | // glyphId identifies the glyph to use |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 87 | // size is dimension of icon in pixels. Defaults to 20. |
| 88 | // installGlyph, if truthy, will cause the glyph to be added to |
| 89 | // well-known defs element. Defaults to false. |
| 90 | // svgClass is the CSS class used to identify the SVG layer. |
| 91 | // Defaults to 'embeddedIcon'. |
Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 92 | function loadIcon(div, glyphId, size, installGlyph, svgClass) { |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 93 | var dim = size || 20, |
| 94 | svgCls = svgClass || 'embeddedIcon', |
Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 95 | gid = glyphId || 'unknown', |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 96 | svg, g; |
| 97 | |
| 98 | if (installGlyph) { |
| 99 | gs.loadDefs(ensureIconLibDefs(), [gid], true); |
| 100 | } |
| 101 | |
| 102 | svg = div.append('svg').attr({ |
| 103 | 'class': svgCls, |
| 104 | width: dim, |
| 105 | height: dim, |
| 106 | viewBox: viewBox |
| 107 | }); |
| 108 | |
| 109 | g = svg.append('g').attr({ |
Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 110 | 'class': 'icon' |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 111 | }); |
| 112 | |
| 113 | g.append('rect').attr({ |
| 114 | width: vboxSize, |
| 115 | height: vboxSize, |
| 116 | rx: cornerSize |
| 117 | }); |
| 118 | |
Bri Prebilic Cole | e1bda3f | 2015-02-13 17:01:49 -0800 | [diff] [blame] | 119 | g.append('use').attr({ |
| 120 | width: vboxSize, |
| 121 | height: vboxSize, |
| 122 | 'class': 'glyph', |
| 123 | 'xlink:href': '#' + gid |
| 124 | }); |
Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | // div is a D3 selection of the <DIV> element into which icon should load |
| 128 | // iconCls is the CSS class used to identify the icon |
| 129 | // size is dimension of icon in pixels. Defaults to 20. |
| 130 | // installGlyph, if truthy, will cause the glyph to be added to |
| 131 | // well-known defs element. Defaults to false. |
| 132 | // svgClass is the CSS class used to identify the SVG layer. |
| 133 | // Defaults to 'embeddedIcon'. |
| 134 | function loadIconByClass(div, iconCls, size, installGlyph, svgClass) { |
| 135 | loadIcon(div, glyphMapping[iconCls], size, installGlyph, svgClass); |
| 136 | div.select('svg g').classed(iconCls, true); |
| 137 | } |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 138 | |
| 139 | function loadEmbeddedIcon(div, iconCls, size) { |
Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 140 | loadIconByClass(div, iconCls, size, true); |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | |
| 144 | // configuration for device and host icons in the topology view |
| 145 | var config = { |
| 146 | device: { |
| 147 | dim: 36, |
| 148 | rx: 4 |
| 149 | }, |
| 150 | host: { |
Simon Hunt | c2bfe33 | 2015-12-04 11:01:24 -0800 | [diff] [blame] | 151 | badge: { |
| 152 | dx: 14, |
| 153 | dy: -14 |
| 154 | }, |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 155 | radius: { |
| 156 | noGlyph: 9, |
| 157 | withGlyph: 14 |
| 158 | }, |
| 159 | glyphed: { |
| 160 | endstation: 1, |
| 161 | bgpSpeaker: 1, |
| 162 | router: 1 |
| 163 | } |
| 164 | } |
| 165 | }; |
| 166 | |
| 167 | |
| 168 | // Adds a device icon to the specified element, using the given glyph. |
| 169 | // Returns the D3 selection of the icon. |
| 170 | function addDeviceIcon(elem, glyphId) { |
| 171 | var cfg = config.device, |
Simon Hunt | 044f2807 | 2015-10-08 12:38:16 -0700 | [diff] [blame] | 172 | gid = gs.glyphDefined(glyphId) ? glyphId : 'query', |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 173 | g = elem.append('g') |
| 174 | .attr('class', 'svgIcon deviceIcon'); |
| 175 | |
| 176 | g.append('rect').attr({ |
| 177 | x: 0, |
| 178 | y: 0, |
| 179 | rx: cfg.rx, |
| 180 | width: cfg.dim, |
| 181 | height: cfg.dim |
| 182 | }); |
| 183 | |
| 184 | g.append('use').attr({ |
Simon Hunt | 044f2807 | 2015-10-08 12:38:16 -0700 | [diff] [blame] | 185 | 'xlink:href': '#' + gid, |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 186 | width: cfg.dim, |
| 187 | height: cfg.dim |
| 188 | }); |
| 189 | |
| 190 | g.dim = cfg.dim; |
| 191 | return g; |
| 192 | } |
| 193 | |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 194 | function addHostIcon(elem, radius, glyphId) { |
| 195 | var dim = radius * 1.5, |
| 196 | xlate = -dim / 2, |
| 197 | g = elem.append('g') |
| 198 | .attr('class', 'svgIcon hostIcon'); |
| 199 | |
| 200 | g.append('circle').attr('r', radius); |
| 201 | |
| 202 | g.append('use').attr({ |
| 203 | 'xlink:href': '#' + glyphId, |
| 204 | width: dim, |
| 205 | height: dim, |
| 206 | transform: sus.translate(xlate,xlate) |
| 207 | }); |
| 208 | return g; |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Bri Prebilic Cole | 3d4d01c | 2015-04-30 13:48:36 -0700 | [diff] [blame] | 211 | function sortIcons() { |
Simon Hunt | 051e9fa | 2016-01-19 15:54:22 -0800 | [diff] [blame] | 212 | function _s(div, gid) { |
Bri Prebilic Cole | 1f93be2 | 2015-02-10 17:16:46 -0800 | [diff] [blame] | 213 | div.style('display', 'inline-block'); |
Simon Hunt | 051e9fa | 2016-01-19 15:54:22 -0800 | [diff] [blame] | 214 | loadEmbeddedIcon(div, gid, 10); |
Simon Hunt | 3695a62 | 2015-03-31 11:52:23 -0700 | [diff] [blame] | 215 | div.classed('tableColSort', true); |
Bri Prebilic Cole | 1f93be2 | 2015-02-10 17:16:46 -0800 | [diff] [blame] | 216 | } |
| 217 | |
Bri Prebilic Cole | 1f93be2 | 2015-02-10 17:16:46 -0800 | [diff] [blame] | 218 | return { |
Simon Hunt | 051e9fa | 2016-01-19 15:54:22 -0800 | [diff] [blame] | 219 | asc: function (div) { _s(div, 'upArrow'); }, |
| 220 | desc: function (div) { _s(div, 'downArrow'); }, |
| 221 | none: function (div) { div.remove(); } |
Bri Prebilic Cole | 1f93be2 | 2015-02-10 17:16:46 -0800 | [diff] [blame] | 222 | }; |
| 223 | } |
| 224 | |
Simon Hunt | 6f7668c | 2016-02-02 14:27:11 -0800 | [diff] [blame] | 225 | function registerIconMapping(iconId, glyphId) { |
| 226 | if (glyphMapping[iconId]) { |
| 227 | $log.warn('Icon with id', iconId, 'already mapped. Ignoring.'); |
| 228 | } else { |
Simon Hunt | 24d0c5c | 2016-03-03 00:05:08 -0800 | [diff] [blame] | 229 | // map icon-->glyph |
Simon Hunt | 6f7668c | 2016-02-02 14:27:11 -0800 | [diff] [blame] | 230 | glyphMapping[iconId] = glyphId; |
Simon Hunt | 24d0c5c | 2016-03-03 00:05:08 -0800 | [diff] [blame] | 231 | // make sure definition is installed |
| 232 | gs.loadDefs(ensureIconLibDefs(), [glyphId], true); |
Simon Hunt | 6f7668c | 2016-02-02 14:27:11 -0800 | [diff] [blame] | 233 | } |
| 234 | } |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 235 | |
| 236 | // ========================= |
| 237 | // === DEFINE THE MODULE |
| 238 | |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 239 | angular.module('onosSvg') |
Bri Prebilic Cole | 0c41ba2 | 2015-07-06 15:09:48 -0700 | [diff] [blame] | 240 | .directive('icon', ['IconService', function (is) { |
| 241 | return { |
| 242 | restrict: 'A', |
| 243 | link: function (scope, element, attrs) { |
| 244 | attrs.$observe('iconId', function () { |
| 245 | var div = d3.select(element[0]); |
| 246 | div.selectAll('*').remove(); |
| 247 | is.loadEmbeddedIcon(div, attrs.iconId, attrs.iconSize); |
| 248 | }); |
| 249 | } |
| 250 | }; |
| 251 | }]) |
| 252 | |
Simon Hunt | 9722538 | 2015-01-19 13:33:09 -0800 | [diff] [blame] | 253 | .factory('IconService', ['$log', 'FnService', 'GlyphService', |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 254 | 'SvgUtilService', |
| 255 | |
| 256 | function (_$log_, _fs_, _gs_, _sus_) { |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 257 | $log = _$log_; |
Simon Hunt | 7f172cc | 2015-01-16 17:43:00 -0800 | [diff] [blame] | 258 | fs = _fs_; |
Simon Hunt | 9722538 | 2015-01-19 13:33:09 -0800 | [diff] [blame] | 259 | gs = _gs_; |
Simon Hunt | 1894d79 | 2015-02-04 17:09:20 -0800 | [diff] [blame] | 260 | sus = _sus_; |
Simon Hunt | 7f172cc | 2015-01-16 17:43:00 -0800 | [diff] [blame] | 261 | |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 262 | return { |
Simon Hunt | 9722538 | 2015-01-19 13:33:09 -0800 | [diff] [blame] | 263 | loadIcon: loadIcon, |
Bri Prebilic Cole | 40be6b2 | 2015-02-19 17:12:23 -0800 | [diff] [blame] | 264 | loadIconByClass: loadIconByClass, |
Simon Hunt | ac4c6f7 | 2015-02-03 19:50:53 -0800 | [diff] [blame] | 265 | loadEmbeddedIcon: loadEmbeddedIcon, |
| 266 | addDeviceIcon: addDeviceIcon, |
| 267 | addHostIcon: addHostIcon, |
Bri Prebilic Cole | 1f93be2 | 2015-02-10 17:16:46 -0800 | [diff] [blame] | 268 | iconConfig: function () { return config; }, |
Simon Hunt | 6f7668c | 2016-02-02 14:27:11 -0800 | [diff] [blame] | 269 | sortIcons: sortIcons, |
| 270 | registerIconMapping: registerIconMapping |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 271 | }; |
| 272 | }]); |
| 273 | |
| 274 | }()); |