blob: 8a8b35f3c547440638fd521bac665b84ac7ae34a [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 = {
Thomas Vachuska0fa583c2015-03-30 23:07:41 -070032 appActive: 'checkMark',
33 appInactive: 'unknown',
34
Simon Huntac4c6f72015-02-03 19:50:53 -080035 deviceOnline: 'checkMark',
36 deviceOffline: 'xMark',
Bri Prebilic Coledea09742015-02-12 15:33:50 -080037 devIcon_SWITCH: 'switch',
38
Simon Huntac4c6f72015-02-03 19:50:53 -080039 tableColSortAsc: 'triangleUp',
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080040 tableColSortDesc: 'triangleDown'
Simon Huntac4c6f72015-02-03 19:50:53 -080041 };
42
43
Simon Hunt7ac7be92015-01-06 10:47:56 -080044
Simon Hunt97225382015-01-19 13:33:09 -080045 function ensureIconLibDefs() {
46 var body = d3.select('body'),
47 svg = body.select('svg#IconLibDefs'),
48 defs;
49
50 if (svg.empty()) {
51 svg = body.append('svg').attr('id', 'IconLibDefs');
52 defs = svg.append('defs');
53 }
54 return svg.select('defs');
55 }
56
Simon Huntac4c6f72015-02-03 19:50:53 -080057 // div is a D3 selection of the <DIV> element into which icon should load
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080058 // glyphId identifies the glyph to use
Simon Huntac4c6f72015-02-03 19:50:53 -080059 // size is dimension of icon in pixels. Defaults to 20.
60 // installGlyph, if truthy, will cause the glyph to be added to
61 // well-known defs element. Defaults to false.
62 // svgClass is the CSS class used to identify the SVG layer.
63 // Defaults to 'embeddedIcon'.
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080064 function loadIcon(div, glyphId, size, installGlyph, svgClass) {
Simon Huntac4c6f72015-02-03 19:50:53 -080065 var dim = size || 20,
66 svgCls = svgClass || 'embeddedIcon',
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080067 gid = glyphId || 'unknown',
Simon Huntac4c6f72015-02-03 19:50:53 -080068 svg, g;
69
70 if (installGlyph) {
71 gs.loadDefs(ensureIconLibDefs(), [gid], true);
72 }
73
74 svg = div.append('svg').attr({
75 'class': svgCls,
76 width: dim,
77 height: dim,
78 viewBox: viewBox
79 });
80
81 g = svg.append('g').attr({
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080082 'class': 'icon'
Simon Huntac4c6f72015-02-03 19:50:53 -080083 });
84
85 g.append('rect').attr({
86 width: vboxSize,
87 height: vboxSize,
88 rx: cornerSize
89 });
90
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080091 g.append('use').attr({
92 width: vboxSize,
93 height: vboxSize,
94 'class': 'glyph',
95 'xlink:href': '#' + gid
96 });
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080097 }
98
99 // div is a D3 selection of the <DIV> element into which icon should load
100 // iconCls is the CSS class used to identify the icon
101 // size is dimension of icon in pixels. Defaults to 20.
102 // installGlyph, if truthy, will cause the glyph to be added to
103 // well-known defs element. Defaults to false.
104 // svgClass is the CSS class used to identify the SVG layer.
105 // Defaults to 'embeddedIcon'.
106 function loadIconByClass(div, iconCls, size, installGlyph, svgClass) {
107 loadIcon(div, glyphMapping[iconCls], size, installGlyph, svgClass);
108 div.select('svg g').classed(iconCls, true);
109 }
Simon Huntac4c6f72015-02-03 19:50:53 -0800110
111 function loadEmbeddedIcon(div, iconCls, size) {
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800112 loadIconByClass(div, iconCls, size, true);
Simon Huntac4c6f72015-02-03 19:50:53 -0800113 }
114
115
116 // configuration for device and host icons in the topology view
117 var config = {
118 device: {
119 dim: 36,
120 rx: 4
121 },
122 host: {
123 radius: {
124 noGlyph: 9,
125 withGlyph: 14
126 },
127 glyphed: {
128 endstation: 1,
129 bgpSpeaker: 1,
130 router: 1
131 }
132 }
133 };
134
135
136 // Adds a device icon to the specified element, using the given glyph.
137 // Returns the D3 selection of the icon.
138 function addDeviceIcon(elem, glyphId) {
139 var cfg = config.device,
140 g = elem.append('g')
141 .attr('class', 'svgIcon deviceIcon');
142
143 g.append('rect').attr({
144 x: 0,
145 y: 0,
146 rx: cfg.rx,
147 width: cfg.dim,
148 height: cfg.dim
149 });
150
151 g.append('use').attr({
152 'xlink:href': '#' + glyphId,
153 width: cfg.dim,
154 height: cfg.dim
155 });
156
157 g.dim = cfg.dim;
158 return g;
159 }
160
Simon Hunt1894d792015-02-04 17:09:20 -0800161 function addHostIcon(elem, radius, glyphId) {
162 var dim = radius * 1.5,
163 xlate = -dim / 2,
164 g = elem.append('g')
165 .attr('class', 'svgIcon hostIcon');
166
167 g.append('circle').attr('r', radius);
168
169 g.append('use').attr({
170 'xlink:href': '#' + glyphId,
171 width: dim,
172 height: dim,
173 transform: sus.translate(xlate,xlate)
174 });
175 return g;
Simon Huntac4c6f72015-02-03 19:50:53 -0800176 }
177
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800178 function createSortIcon() {
179 function sortAsc(div) {
180 div.style('display', 'inline-block');
181 loadEmbeddedIcon(div, 'tableColSortAsc', 10);
182 }
183
184 function sortDesc(div) {
185 div.style('display', 'inline-block');
186 loadEmbeddedIcon(div, 'tableColSortDesc', 10);
187 }
188
189 function sortNone(div) {
190 div.remove();
191 }
192
193 return {
194 sortAsc: sortAsc,
195 sortDesc: sortDesc,
196 sortNone: sortNone
197 };
198 }
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')
Simon Hunt97225382015-01-19 13:33:09 -0800205 .factory('IconService', ['$log', 'FnService', 'GlyphService',
Simon Hunt1894d792015-02-04 17:09:20 -0800206 'SvgUtilService',
207
208 function (_$log_, _fs_, _gs_, _sus_) {
Simon Hunt7ac7be92015-01-06 10:47:56 -0800209 $log = _$log_;
Simon Hunt7f172cc2015-01-16 17:43:00 -0800210 fs = _fs_;
Simon Hunt97225382015-01-19 13:33:09 -0800211 gs = _gs_;
Simon Hunt1894d792015-02-04 17:09:20 -0800212 sus = _sus_;
Simon Hunt7f172cc2015-01-16 17:43:00 -0800213
Simon Hunt7ac7be92015-01-06 10:47:56 -0800214 return {
Simon Hunt97225382015-01-19 13:33:09 -0800215 loadIcon: loadIcon,
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800216 loadIconByClass: loadIconByClass,
Simon Huntac4c6f72015-02-03 19:50:53 -0800217 loadEmbeddedIcon: loadEmbeddedIcon,
218 addDeviceIcon: addDeviceIcon,
219 addHostIcon: addHostIcon,
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800220 iconConfig: function () { return config; },
221 createSortIcon: createSortIcon
Simon Hunt7ac7be92015-01-06 10:47:56 -0800222 };
223 }]);
224
225}());