blob: f40a425f7fcd71b8db054bac0b84d344355a5c6b [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
46 appInactive: 'unknown',
47
Bri Prebilic Coledea09742015-02-12 15:33:50 -080048 devIcon_SWITCH: 'switch',
Thomas Vachuska58eded62015-03-31 12:04:03 -070049 devIcon_ROADM: 'roadm',
50
51 hostIcon_endstation: 'endstation',
52 hostIcon_router: 'router',
Simon Hunt20e16792015-04-24 14:29:39 -070053 hostIcon_bgpSpeaker: 'bgpSpeaker',
54
55 nav_apps: 'bird',
56 nav_cluster: 'node',
Bri Prebilic Cole6ed04eb2015-04-27 16:26:03 -070057 nav_topo: 'topo',
Simon Hunt20e16792015-04-24 14:29:39 -070058 nav_devs: 'switch',
59 nav_links: 'ports',
60 nav_hosts: 'endstation',
61 nav_intents: 'relatedIntents'
Simon Huntac4c6f72015-02-03 19:50:53 -080062 };
63
Simon Hunt97225382015-01-19 13:33:09 -080064 function ensureIconLibDefs() {
65 var body = d3.select('body'),
66 svg = body.select('svg#IconLibDefs'),
67 defs;
68
69 if (svg.empty()) {
70 svg = body.append('svg').attr('id', 'IconLibDefs');
71 defs = svg.append('defs');
72 }
73 return svg.select('defs');
74 }
75
Simon Huntac4c6f72015-02-03 19:50:53 -080076 // div is a D3 selection of the <DIV> element into which icon should load
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080077 // glyphId identifies the glyph to use
Simon Huntac4c6f72015-02-03 19:50:53 -080078 // size is dimension of icon in pixels. Defaults to 20.
79 // installGlyph, if truthy, will cause the glyph to be added to
80 // well-known defs element. Defaults to false.
81 // svgClass is the CSS class used to identify the SVG layer.
82 // Defaults to 'embeddedIcon'.
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080083 function loadIcon(div, glyphId, size, installGlyph, svgClass) {
Simon Huntac4c6f72015-02-03 19:50:53 -080084 var dim = size || 20,
85 svgCls = svgClass || 'embeddedIcon',
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080086 gid = glyphId || 'unknown',
Simon Huntac4c6f72015-02-03 19:50:53 -080087 svg, g;
88
89 if (installGlyph) {
90 gs.loadDefs(ensureIconLibDefs(), [gid], true);
91 }
92
93 svg = div.append('svg').attr({
94 'class': svgCls,
95 width: dim,
96 height: dim,
97 viewBox: viewBox
98 });
99
100 g = svg.append('g').attr({
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800101 'class': 'icon'
Simon Huntac4c6f72015-02-03 19:50:53 -0800102 });
103
104 g.append('rect').attr({
105 width: vboxSize,
106 height: vboxSize,
107 rx: cornerSize
108 });
109
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -0800110 g.append('use').attr({
111 width: vboxSize,
112 height: vboxSize,
113 'class': 'glyph',
114 'xlink:href': '#' + gid
115 });
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800116 }
117
118 // div is a D3 selection of the <DIV> element into which icon should load
119 // iconCls is the CSS class used to identify the icon
120 // size is dimension of icon in pixels. Defaults to 20.
121 // installGlyph, if truthy, will cause the glyph to be added to
122 // well-known defs element. Defaults to false.
123 // svgClass is the CSS class used to identify the SVG layer.
124 // Defaults to 'embeddedIcon'.
125 function loadIconByClass(div, iconCls, size, installGlyph, svgClass) {
126 loadIcon(div, glyphMapping[iconCls], size, installGlyph, svgClass);
127 div.select('svg g').classed(iconCls, true);
128 }
Simon Huntac4c6f72015-02-03 19:50:53 -0800129
130 function loadEmbeddedIcon(div, iconCls, size) {
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800131 loadIconByClass(div, iconCls, size, true);
Simon Huntac4c6f72015-02-03 19:50:53 -0800132 }
133
134
135 // configuration for device and host icons in the topology view
136 var config = {
137 device: {
138 dim: 36,
139 rx: 4
140 },
141 host: {
142 radius: {
143 noGlyph: 9,
144 withGlyph: 14
145 },
146 glyphed: {
147 endstation: 1,
148 bgpSpeaker: 1,
149 router: 1
150 }
151 }
152 };
153
154
155 // Adds a device icon to the specified element, using the given glyph.
156 // Returns the D3 selection of the icon.
157 function addDeviceIcon(elem, glyphId) {
158 var cfg = config.device,
159 g = elem.append('g')
160 .attr('class', 'svgIcon deviceIcon');
161
162 g.append('rect').attr({
163 x: 0,
164 y: 0,
165 rx: cfg.rx,
166 width: cfg.dim,
167 height: cfg.dim
168 });
169
170 g.append('use').attr({
171 'xlink:href': '#' + glyphId,
172 width: cfg.dim,
173 height: cfg.dim
174 });
175
176 g.dim = cfg.dim;
177 return g;
178 }
179
Simon Hunt1894d792015-02-04 17:09:20 -0800180 function addHostIcon(elem, radius, glyphId) {
181 var dim = radius * 1.5,
182 xlate = -dim / 2,
183 g = elem.append('g')
184 .attr('class', 'svgIcon hostIcon');
185
186 g.append('circle').attr('r', radius);
187
188 g.append('use').attr({
189 'xlink:href': '#' + glyphId,
190 width: dim,
191 height: dim,
192 transform: sus.translate(xlate,xlate)
193 });
194 return g;
Simon Huntac4c6f72015-02-03 19:50:53 -0800195 }
196
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700197 function sortIcons() {
Simon Hunt3695a622015-03-31 11:52:23 -0700198 function sortAsc(div) {
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800199 div.style('display', 'inline-block');
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700200 loadEmbeddedIcon(div, 'upArrow', 10);
Simon Hunt3695a622015-03-31 11:52:23 -0700201 div.classed('tableColSort', true);
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800202 }
203
204 function sortDesc(div) {
205 div.style('display', 'inline-block');
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700206 loadEmbeddedIcon(div, 'downArrow', 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 sortNone(div) {
211 div.remove();
212 }
213
214 return {
215 sortAsc: sortAsc,
216 sortDesc: sortDesc,
217 sortNone: sortNone
218 };
219 }
220
Simon Huntac4c6f72015-02-03 19:50:53 -0800221
222 // =========================
223 // === DEFINE THE MODULE
224
Simon Hunt7ac7be92015-01-06 10:47:56 -0800225 angular.module('onosSvg')
Simon Hunt97225382015-01-19 13:33:09 -0800226 .factory('IconService', ['$log', 'FnService', 'GlyphService',
Simon Hunt1894d792015-02-04 17:09:20 -0800227 'SvgUtilService',
228
229 function (_$log_, _fs_, _gs_, _sus_) {
Simon Hunt7ac7be92015-01-06 10:47:56 -0800230 $log = _$log_;
Simon Hunt7f172cc2015-01-16 17:43:00 -0800231 fs = _fs_;
Simon Hunt97225382015-01-19 13:33:09 -0800232 gs = _gs_;
Simon Hunt1894d792015-02-04 17:09:20 -0800233 sus = _sus_;
Simon Hunt7f172cc2015-01-16 17:43:00 -0800234
Simon Hunt7ac7be92015-01-06 10:47:56 -0800235 return {
Simon Hunt97225382015-01-19 13:33:09 -0800236 loadIcon: loadIcon,
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800237 loadIconByClass: loadIconByClass,
Simon Huntac4c6f72015-02-03 19:50:53 -0800238 loadEmbeddedIcon: loadEmbeddedIcon,
239 addDeviceIcon: addDeviceIcon,
240 addHostIcon: addHostIcon,
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800241 iconConfig: function () { return config; },
Bri Prebilic Cole3d4d01c2015-04-30 13:48:36 -0700242 sortIcons: sortIcons
Simon Hunt7ac7be92015-01-06 10:47:56 -0800243 };
244 }]);
245
246}());