blob: 67a557b3591d9fb281c5fd1563514c8bb12b7632 [file] [log] [blame]
Simon Hunt7ac7be92015-01-06 10:47:56 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Simon Hunt7ac7be92015-01-06 10:47:56 -08003 *
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 Hunt7ac7be92015-01-06 10:47:56 -080019 */
20(function () {
21 'use strict';
22
Simon Hunt5c3ed732017-07-20 19:03:28 +000023 var $log, fs, gs, sus;
Simon Hunt7f172cc2015-01-16 17:43:00 -080024
Simon Hunt97225382015-01-19 13:33:09 -080025 var vboxSize = 50,
26 cornerSize = vboxSize / 10,
27 viewBox = '0 0 ' + vboxSize + ' ' + vboxSize;
Simon Hunt7f172cc2015-01-16 17:43:00 -080028
Simon Huntac4c6f72015-02-03 19:50:53 -080029 // Maps icon ID to the glyph ID it uses.
30 // NOTE: icon ID maps to a CSS class for styling that icon
Simon Hunt7f172cc2015-01-16 17:43:00 -080031 var glyphMapping = {
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070032 active: 'checkMark',
33 inactive: 'xMark',
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070034
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070035 plus: 'plus',
36 minus: 'minus',
37 play: 'play',
38 stop: 'stop',
Simon Huntca5e8872017-08-01 13:33:51 -070039
40 delta: 'delta',
41 nonzero: 'nonzero',
42
Simon Hunt986b92f2016-06-03 15:46:59 -070043 close: 'xClose',
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070044
Simon Hunt4e412732015-10-27 15:25:39 -070045 topo: 'topo',
46
Bri Prebilic Cole43f17c02015-05-01 10:43:38 -070047 refresh: 'refresh',
chengfan386620e2016-11-09 17:02:40 +080048 query: 'query',
Bri Prebilic Colebd0bc772015-05-13 13:02:26 -070049 garbage: 'garbage',
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -070050
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070051 upArrow: 'triangleUp',
52 downArrow: 'triangleDown',
53
54 appInactive: 'unknown',
55
Simon Hunt6b5a9412016-10-26 19:32:04 -070056 node: 'node',
Bri Prebilic Coledea09742015-02-12 15:33:50 -080057 devIcon_SWITCH: 'switch',
Thomas Vachuska58eded62015-03-31 12:04:03 -070058 devIcon_ROADM: 'roadm',
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +020059 devIcon_OTN: 'otn',
Simon Hunt864333a2015-11-16 17:08:08 -080060 deviceTable: 'switch',
Bri Prebilic Cole9b1fb9a2015-07-01 13:57:11 -070061 flowTable: 'flowTable',
62 portTable: 'portTable',
63 groupTable: 'groupTable',
Jian Li1f544732015-12-30 23:36:37 -080064 meterTable: 'meterTable',
Thomas Vachuska58eded62015-03-31 12:04:03 -070065
66 hostIcon_endstation: 'endstation',
67 hostIcon_router: 'router',
Simon Hunt20e16792015-04-24 14:29:39 -070068 hostIcon_bgpSpeaker: 'bgpSpeaker',
69
Simon Hunt986b92f2016-06-03 15:46:59 -070070 // navigation menu icons...
Simon Hunt20e16792015-04-24 14:29:39 -070071 nav_apps: 'bird',
Simon Hunta758f6d2016-06-23 13:59:33 -070072 nav_settings: 'cog',
Simon Hunt20e16792015-04-24 14:29:39 -070073 nav_cluster: 'node',
Simon Hunt986b92f2016-06-03 15:46:59 -070074 nav_processors: 'allTraffic',
75
Bri Prebilic Cole6ed04eb2015-04-27 16:26:03 -070076 nav_topo: 'topo',
Simon Hunt47f8fb72017-04-11 13:49:31 -070077 nav_topo2: 'm_cloud',
Simon Hunt20e16792015-04-24 14:29:39 -070078 nav_devs: 'switch',
79 nav_links: 'ports',
80 nav_hosts: 'endstation',
Thomas Vachuska3ece3732015-09-22 23:58:50 -070081 nav_intents: 'relatedIntents',
Simon Hunt5c3ed732017-07-20 19:03:28 +000082 nav_tunnels: 'ports', // TODO: use tunnel glyph, when available
83 nav_yang: 'yang'
Simon Huntac4c6f72015-02-03 19:50:53 -080084 };
85
Simon Hunt97225382015-01-19 13:33:09 -080086 function ensureIconLibDefs() {
87 var body = d3.select('body'),
Simon Hunt24d0c5c2016-03-03 00:05:08 -080088 svg = body.select('svg#IconLibDefs');
Simon Hunt97225382015-01-19 13:33:09 -080089
90 if (svg.empty()) {
91 svg = body.append('svg').attr('id', 'IconLibDefs');
Simon Hunt24d0c5c2016-03-03 00:05:08 -080092 svg.append('defs');
Simon Hunt97225382015-01-19 13:33:09 -080093 }
94 return svg.select('defs');
95 }
96
Simon Huntac4c6f72015-02-03 19:50:53 -080097 // div is a D3 selection of the <DIV> element into which icon should load
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080098 // glyphId identifies the glyph to use
Simon Huntac4c6f72015-02-03 19:50:53 -080099 // size is dimension of icon in pixels. Defaults to 20.
100 // installGlyph, if truthy, will cause the glyph to be added to
101 // well-known defs element. Defaults to false.
102 // svgClass is the CSS class used to identify the SVG layer.
103 // Defaults to 'embeddedIcon'.
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800104 function loadIcon(div, glyphId, size, installGlyph, svgClass) {
Simon Huntac4c6f72015-02-03 19:50:53 -0800105 var dim = size || 20,
106 svgCls = svgClass || 'embeddedIcon',
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800107 gid = glyphId || 'unknown',
Simon Huntac4c6f72015-02-03 19:50:53 -0800108 svg, g;
109
110 if (installGlyph) {
111 gs.loadDefs(ensureIconLibDefs(), [gid], true);
112 }
113
114 svg = div.append('svg').attr({
115 'class': svgCls,
116 width: dim,
117 height: dim,
Simon Hunt5c3ed732017-07-20 19:03:28 +0000118 viewBox: viewBox
Simon Huntac4c6f72015-02-03 19:50:53 -0800119 });
120
121 g = svg.append('g').attr({
Simon Hunt5c3ed732017-07-20 19:03:28 +0000122 'class': 'icon'
Simon Huntac4c6f72015-02-03 19:50:53 -0800123 });
124
125 g.append('rect').attr({
126 width: vboxSize,
127 height: vboxSize,
Simon Hunt5c3ed732017-07-20 19:03:28 +0000128 rx: cornerSize
Simon Huntac4c6f72015-02-03 19:50:53 -0800129 });
130
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800131 g.append('use').attr({
132 width: vboxSize,
133 height: vboxSize,
134 'class': 'glyph',
Simon Hunt5c3ed732017-07-20 19:03:28 +0000135 'xlink:href': '#' + gid
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800136 });
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800137 }
138
139 // div is a D3 selection of the <DIV> element into which icon should load
140 // iconCls is the CSS class used to identify the icon
141 // size is dimension of icon in pixels. Defaults to 20.
142 // installGlyph, if truthy, will cause the glyph to be added to
143 // well-known defs element. Defaults to false.
144 // svgClass is the CSS class used to identify the SVG layer.
145 // Defaults to 'embeddedIcon'.
146 function loadIconByClass(div, iconCls, size, installGlyph, svgClass) {
147 loadIcon(div, glyphMapping[iconCls], size, installGlyph, svgClass);
148 div.select('svg g').classed(iconCls, true);
149 }
Simon Huntac4c6f72015-02-03 19:50:53 -0800150
151 function loadEmbeddedIcon(div, iconCls, size) {
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800152 loadIconByClass(div, iconCls, size, true);
Simon Huntac4c6f72015-02-03 19:50:53 -0800153 }
Simon Huntca5e8872017-08-01 13:33:51 -0700154
Simon Huntf44d7262016-06-14 14:46:56 -0700155 // Adds a device glyph to the specified element.
156 // Returns the D3 selection of the glyph (use) element.
157 function addDeviceIcon(elem, glyphId, iconDim) {
158 var gid = gs.glyphDefined(glyphId) ? glyphId : 'query';
159 return elem.append('use').attr({
Simon Hunt044f28072015-10-08 12:38:16 -0700160 'xlink:href': '#' + gid,
Simon Huntf44d7262016-06-14 14:46:56 -0700161 width: iconDim,
Simon Hunt5c3ed732017-07-20 19:03:28 +0000162 height: iconDim
Simon Huntac4c6f72015-02-03 19:50:53 -0800163 });
Simon Huntac4c6f72015-02-03 19:50:53 -0800164 }
165
Simon Hunt1894d792015-02-04 17:09:20 -0800166 function addHostIcon(elem, radius, glyphId) {
167 var dim = radius * 1.5,
168 xlate = -dim / 2,
169 g = elem.append('g')
170 .attr('class', 'svgIcon hostIcon');
171
172 g.append('circle').attr('r', radius);
173
174 g.append('use').attr({
175 'xlink:href': '#' + glyphId,
176 width: dim,
177 height: dim,
Simon Hunt5c3ed732017-07-20 19:03:28 +0000178 transform: sus.translate(xlate,xlate)
Simon Hunt1894d792015-02-04 17:09:20 -0800179 });
180 return g;
Simon Huntac4c6f72015-02-03 19:50:53 -0800181 }
182
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700183 function sortIcons() {
Simon Hunt051e9fa2016-01-19 15:54:22 -0800184 function _s(div, gid) {
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800185 div.style('display', 'inline-block');
Simon Hunt051e9fa2016-01-19 15:54:22 -0800186 loadEmbeddedIcon(div, gid, 10);
Simon Hunt3695a622015-03-31 11:52:23 -0700187 div.classed('tableColSort', true);
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800188 }
189
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800190 return {
Simon Hunt051e9fa2016-01-19 15:54:22 -0800191 asc: function (div) { _s(div, 'upArrow'); },
192 desc: function (div) { _s(div, 'downArrow'); },
Simon Hunt5c3ed732017-07-20 19:03:28 +0000193 none: function (div) { div.remove(); }
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800194 };
195 }
196
Simon Hunt6f7668c2016-02-02 14:27:11 -0800197 function registerIconMapping(iconId, glyphId) {
198 if (glyphMapping[iconId]) {
199 $log.warn('Icon with id', iconId, 'already mapped. Ignoring.');
200 } else {
Simon Hunt24d0c5c2016-03-03 00:05:08 -0800201 // map icon-->glyph
Simon Hunt6f7668c2016-02-02 14:27:11 -0800202 glyphMapping[iconId] = glyphId;
Simon Hunt24d0c5c2016-03-03 00:05:08 -0800203 // make sure definition is installed
204 gs.loadDefs(ensureIconLibDefs(), [glyphId], true);
Simon Hunt6f7668c2016-02-02 14:27:11 -0800205 }
206 }
Simon Huntac4c6f72015-02-03 19:50:53 -0800207
208 // =========================
209 // === DEFINE THE MODULE
210
Simon Hunt7ac7be92015-01-06 10:47:56 -0800211 angular.module('onosSvg')
Bri Prebilic Cole0c41ba22015-07-06 15:09:48 -0700212 .directive('icon', ['IconService', function (is) {
213 return {
214 restrict: 'A',
215 link: function (scope, element, attrs) {
216 attrs.$observe('iconId', function () {
217 var div = d3.select(element[0]);
218 div.selectAll('*').remove();
219 is.loadEmbeddedIcon(div, attrs.iconId, attrs.iconSize);
220 });
Simon Hunt5c3ed732017-07-20 19:03:28 +0000221 }
Bri Prebilic Cole0c41ba22015-07-06 15:09:48 -0700222 };
223 }])
224
Simon Hunt5c3ed732017-07-20 19:03:28 +0000225 .factory('IconService', ['$log', 'FnService', 'GlyphService',
Simon Hunt1894d792015-02-04 17:09:20 -0800226 'SvgUtilService',
227
Simon Hunt5c3ed732017-07-20 19:03:28 +0000228 function (_$log_, _fs_, _gs_, _sus_) {
Simon Hunt7ac7be92015-01-06 10:47:56 -0800229 $log = _$log_;
Simon Hunt5c3ed732017-07-20 19:03:28 +0000230 fs = _fs_;
Simon Hunt97225382015-01-19 13:33:09 -0800231 gs = _gs_;
Simon Hunt1894d792015-02-04 17:09:20 -0800232 sus = _sus_;
Simon Hunt7f172cc2015-01-16 17:43:00 -0800233
Simon Hunt7ac7be92015-01-06 10:47:56 -0800234 return {
Simon Hunt97225382015-01-19 13:33:09 -0800235 loadIcon: loadIcon,
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800236 loadIconByClass: loadIconByClass,
Simon Huntac4c6f72015-02-03 19:50:53 -0800237 loadEmbeddedIcon: loadEmbeddedIcon,
238 addDeviceIcon: addDeviceIcon,
239 addHostIcon: addHostIcon,
Simon Hunt6f7668c2016-02-02 14:27:11 -0800240 sortIcons: sortIcons,
Simon Hunt5c3ed732017-07-20 19:03:28 +0000241 registerIconMapping: registerIconMapping
Simon Hunt7ac7be92015-01-06 10:47:56 -0800242 };
243 }]);
244
245}());