blob: d8c258427f181aa15cf57dab0c10a8aca586a8c0 [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
29 // maps icon id to the glyph id it uses.
30 // note: icon id maps to a CSS class for styling that icon
31 var glyphMapping = {
Bri Prebilic Cole94a856e2015-01-19 15:16:40 -080032 deviceOnline: 'checkMark',
Bri Prebilic Cole1dc32e62015-02-03 09:44:33 -080033 deviceOffline: 'xMark',
34 tableColSortAsc: 'triangleUp',
35 tableColSortDesc: 'triangleDown',
36 tableColSortNone: '-'
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
Bri Prebilic Coledee46622015-02-03 16:36:11 -080092 if (gid !== '-') {
93 g.append('use').attr({
94 width: vboxSize,
95 height: vboxSize,
96 'class': 'glyph',
97 'xlink:href': '#' + gid
98 });
99 }
Simon Hunt7f172cc2015-01-16 17:43:00 -0800100 }
Simon Hunt7ac7be92015-01-06 10:47:56 -0800101
Simon Hunt97225382015-01-19 13:33:09 -0800102 function loadEmbeddedIcon(div, iconCls, size) {
103 loadIcon(div, iconCls, size, true);
104 }
105
Simon Hunt7ac7be92015-01-06 10:47:56 -0800106 return {
Simon Hunt97225382015-01-19 13:33:09 -0800107 loadIcon: loadIcon,
108 loadEmbeddedIcon: loadEmbeddedIcon
Simon Hunt7ac7be92015-01-06 10:47:56 -0800109 };
110 }]);
111
112}());