blob: aade3a703e608d0698831e948d08ff5f023e56cc [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
19
20 @author Simon Hunt
Bri Prebilic Cole94a856e2015-01-19 15:16:40 -080021 @author Bri Prebilic Cole
Simon Hunt7ac7be92015-01-06 10:47:56 -080022 */
23(function () {
24 'use strict';
25
Simon Hunt97225382015-01-19 13:33:09 -080026 var $log, fs, gs;
Simon Hunt7f172cc2015-01-16 17:43:00 -080027
Simon Hunt97225382015-01-19 13:33:09 -080028 var vboxSize = 50,
29 cornerSize = vboxSize / 10,
30 viewBox = '0 0 ' + vboxSize + ' ' + vboxSize;
Simon Hunt7f172cc2015-01-16 17:43:00 -080031
32 // maps icon id to the glyph id it uses.
33 // note: icon id maps to a CSS class for styling that icon
34 var glyphMapping = {
Bri Prebilic Cole94a856e2015-01-19 15:16:40 -080035 deviceOnline: 'checkMark',
36 deviceOffline: 'xMark'
Simon Hunt7f172cc2015-01-16 17:43:00 -080037 };
Simon Hunt7ac7be92015-01-06 10:47:56 -080038
Simon Hunt97225382015-01-19 13:33:09 -080039 function ensureIconLibDefs() {
40 var body = d3.select('body'),
41 svg = body.select('svg#IconLibDefs'),
42 defs;
43
44 if (svg.empty()) {
45 svg = body.append('svg').attr('id', 'IconLibDefs');
46 defs = svg.append('defs');
47 }
48 return svg.select('defs');
49 }
50
Simon Hunt7ac7be92015-01-06 10:47:56 -080051 angular.module('onosSvg')
Simon Hunt97225382015-01-19 13:33:09 -080052 .factory('IconService', ['$log', 'FnService', 'GlyphService',
53 function (_$log_, _fs_, _gs_) {
Simon Hunt7ac7be92015-01-06 10:47:56 -080054 $log = _$log_;
Simon Hunt7f172cc2015-01-16 17:43:00 -080055 fs = _fs_;
Simon Hunt97225382015-01-19 13:33:09 -080056 gs = _gs_;
Simon Hunt7f172cc2015-01-16 17:43:00 -080057
58 // div is a D3 selection of the <DIV> element into which icon should load
59 // iconCls is the CSS class used to identify the icon
60 // size is dimension of icon in pixels. Defaults to 20.
Simon Hunt97225382015-01-19 13:33:09 -080061 // installGlyph, if truthy, will cause the glyph to be added to
62 // well-known defs element. Defaults to false.
63 // svgClass is the CSS class used to identify the SVG layer.
64 // Defaults to 'embeddedIcon'.
65 function loadIcon(div, iconCls, size, installGlyph, svgClass) {
Simon Hunt7f172cc2015-01-16 17:43:00 -080066 var dim = size || 20,
Simon Hunt97225382015-01-19 13:33:09 -080067 svgCls = svgClass || 'embeddedIcon',
68 gid = glyphMapping[iconCls] || 'unknown',
69 svg, g;
Simon Hunt7f172cc2015-01-16 17:43:00 -080070
Simon Hunt97225382015-01-19 13:33:09 -080071 if (installGlyph) {
72 gs.loadDefs(ensureIconLibDefs(), [gid], true);
73 }
74
75 svg = div.append('svg').attr({
76 'class': svgCls,
Simon Hunt7f172cc2015-01-16 17:43:00 -080077 width: dim,
78 height: dim,
79 viewBox: viewBox
80 });
Simon Hunt97225382015-01-19 13:33:09 -080081
82 g = svg.append('g').attr({
Simon Hunt7f172cc2015-01-16 17:43:00 -080083 'class': 'icon ' + iconCls
84 });
Simon Hunt97225382015-01-19 13:33:09 -080085
Simon Hunt7f172cc2015-01-16 17:43:00 -080086 g.append('rect').attr({
Simon Hunt97225382015-01-19 13:33:09 -080087 width: vboxSize,
88 height: vboxSize,
89 rx: cornerSize
Simon Hunt7f172cc2015-01-16 17:43:00 -080090 });
Simon Hunt97225382015-01-19 13:33:09 -080091
Simon Hunt7f172cc2015-01-16 17:43:00 -080092 g.append('use').attr({
Simon Hunt97225382015-01-19 13:33:09 -080093 width: vboxSize,
94 height: vboxSize,
Simon Hunt7f172cc2015-01-16 17:43:00 -080095 'class': 'glyph',
96 'xlink:href': '#' + gid
97 });
98 }
Simon Hunt7ac7be92015-01-06 10:47:56 -080099
Simon Hunt97225382015-01-19 13:33:09 -0800100 function loadEmbeddedIcon(div, iconCls, size) {
101 loadIcon(div, iconCls, size, true);
102 }
103
Simon Hunt7ac7be92015-01-06 10:47:56 -0800104 return {
Simon Hunt97225382015-01-19 13:33:09 -0800105 loadIcon: loadIcon,
106 loadEmbeddedIcon: loadEmbeddedIcon
Simon Hunt7ac7be92015-01-06 10:47:56 -0800107 };
108 }]);
109
110}());