blob: 21f9b9dc3c360c86a8ee8366bbe754ef46d6cf29 [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 Hunt97225382015-01-19 13:33:09 -080023 var $log, fs, gs;
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
147 function addHostIcon(elem, glyphId) {
148 // TODO:
149 }
150
151
152 // =========================
153 // === DEFINE THE MODULE
154
Simon Hunt7ac7be92015-01-06 10:47:56 -0800155 angular.module('onosSvg')
Simon Hunt97225382015-01-19 13:33:09 -0800156 .factory('IconService', ['$log', 'FnService', 'GlyphService',
157 function (_$log_, _fs_, _gs_) {
Simon Hunt7ac7be92015-01-06 10:47:56 -0800158 $log = _$log_;
Simon Hunt7f172cc2015-01-16 17:43:00 -0800159 fs = _fs_;
Simon Hunt97225382015-01-19 13:33:09 -0800160 gs = _gs_;
Simon Hunt7f172cc2015-01-16 17:43:00 -0800161
Simon Hunt7ac7be92015-01-06 10:47:56 -0800162 return {
Simon Hunt97225382015-01-19 13:33:09 -0800163 loadIcon: loadIcon,
Simon Huntac4c6f72015-02-03 19:50:53 -0800164 loadEmbeddedIcon: loadEmbeddedIcon,
165 addDeviceIcon: addDeviceIcon,
166 addHostIcon: addHostIcon,
167 iconConfig: function () { return config; }
Simon Hunt7ac7be92015-01-06 10:47:56 -0800168 };
169 }]);
170
171}());