blob: 3a9092d94138555d2fa16a09cf4165d5cf71e34b [file] [log] [blame]
Simon Hunt7ac7be92015-01-06 10:47:56 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
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 Hunt1894d792015-02-04 17:09:20 -080023 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 Hunt986b92f2016-06-03 15:46:59 -070039
40 close: 'xClose',
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070041
Simon Hunt4e412732015-10-27 15:25:39 -070042 topo: 'topo',
43
Bri Prebilic Cole43f17c02015-05-01 10:43:38 -070044 refresh: 'refresh',
Bri Prebilic Colebd0bc772015-05-13 13:02:26 -070045 garbage: 'garbage',
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -070046
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070047 upArrow: 'triangleUp',
48 downArrow: 'triangleDown',
49
50 appInactive: 'unknown',
51
Bri Prebilic Coledea09742015-02-12 15:33:50 -080052 devIcon_SWITCH: 'switch',
Thomas Vachuska58eded62015-03-31 12:04:03 -070053 devIcon_ROADM: 'roadm',
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +020054 devIcon_OTN: 'otn',
Simon Hunt864333a2015-11-16 17:08:08 -080055 deviceTable: 'switch',
Bri Prebilic Cole9b1fb9a2015-07-01 13:57:11 -070056 flowTable: 'flowTable',
57 portTable: 'portTable',
58 groupTable: 'groupTable',
Jian Li1f544732015-12-30 23:36:37 -080059 meterTable: 'meterTable',
Thomas Vachuska58eded62015-03-31 12:04:03 -070060
61 hostIcon_endstation: 'endstation',
62 hostIcon_router: 'router',
Simon Hunt20e16792015-04-24 14:29:39 -070063 hostIcon_bgpSpeaker: 'bgpSpeaker',
64
Simon Hunt986b92f2016-06-03 15:46:59 -070065 // navigation menu icons...
Simon Hunt20e16792015-04-24 14:29:39 -070066 nav_apps: 'bird',
Thomas Vachuskaaa8b0eb2015-05-22 09:54:15 -070067 nav_settings: 'chain',
Simon Hunt20e16792015-04-24 14:29:39 -070068 nav_cluster: 'node',
Simon Hunt986b92f2016-06-03 15:46:59 -070069 nav_processors: 'allTraffic',
70
Bri Prebilic Cole6ed04eb2015-04-27 16:26:03 -070071 nav_topo: 'topo',
Simon Hunt20e16792015-04-24 14:29:39 -070072 nav_devs: 'switch',
73 nav_links: 'ports',
74 nav_hosts: 'endstation',
Thomas Vachuska3ece3732015-09-22 23:58:50 -070075 nav_intents: 'relatedIntents',
Simon Hunt986b92f2016-06-03 15:46:59 -070076 nav_tunnels: 'ports' // TODO: use tunnel glyph, when available
Simon Huntac4c6f72015-02-03 19:50:53 -080077 };
78
Simon Hunt97225382015-01-19 13:33:09 -080079 function ensureIconLibDefs() {
80 var body = d3.select('body'),
Simon Hunt24d0c5c2016-03-03 00:05:08 -080081 svg = body.select('svg#IconLibDefs');
Simon Hunt97225382015-01-19 13:33:09 -080082
83 if (svg.empty()) {
84 svg = body.append('svg').attr('id', 'IconLibDefs');
Simon Hunt24d0c5c2016-03-03 00:05:08 -080085 svg.append('defs');
Simon Hunt97225382015-01-19 13:33:09 -080086 }
87 return svg.select('defs');
88 }
89
Simon Huntac4c6f72015-02-03 19:50:53 -080090 // div is a D3 selection of the <DIV> element into which icon should load
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080091 // glyphId identifies the glyph to use
Simon Huntac4c6f72015-02-03 19:50:53 -080092 // size is dimension of icon in pixels. Defaults to 20.
93 // installGlyph, if truthy, will cause the glyph to be added to
94 // well-known defs element. Defaults to false.
95 // svgClass is the CSS class used to identify the SVG layer.
96 // Defaults to 'embeddedIcon'.
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080097 function loadIcon(div, glyphId, size, installGlyph, svgClass) {
Simon Huntac4c6f72015-02-03 19:50:53 -080098 var dim = size || 20,
99 svgCls = svgClass || 'embeddedIcon',
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800100 gid = glyphId || 'unknown',
Simon Huntac4c6f72015-02-03 19:50:53 -0800101 svg, g;
102
103 if (installGlyph) {
104 gs.loadDefs(ensureIconLibDefs(), [gid], true);
105 }
106
107 svg = div.append('svg').attr({
108 'class': svgCls,
109 width: dim,
110 height: dim,
111 viewBox: viewBox
112 });
113
114 g = svg.append('g').attr({
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800115 'class': 'icon'
Simon Huntac4c6f72015-02-03 19:50:53 -0800116 });
117
118 g.append('rect').attr({
119 width: vboxSize,
120 height: vboxSize,
121 rx: cornerSize
122 });
123
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800124 g.append('use').attr({
125 width: vboxSize,
126 height: vboxSize,
127 'class': 'glyph',
128 'xlink:href': '#' + gid
129 });
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800130 }
131
132 // div is a D3 selection of the <DIV> element into which icon should load
133 // iconCls is the CSS class used to identify the icon
134 // size is dimension of icon in pixels. Defaults to 20.
135 // installGlyph, if truthy, will cause the glyph to be added to
136 // well-known defs element. Defaults to false.
137 // svgClass is the CSS class used to identify the SVG layer.
138 // Defaults to 'embeddedIcon'.
139 function loadIconByClass(div, iconCls, size, installGlyph, svgClass) {
140 loadIcon(div, glyphMapping[iconCls], size, installGlyph, svgClass);
141 div.select('svg g').classed(iconCls, true);
142 }
Simon Huntac4c6f72015-02-03 19:50:53 -0800143
144 function loadEmbeddedIcon(div, iconCls, size) {
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800145 loadIconByClass(div, iconCls, size, true);
Simon Huntac4c6f72015-02-03 19:50:53 -0800146 }
Simon Hunta5487ad2016-06-16 13:10:41 -0700147
Simon Huntf44d7262016-06-14 14:46:56 -0700148 // Adds a device glyph to the specified element.
149 // Returns the D3 selection of the glyph (use) element.
150 function addDeviceIcon(elem, glyphId, iconDim) {
151 var gid = gs.glyphDefined(glyphId) ? glyphId : 'query';
152 return elem.append('use').attr({
Simon Hunt044f28072015-10-08 12:38:16 -0700153 'xlink:href': '#' + gid,
Simon Huntf44d7262016-06-14 14:46:56 -0700154 width: iconDim,
155 height: iconDim
Simon Huntac4c6f72015-02-03 19:50:53 -0800156 });
Simon Huntac4c6f72015-02-03 19:50:53 -0800157 }
158
Simon Hunt1894d792015-02-04 17:09:20 -0800159 function addHostIcon(elem, radius, glyphId) {
160 var dim = radius * 1.5,
161 xlate = -dim / 2,
162 g = elem.append('g')
163 .attr('class', 'svgIcon hostIcon');
164
165 g.append('circle').attr('r', radius);
166
167 g.append('use').attr({
168 'xlink:href': '#' + glyphId,
169 width: dim,
170 height: dim,
171 transform: sus.translate(xlate,xlate)
172 });
173 return g;
Simon Huntac4c6f72015-02-03 19:50:53 -0800174 }
175
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700176 function sortIcons() {
Simon Hunt051e9fa2016-01-19 15:54:22 -0800177 function _s(div, gid) {
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800178 div.style('display', 'inline-block');
Simon Hunt051e9fa2016-01-19 15:54:22 -0800179 loadEmbeddedIcon(div, gid, 10);
Simon Hunt3695a622015-03-31 11:52:23 -0700180 div.classed('tableColSort', true);
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800181 }
182
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800183 return {
Simon Hunt051e9fa2016-01-19 15:54:22 -0800184 asc: function (div) { _s(div, 'upArrow'); },
185 desc: function (div) { _s(div, 'downArrow'); },
186 none: function (div) { div.remove(); }
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800187 };
188 }
189
Simon Hunt6f7668c2016-02-02 14:27:11 -0800190 function registerIconMapping(iconId, glyphId) {
191 if (glyphMapping[iconId]) {
192 $log.warn('Icon with id', iconId, 'already mapped. Ignoring.');
193 } else {
Simon Hunt24d0c5c2016-03-03 00:05:08 -0800194 // map icon-->glyph
Simon Hunt6f7668c2016-02-02 14:27:11 -0800195 glyphMapping[iconId] = glyphId;
Simon Hunt24d0c5c2016-03-03 00:05:08 -0800196 // make sure definition is installed
197 gs.loadDefs(ensureIconLibDefs(), [glyphId], true);
Simon Hunt6f7668c2016-02-02 14:27:11 -0800198 }
199 }
Simon Huntac4c6f72015-02-03 19:50:53 -0800200
201 // =========================
202 // === DEFINE THE MODULE
203
Simon Hunt7ac7be92015-01-06 10:47:56 -0800204 angular.module('onosSvg')
Bri Prebilic Cole0c41ba22015-07-06 15:09:48 -0700205 .directive('icon', ['IconService', function (is) {
206 return {
207 restrict: 'A',
208 link: function (scope, element, attrs) {
209 attrs.$observe('iconId', function () {
210 var div = d3.select(element[0]);
211 div.selectAll('*').remove();
212 is.loadEmbeddedIcon(div, attrs.iconId, attrs.iconSize);
213 });
214 }
215 };
216 }])
217
Simon Hunt97225382015-01-19 13:33:09 -0800218 .factory('IconService', ['$log', 'FnService', 'GlyphService',
Simon Hunt1894d792015-02-04 17:09:20 -0800219 'SvgUtilService',
220
221 function (_$log_, _fs_, _gs_, _sus_) {
Simon Hunt7ac7be92015-01-06 10:47:56 -0800222 $log = _$log_;
Simon Hunt7f172cc2015-01-16 17:43:00 -0800223 fs = _fs_;
Simon Hunt97225382015-01-19 13:33:09 -0800224 gs = _gs_;
Simon Hunt1894d792015-02-04 17:09:20 -0800225 sus = _sus_;
Simon Hunt7f172cc2015-01-16 17:43:00 -0800226
Simon Hunt7ac7be92015-01-06 10:47:56 -0800227 return {
Simon Hunt97225382015-01-19 13:33:09 -0800228 loadIcon: loadIcon,
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800229 loadIconByClass: loadIconByClass,
Simon Huntac4c6f72015-02-03 19:50:53 -0800230 loadEmbeddedIcon: loadEmbeddedIcon,
231 addDeviceIcon: addDeviceIcon,
232 addHostIcon: addHostIcon,
Simon Hunt6f7668c2016-02-02 14:27:11 -0800233 sortIcons: sortIcons,
234 registerIconMapping: registerIconMapping
Simon Hunt7ac7be92015-01-06 10:47:56 -0800235 };
236 }]);
237
238}());