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