blob: e3f4bdb92ecf10eb7981756106e9e2e616513217 [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',
34 tableColSortAsc: 'triangleUp',
35 tableColSortDesc: 'triangleDown',
36 tableColSortNone: '-'
37 };
38
39
Simon Hunt7ac7be92015-01-06 10:47:56 -080040
Simon Hunt97225382015-01-19 13:33:09 -080041 function ensureIconLibDefs() {
42 var body = d3.select('body'),
43 svg = body.select('svg#IconLibDefs'),
44 defs;
45
46 if (svg.empty()) {
47 svg = body.append('svg').attr('id', 'IconLibDefs');
48 defs = svg.append('defs');
49 }
50 return svg.select('defs');
51 }
52
Simon Huntac4c6f72015-02-03 19:50:53 -080053 // div is a D3 selection of the <DIV> element into which icon should load
54 // iconCls is the CSS class used to identify the icon
55 // size is dimension of icon in pixels. Defaults to 20.
56 // installGlyph, if truthy, will cause the glyph to be added to
57 // well-known defs element. Defaults to false.
58 // svgClass is the CSS class used to identify the SVG layer.
59 // Defaults to 'embeddedIcon'.
60 function loadIcon(div, iconCls, size, installGlyph, svgClass) {
61 var dim = size || 20,
62 svgCls = svgClass || 'embeddedIcon',
63 gid = glyphMapping[iconCls] || 'unknown',
64 svg, g;
65
66 if (installGlyph) {
67 gs.loadDefs(ensureIconLibDefs(), [gid], true);
68 }
69
70 svg = div.append('svg').attr({
71 'class': svgCls,
72 width: dim,
73 height: dim,
74 viewBox: viewBox
75 });
76
77 g = svg.append('g').attr({
78 'class': 'icon ' + iconCls
79 });
80
81 g.append('rect').attr({
82 width: vboxSize,
83 height: vboxSize,
84 rx: cornerSize
85 });
86
87 if (gid !== '-') {
88 g.append('use').attr({
89 width: vboxSize,
90 height: vboxSize,
91 'class': 'glyph',
92 'xlink:href': '#' + gid
93 });
94 }
95 }
96
97 function loadEmbeddedIcon(div, iconCls, size) {
98 loadIcon(div, iconCls, size, true);
99 }
100
101
102 // configuration for device and host icons in the topology view
103 var config = {
104 device: {
105 dim: 36,
106 rx: 4
107 },
108 host: {
109 radius: {
110 noGlyph: 9,
111 withGlyph: 14
112 },
113 glyphed: {
114 endstation: 1,
115 bgpSpeaker: 1,
116 router: 1
117 }
118 }
119 };
120
121
122 // Adds a device icon to the specified element, using the given glyph.
123 // Returns the D3 selection of the icon.
124 function addDeviceIcon(elem, glyphId) {
125 var cfg = config.device,
126 g = elem.append('g')
127 .attr('class', 'svgIcon deviceIcon');
128
129 g.append('rect').attr({
130 x: 0,
131 y: 0,
132 rx: cfg.rx,
133 width: cfg.dim,
134 height: cfg.dim
135 });
136
137 g.append('use').attr({
138 'xlink:href': '#' + glyphId,
139 width: cfg.dim,
140 height: cfg.dim
141 });
142
143 g.dim = cfg.dim;
144 return g;
145 }
146
Simon Hunt1894d792015-02-04 17:09:20 -0800147 function addHostIcon(elem, radius, glyphId) {
148 var dim = radius * 1.5,
149 xlate = -dim / 2,
150 g = elem.append('g')
151 .attr('class', 'svgIcon hostIcon');
152
153 g.append('circle').attr('r', radius);
154
155 g.append('use').attr({
156 'xlink:href': '#' + glyphId,
157 width: dim,
158 height: dim,
159 transform: sus.translate(xlate,xlate)
160 });
161 return g;
Simon Huntac4c6f72015-02-03 19:50:53 -0800162 }
163
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800164 function createSortIcon() {
165 function sortAsc(div) {
166 div.style('display', 'inline-block');
167 loadEmbeddedIcon(div, 'tableColSortAsc', 10);
168 }
169
170 function sortDesc(div) {
171 div.style('display', 'inline-block');
172 loadEmbeddedIcon(div, 'tableColSortDesc', 10);
173 }
174
175 function sortNone(div) {
176 div.remove();
177 }
178
179 return {
180 sortAsc: sortAsc,
181 sortDesc: sortDesc,
182 sortNone: sortNone
183 };
184 }
185
Simon Huntac4c6f72015-02-03 19:50:53 -0800186
187 // =========================
188 // === DEFINE THE MODULE
189
Simon Hunt7ac7be92015-01-06 10:47:56 -0800190 angular.module('onosSvg')
Simon Hunt97225382015-01-19 13:33:09 -0800191 .factory('IconService', ['$log', 'FnService', 'GlyphService',
Simon Hunt1894d792015-02-04 17:09:20 -0800192 'SvgUtilService',
193
194 function (_$log_, _fs_, _gs_, _sus_) {
Simon Hunt7ac7be92015-01-06 10:47:56 -0800195 $log = _$log_;
Simon Hunt7f172cc2015-01-16 17:43:00 -0800196 fs = _fs_;
Simon Hunt97225382015-01-19 13:33:09 -0800197 gs = _gs_;
Simon Hunt1894d792015-02-04 17:09:20 -0800198 sus = _sus_;
Simon Hunt7f172cc2015-01-16 17:43:00 -0800199
Simon Hunt7ac7be92015-01-06 10:47:56 -0800200 return {
Simon Hunt97225382015-01-19 13:33:09 -0800201 loadIcon: loadIcon,
Simon Huntac4c6f72015-02-03 19:50:53 -0800202 loadEmbeddedIcon: loadEmbeddedIcon,
203 addDeviceIcon: addDeviceIcon,
204 addHostIcon: addHostIcon,
Bri Prebilic Cole1f93be22015-02-10 17:16:46 -0800205 iconConfig: function () { return config; },
206 createSortIcon: createSortIcon
Simon Hunt7ac7be92015-01-06 10:47:56 -0800207 };
208 }]);
209
210}());