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