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