blob: 400b7aa7cb58e8200e75daafcb068728ace5411e [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 = {
Simon Huntac4c6f72015-02-03 19:50:53 -080032 deviceOnline: 'checkMark',
33 deviceOffline: 'xMark',
Bri Prebilic Coledea09742015-02-12 15:33:50 -080034 devIcon_SWITCH: 'switch',
35
Simon Huntac4c6f72015-02-03 19:50:53 -080036 tableColSortAsc: 'triangleUp',
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080037 tableColSortDesc: 'triangleDown'
Simon Huntac4c6f72015-02-03 19:50:53 -080038 };
39
40
Simon Hunt7ac7be92015-01-06 10:47:56 -080041
Simon Hunt97225382015-01-19 13:33:09 -080042 function ensureIconLibDefs() {
43 var body = d3.select('body'),
44 svg = body.select('svg#IconLibDefs'),
45 defs;
46
47 if (svg.empty()) {
48 svg = body.append('svg').attr('id', 'IconLibDefs');
49 defs = svg.append('defs');
50 }
51 return svg.select('defs');
52 }
53
Simon Huntac4c6f72015-02-03 19:50:53 -080054 // div is a D3 selection of the <DIV> element into which icon should load
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080055 // glyphId identifies the glyph to use
Simon Huntac4c6f72015-02-03 19:50:53 -080056 // size is dimension of icon in pixels. Defaults to 20.
57 // installGlyph, if truthy, will cause the glyph to be added to
58 // well-known defs element. Defaults to false.
59 // svgClass is the CSS class used to identify the SVG layer.
60 // Defaults to 'embeddedIcon'.
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080061 function loadIcon(div, glyphId, size, installGlyph, svgClass) {
Simon Huntac4c6f72015-02-03 19:50:53 -080062 var dim = size || 20,
63 svgCls = svgClass || 'embeddedIcon',
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080064 gid = glyphId || 'unknown',
Simon Huntac4c6f72015-02-03 19:50:53 -080065 svg, g;
66
67 if (installGlyph) {
68 gs.loadDefs(ensureIconLibDefs(), [gid], true);
69 }
70
71 svg = div.append('svg').attr({
72 'class': svgCls,
73 width: dim,
74 height: dim,
75 viewBox: viewBox
76 });
77
78 g = svg.append('g').attr({
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080079 'class': 'icon'
Simon Huntac4c6f72015-02-03 19:50:53 -080080 });
81
82 g.append('rect').attr({
83 width: vboxSize,
84 height: vboxSize,
85 rx: cornerSize
86 });
87
Bri Prebilic Colee1bda3f2015-02-13 17:01:49 -080088 g.append('use').attr({
89 width: vboxSize,
90 height: vboxSize,
91 'class': 'glyph',
92 'xlink:href': '#' + gid
93 });
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -080094 }
95
96 // div is a D3 selection of the <DIV> element into which icon should load
97 // iconCls is the CSS class used to identify the icon
98 // size is dimension of icon in pixels. Defaults to 20.
99 // installGlyph, if truthy, will cause the glyph to be added to
100 // well-known defs element. Defaults to false.
101 // svgClass is the CSS class used to identify the SVG layer.
102 // Defaults to 'embeddedIcon'.
103 function loadIconByClass(div, iconCls, size, installGlyph, svgClass) {
104 loadIcon(div, glyphMapping[iconCls], size, installGlyph, svgClass);
105 div.select('svg g').classed(iconCls, true);
106 }
Simon Huntac4c6f72015-02-03 19:50:53 -0800107
108 function loadEmbeddedIcon(div, iconCls, size) {
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800109 loadIconByClass(div, iconCls, size, true);
Simon Huntac4c6f72015-02-03 19:50:53 -0800110 }
111
112
113 // configuration for device and host icons in the topology view
114 var config = {
115 device: {
116 dim: 36,
117 rx: 4
118 },
119 host: {
120 radius: {
121 noGlyph: 9,
122 withGlyph: 14
123 },
124 glyphed: {
125 endstation: 1,
126 bgpSpeaker: 1,
127 router: 1
128 }
129 }
130 };
131
132
133 // Adds a device icon to the specified element, using the given glyph.
134 // Returns the D3 selection of the icon.
135 function addDeviceIcon(elem, glyphId) {
136 var cfg = config.device,
137 g = elem.append('g')
138 .attr('class', 'svgIcon deviceIcon');
139
140 g.append('rect').attr({
141 x: 0,
142 y: 0,
143 rx: cfg.rx,
144 width: cfg.dim,
145 height: cfg.dim
146 });
147
148 g.append('use').attr({
149 'xlink:href': '#' + glyphId,
150 width: cfg.dim,
151 height: cfg.dim
152 });
153
154 g.dim = cfg.dim;
155 return g;
156 }
157
Simon Hunt1894d792015-02-04 17:09:20 -0800158 function addHostIcon(elem, radius, glyphId) {
159 var dim = radius * 1.5,
160 xlate = -dim / 2,
161 g = elem.append('g')
162 .attr('class', 'svgIcon hostIcon');
163
164 g.append('circle').attr('r', radius);
165
166 g.append('use').attr({
167 'xlink:href': '#' + glyphId,
168 width: dim,
169 height: dim,
170 transform: sus.translate(xlate,xlate)
171 });
172 return g;
Simon Huntac4c6f72015-02-03 19:50:53 -0800173 }
174
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800175 function createSortIcon() {
176 function sortAsc(div) {
177 div.style('display', 'inline-block');
178 loadEmbeddedIcon(div, 'tableColSortAsc', 10);
179 }
180
181 function sortDesc(div) {
182 div.style('display', 'inline-block');
183 loadEmbeddedIcon(div, 'tableColSortDesc', 10);
184 }
185
186 function sortNone(div) {
187 div.remove();
188 }
189
190 return {
191 sortAsc: sortAsc,
192 sortDesc: sortDesc,
193 sortNone: sortNone
194 };
195 }
196
Simon Huntac4c6f72015-02-03 19:50:53 -0800197
198 // =========================
199 // === DEFINE THE MODULE
200
Simon Hunt7ac7be92015-01-06 10:47:56 -0800201 angular.module('onosSvg')
Simon Hunt97225382015-01-19 13:33:09 -0800202 .factory('IconService', ['$log', 'FnService', 'GlyphService',
Simon Hunt1894d792015-02-04 17:09:20 -0800203 'SvgUtilService',
204
205 function (_$log_, _fs_, _gs_, _sus_) {
Simon Hunt7ac7be92015-01-06 10:47:56 -0800206 $log = _$log_;
Simon Hunt7f172cc2015-01-16 17:43:00 -0800207 fs = _fs_;
Simon Hunt97225382015-01-19 13:33:09 -0800208 gs = _gs_;
Simon Hunt1894d792015-02-04 17:09:20 -0800209 sus = _sus_;
Simon Hunt7f172cc2015-01-16 17:43:00 -0800210
Simon Hunt7ac7be92015-01-06 10:47:56 -0800211 return {
Simon Hunt97225382015-01-19 13:33:09 -0800212 loadIcon: loadIcon,
Bri Prebilic Cole40be6b22015-02-19 17:12:23 -0800213 loadIconByClass: loadIconByClass,
Simon Huntac4c6f72015-02-03 19:50:53 -0800214 loadEmbeddedIcon: loadEmbeddedIcon,
215 addDeviceIcon: addDeviceIcon,
216 addHostIcon: addHostIcon,
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800217 iconConfig: function () { return config; },
218 createSortIcon: createSortIcon
Simon Hunt7ac7be92015-01-06 10:47:56 -0800219 };
220 }]);
221
222}());