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