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