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