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