blob: ba79431315bfc10eb0a2fe8396fed03f47b05490 [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
Bri Prebilic Cole43f17c02015-05-01 10:43:38 -070040 refresh: 'refresh',
Bri Prebilic Colebd0bc772015-05-13 13:02:26 -070041 garbage: 'garbage',
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -070042
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070043 upArrow: 'triangleUp',
44 downArrow: 'triangleDown',
45
Bri Prebilic Cole6e1b4a52015-08-03 17:10:44 -070046 loading: 'loading',
47
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070048 appInactive: 'unknown',
49
Bri Prebilic Coledea09742015-02-12 15:33:50 -080050 devIcon_SWITCH: 'switch',
Thomas Vachuska58eded62015-03-31 12:04:03 -070051 devIcon_ROADM: 'roadm',
Bri Prebilic Cole9b1fb9a2015-07-01 13:57:11 -070052 flowTable: 'flowTable',
53 portTable: 'portTable',
54 groupTable: 'groupTable',
Thomas Vachuska58eded62015-03-31 12:04:03 -070055
56 hostIcon_endstation: 'endstation',
57 hostIcon_router: 'router',
Simon Hunt20e16792015-04-24 14:29:39 -070058 hostIcon_bgpSpeaker: 'bgpSpeaker',
59
60 nav_apps: 'bird',
Thomas Vachuskaaa8b0eb2015-05-22 09:54:15 -070061 nav_settings: 'chain',
Simon Hunt20e16792015-04-24 14:29:39 -070062 nav_cluster: 'node',
Bri Prebilic Cole6ed04eb2015-04-27 16:26:03 -070063 nav_topo: 'topo',
Simon Hunt20e16792015-04-24 14:29:39 -070064 nav_devs: 'switch',
65 nav_links: 'ports',
66 nav_hosts: 'endstation',
67 nav_intents: 'relatedIntents'
Simon Huntac4c6f72015-02-03 19:50:53 -080068 };
69
Simon Hunt97225382015-01-19 13:33:09 -080070 function ensureIconLibDefs() {
71 var body = d3.select('body'),
72 svg = body.select('svg#IconLibDefs'),
73 defs;
74
75 if (svg.empty()) {
76 svg = body.append('svg').attr('id', 'IconLibDefs');
77 defs = svg.append('defs');
78 }
79 return svg.select('defs');
80 }
81
Simon Huntac4c6f72015-02-03 19:50:53 -080082 // div is a D3 selection of the <DIV> element into which icon should load
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080083 // glyphId identifies the glyph to use
Simon Huntac4c6f72015-02-03 19:50:53 -080084 // size is dimension of icon in pixels. Defaults to 20.
85 // installGlyph, if truthy, will cause the glyph to be added to
86 // well-known defs element. Defaults to false.
87 // svgClass is the CSS class used to identify the SVG layer.
88 // Defaults to 'embeddedIcon'.
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080089 function loadIcon(div, glyphId, size, installGlyph, svgClass) {
Simon Huntac4c6f72015-02-03 19:50:53 -080090 var dim = size || 20,
91 svgCls = svgClass || 'embeddedIcon',
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080092 gid = glyphId || 'unknown',
Simon Huntac4c6f72015-02-03 19:50:53 -080093 svg, g;
94
95 if (installGlyph) {
96 gs.loadDefs(ensureIconLibDefs(), [gid], true);
97 }
98
99 svg = div.append('svg').attr({
100 'class': svgCls,
101 width: dim,
102 height: dim,
103 viewBox: viewBox
104 });
105
106 g = svg.append('g').attr({
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800107 'class': 'icon'
Simon Huntac4c6f72015-02-03 19:50:53 -0800108 });
109
110 g.append('rect').attr({
111 width: vboxSize,
112 height: vboxSize,
113 rx: cornerSize
114 });
115
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800116 g.append('use').attr({
117 width: vboxSize,
118 height: vboxSize,
119 'class': 'glyph',
120 'xlink:href': '#' + gid
121 });
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800122 }
123
124 // div is a D3 selection of the <DIV> element into which icon should load
125 // iconCls is the CSS class used to identify the icon
126 // size is dimension of icon in pixels. Defaults to 20.
127 // installGlyph, if truthy, will cause the glyph to be added to
128 // well-known defs element. Defaults to false.
129 // svgClass is the CSS class used to identify the SVG layer.
130 // Defaults to 'embeddedIcon'.
131 function loadIconByClass(div, iconCls, size, installGlyph, svgClass) {
132 loadIcon(div, glyphMapping[iconCls], size, installGlyph, svgClass);
133 div.select('svg g').classed(iconCls, true);
134 }
Simon Huntac4c6f72015-02-03 19:50:53 -0800135
136 function loadEmbeddedIcon(div, iconCls, size) {
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800137 loadIconByClass(div, iconCls, size, true);
Simon Huntac4c6f72015-02-03 19:50:53 -0800138 }
139
140
141 // configuration for device and host icons in the topology view
142 var config = {
143 device: {
144 dim: 36,
145 rx: 4
146 },
147 host: {
148 radius: {
149 noGlyph: 9,
150 withGlyph: 14
151 },
152 glyphed: {
153 endstation: 1,
154 bgpSpeaker: 1,
155 router: 1
156 }
157 }
158 };
159
160
161 // Adds a device icon to the specified element, using the given glyph.
162 // Returns the D3 selection of the icon.
163 function addDeviceIcon(elem, glyphId) {
164 var cfg = config.device,
165 g = elem.append('g')
166 .attr('class', 'svgIcon deviceIcon');
167
168 g.append('rect').attr({
169 x: 0,
170 y: 0,
171 rx: cfg.rx,
172 width: cfg.dim,
173 height: cfg.dim
174 });
175
176 g.append('use').attr({
177 'xlink:href': '#' + glyphId,
178 width: cfg.dim,
179 height: cfg.dim
180 });
181
182 g.dim = cfg.dim;
183 return g;
184 }
185
Simon Hunt1894d792015-02-04 17:09:20 -0800186 function addHostIcon(elem, radius, glyphId) {
187 var dim = radius * 1.5,
188 xlate = -dim / 2,
189 g = elem.append('g')
190 .attr('class', 'svgIcon hostIcon');
191
192 g.append('circle').attr('r', radius);
193
194 g.append('use').attr({
195 'xlink:href': '#' + glyphId,
196 width: dim,
197 height: dim,
198 transform: sus.translate(xlate,xlate)
199 });
200 return g;
Simon Huntac4c6f72015-02-03 19:50:53 -0800201 }
202
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700203 function sortIcons() {
Simon Hunt3695a622015-03-31 11:52:23 -0700204 function sortAsc(div) {
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800205 div.style('display', 'inline-block');
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700206 loadEmbeddedIcon(div, 'upArrow', 10);
Simon Hunt3695a622015-03-31 11:52:23 -0700207 div.classed('tableColSort', true);
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800208 }
209
210 function sortDesc(div) {
211 div.style('display', 'inline-block');
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700212 loadEmbeddedIcon(div, 'downArrow', 10);
Simon Hunt3695a622015-03-31 11:52:23 -0700213 div.classed('tableColSort', true);
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800214 }
215
216 function sortNone(div) {
217 div.remove();
218 }
219
220 return {
221 sortAsc: sortAsc,
222 sortDesc: sortDesc,
223 sortNone: sortNone
224 };
225 }
226
Simon Huntac4c6f72015-02-03 19:50:53 -0800227
228 // =========================
229 // === DEFINE THE MODULE
230
Simon Hunt7ac7be92015-01-06 10:47:56 -0800231 angular.module('onosSvg')
Bri Prebilic Cole0c41ba22015-07-06 15:09:48 -0700232 .directive('icon', ['IconService', function (is) {
233 return {
234 restrict: 'A',
235 link: function (scope, element, attrs) {
236 attrs.$observe('iconId', function () {
237 var div = d3.select(element[0]);
238 div.selectAll('*').remove();
239 is.loadEmbeddedIcon(div, attrs.iconId, attrs.iconSize);
240 });
241 }
242 };
243 }])
244
Simon Hunt97225382015-01-19 13:33:09 -0800245 .factory('IconService', ['$log', 'FnService', 'GlyphService',
Simon Hunt1894d792015-02-04 17:09:20 -0800246 'SvgUtilService',
247
248 function (_$log_, _fs_, _gs_, _sus_) {
Simon Hunt7ac7be92015-01-06 10:47:56 -0800249 $log = _$log_;
Simon Hunt7f172cc2015-01-16 17:43:00 -0800250 fs = _fs_;
Simon Hunt97225382015-01-19 13:33:09 -0800251 gs = _gs_;
Simon Hunt1894d792015-02-04 17:09:20 -0800252 sus = _sus_;
Simon Hunt7f172cc2015-01-16 17:43:00 -0800253
Simon Hunt7ac7be92015-01-06 10:47:56 -0800254 return {
Simon Hunt97225382015-01-19 13:33:09 -0800255 loadIcon: loadIcon,
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800256 loadIconByClass: loadIconByClass,
Simon Huntac4c6f72015-02-03 19:50:53 -0800257 loadEmbeddedIcon: loadEmbeddedIcon,
258 addDeviceIcon: addDeviceIcon,
259 addHostIcon: addHostIcon,
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800260 iconConfig: function () { return config; },
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700261 sortIcons: sortIcons
Simon Hunt7ac7be92015-01-06 10:47:56 -0800262 };
263 }]);
264
265}());