blob: 12536361d3c467bb3b97739d079b7b262517e306 [file] [log] [blame]
Simon Hunt7ac7be92015-01-06 10:47:56 -08001/*
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 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',
39
Simon Hunt4e412732015-10-27 15:25:39 -070040 topo: 'topo',
41
Bri Prebilic Cole43f17c02015-05-01 10:43:38 -070042 refresh: 'refresh',
Bri Prebilic Colebd0bc772015-05-13 13:02:26 -070043 garbage: 'garbage',
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -070044
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070045 upArrow: 'triangleUp',
46 downArrow: 'triangleDown',
47
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -070048 loading: 'loading',
49
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070050 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',
Bri Prebilic Cole9b1fb9a2015-07-01 13:57:11 -070054 flowTable: 'flowTable',
55 portTable: 'portTable',
56 groupTable: 'groupTable',
Thomas Vachuska58eded62015-03-31 12:04:03 -070057
58 hostIcon_endstation: 'endstation',
59 hostIcon_router: 'router',
Simon Hunt20e16792015-04-24 14:29:39 -070060 hostIcon_bgpSpeaker: 'bgpSpeaker',
61
62 nav_apps: 'bird',
Thomas Vachuskaaa8b0eb2015-05-22 09:54:15 -070063 nav_settings: 'chain',
Simon Hunt20e16792015-04-24 14:29:39 -070064 nav_cluster: 'node',
Bri Prebilic Cole6ed04eb2015-04-27 16:26:03 -070065 nav_topo: 'topo',
Simon Hunt20e16792015-04-24 14:29:39 -070066 nav_devs: 'switch',
67 nav_links: 'ports',
68 nav_hosts: 'endstation',
Thomas Vachuska3ece3732015-09-22 23:58:50 -070069 nav_intents: 'relatedIntents',
70 nav_processors: 'allTraffic'
Simon Huntac4c6f72015-02-03 19:50:53 -080071 };
72
Simon Hunt97225382015-01-19 13:33:09 -080073 function ensureIconLibDefs() {
74 var body = d3.select('body'),
75 svg = body.select('svg#IconLibDefs'),
76 defs;
77
78 if (svg.empty()) {
79 svg = body.append('svg').attr('id', 'IconLibDefs');
80 defs = svg.append('defs');
81 }
82 return svg.select('defs');
83 }
84
Simon Huntac4c6f72015-02-03 19:50:53 -080085 // div is a D3 selection of the <DIV> element into which icon should load
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080086 // glyphId identifies the glyph to use
Simon Huntac4c6f72015-02-03 19:50:53 -080087 // size is dimension of icon in pixels. Defaults to 20.
88 // installGlyph, if truthy, will cause the glyph to be added to
89 // well-known defs element. Defaults to false.
90 // svgClass is the CSS class used to identify the SVG layer.
91 // Defaults to 'embeddedIcon'.
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080092 function loadIcon(div, glyphId, size, installGlyph, svgClass) {
Simon Huntac4c6f72015-02-03 19:50:53 -080093 var dim = size || 20,
94 svgCls = svgClass || 'embeddedIcon',
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080095 gid = glyphId || 'unknown',
Simon Huntac4c6f72015-02-03 19:50:53 -080096 svg, g;
97
98 if (installGlyph) {
99 gs.loadDefs(ensureIconLibDefs(), [gid], true);
100 }
101
102 svg = div.append('svg').attr({
103 'class': svgCls,
104 width: dim,
105 height: dim,
106 viewBox: viewBox
107 });
108
109 g = svg.append('g').attr({
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800110 'class': 'icon'
Simon Huntac4c6f72015-02-03 19:50:53 -0800111 });
112
113 g.append('rect').attr({
114 width: vboxSize,
115 height: vboxSize,
116 rx: cornerSize
117 });
118
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800119 g.append('use').attr({
120 width: vboxSize,
121 height: vboxSize,
122 'class': 'glyph',
123 'xlink:href': '#' + gid
124 });
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800125 }
126
127 // div is a D3 selection of the <DIV> element into which icon should load
128 // iconCls is the CSS class used to identify the icon
129 // size is dimension of icon in pixels. Defaults to 20.
130 // installGlyph, if truthy, will cause the glyph to be added to
131 // well-known defs element. Defaults to false.
132 // svgClass is the CSS class used to identify the SVG layer.
133 // Defaults to 'embeddedIcon'.
134 function loadIconByClass(div, iconCls, size, installGlyph, svgClass) {
135 loadIcon(div, glyphMapping[iconCls], size, installGlyph, svgClass);
136 div.select('svg g').classed(iconCls, true);
137 }
Simon Huntac4c6f72015-02-03 19:50:53 -0800138
139 function loadEmbeddedIcon(div, iconCls, size) {
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800140 loadIconByClass(div, iconCls, size, true);
Simon Huntac4c6f72015-02-03 19:50:53 -0800141 }
142
143
144 // configuration for device and host icons in the topology view
145 var config = {
146 device: {
147 dim: 36,
148 rx: 4
149 },
150 host: {
151 radius: {
152 noGlyph: 9,
153 withGlyph: 14
154 },
155 glyphed: {
156 endstation: 1,
157 bgpSpeaker: 1,
158 router: 1
159 }
160 }
161 };
162
163
164 // Adds a device icon to the specified element, using the given glyph.
165 // Returns the D3 selection of the icon.
166 function addDeviceIcon(elem, glyphId) {
167 var cfg = config.device,
Simon Hunt044f28072015-10-08 12:38:16 -0700168 gid = gs.glyphDefined(glyphId) ? glyphId : 'query',
Simon Huntac4c6f72015-02-03 19:50:53 -0800169 g = elem.append('g')
170 .attr('class', 'svgIcon deviceIcon');
171
172 g.append('rect').attr({
173 x: 0,
174 y: 0,
175 rx: cfg.rx,
176 width: cfg.dim,
177 height: cfg.dim
178 });
179
180 g.append('use').attr({
Simon Hunt044f28072015-10-08 12:38:16 -0700181 'xlink:href': '#' + gid,
Simon Huntac4c6f72015-02-03 19:50:53 -0800182 width: cfg.dim,
183 height: cfg.dim
184 });
185
186 g.dim = cfg.dim;
187 return g;
188 }
189
Simon Hunt1894d792015-02-04 17:09:20 -0800190 function addHostIcon(elem, radius, glyphId) {
191 var dim = radius * 1.5,
192 xlate = -dim / 2,
193 g = elem.append('g')
194 .attr('class', 'svgIcon hostIcon');
195
196 g.append('circle').attr('r', radius);
197
198 g.append('use').attr({
199 'xlink:href': '#' + glyphId,
200 width: dim,
201 height: dim,
202 transform: sus.translate(xlate,xlate)
203 });
204 return g;
Simon Huntac4c6f72015-02-03 19:50:53 -0800205 }
206
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700207 function sortIcons() {
Simon Hunt3695a622015-03-31 11:52:23 -0700208 function sortAsc(div) {
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800209 div.style('display', 'inline-block');
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700210 loadEmbeddedIcon(div, 'upArrow', 10);
Simon Hunt3695a622015-03-31 11:52:23 -0700211 div.classed('tableColSort', true);
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800212 }
213
214 function sortDesc(div) {
215 div.style('display', 'inline-block');
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700216 loadEmbeddedIcon(div, 'downArrow', 10);
Simon Hunt3695a622015-03-31 11:52:23 -0700217 div.classed('tableColSort', true);
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800218 }
219
220 function sortNone(div) {
221 div.remove();
222 }
223
224 return {
225 sortAsc: sortAsc,
226 sortDesc: sortDesc,
227 sortNone: sortNone
228 };
229 }
230
Simon Huntac4c6f72015-02-03 19:50:53 -0800231
232 // =========================
233 // === DEFINE THE MODULE
234
Simon Hunt7ac7be92015-01-06 10:47:56 -0800235 angular.module('onosSvg')
Bri Prebilic Cole0c41ba22015-07-06 15:09:48 -0700236 .directive('icon', ['IconService', function (is) {
237 return {
238 restrict: 'A',
239 link: function (scope, element, attrs) {
240 attrs.$observe('iconId', function () {
241 var div = d3.select(element[0]);
242 div.selectAll('*').remove();
243 is.loadEmbeddedIcon(div, attrs.iconId, attrs.iconSize);
244 });
245 }
246 };
247 }])
248
Simon Hunt97225382015-01-19 13:33:09 -0800249 .factory('IconService', ['$log', 'FnService', 'GlyphService',
Simon Hunt1894d792015-02-04 17:09:20 -0800250 'SvgUtilService',
251
252 function (_$log_, _fs_, _gs_, _sus_) {
Simon Hunt7ac7be92015-01-06 10:47:56 -0800253 $log = _$log_;
Simon Hunt7f172cc2015-01-16 17:43:00 -0800254 fs = _fs_;
Simon Hunt97225382015-01-19 13:33:09 -0800255 gs = _gs_;
Simon Hunt1894d792015-02-04 17:09:20 -0800256 sus = _sus_;
Simon Hunt7f172cc2015-01-16 17:43:00 -0800257
Simon Hunt7ac7be92015-01-06 10:47:56 -0800258 return {
Simon Hunt97225382015-01-19 13:33:09 -0800259 loadIcon: loadIcon,
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800260 loadIconByClass: loadIconByClass,
Simon Huntac4c6f72015-02-03 19:50:53 -0800261 loadEmbeddedIcon: loadEmbeddedIcon,
262 addDeviceIcon: addDeviceIcon,
263 addHostIcon: addHostIcon,
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800264 iconConfig: function () { return config; },
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700265 sortIcons: sortIcons
Simon Hunt7ac7be92015-01-06 10:47:56 -0800266 };
267 }]);
268
269}());