blob: deb1522b7c4c1f1fe3c1c64a6337ab89cdc2ae9c [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
21 */
22(function () {
23 'use strict';
24
Simon Hunt97225382015-01-19 13:33:09 -080025 var $log, fs, gs;
Simon Hunt7f172cc2015-01-16 17:43:00 -080026
Simon Hunt97225382015-01-19 13:33:09 -080027 var vboxSize = 50,
28 cornerSize = vboxSize / 10,
29 viewBox = '0 0 ' + vboxSize + ' ' + vboxSize;
Simon Hunt7f172cc2015-01-16 17:43:00 -080030
31 // maps icon id to the glyph id it uses.
32 // note: icon id maps to a CSS class for styling that icon
33 var glyphMapping = {
Simon Hunt97225382015-01-19 13:33:09 -080034 deviceOnline: 'crown',
35 deviceOffline: 'chain'
36 //deviceOnline: 'checkMark',
37 //deviceOffline: 'xMark'
Simon Hunt7f172cc2015-01-16 17:43:00 -080038 };
Simon Hunt7ac7be92015-01-06 10:47:56 -080039
Simon Hunt97225382015-01-19 13:33:09 -080040 function ensureIconLibDefs() {
41 var body = d3.select('body'),
42 svg = body.select('svg#IconLibDefs'),
43 defs;
44
45 if (svg.empty()) {
46 svg = body.append('svg').attr('id', 'IconLibDefs');
47 defs = svg.append('defs');
48 }
49 return svg.select('defs');
50 }
51
Simon Hunt7ac7be92015-01-06 10:47:56 -080052 angular.module('onosSvg')
Simon Hunt97225382015-01-19 13:33:09 -080053 .factory('IconService', ['$log', 'FnService', 'GlyphService',
54 function (_$log_, _fs_, _gs_) {
Simon Hunt7ac7be92015-01-06 10:47:56 -080055 $log = _$log_;
Simon Hunt7f172cc2015-01-16 17:43:00 -080056 fs = _fs_;
Simon Hunt97225382015-01-19 13:33:09 -080057 gs = _gs_;
Simon Hunt7f172cc2015-01-16 17:43:00 -080058
59 // div is a D3 selection of the <DIV> element into which icon should load
60 // iconCls is the CSS class used to identify the icon
61 // size is dimension of icon in pixels. Defaults to 20.
Simon Hunt97225382015-01-19 13:33:09 -080062 // installGlyph, if truthy, will cause the glyph to be added to
63 // well-known defs element. Defaults to false.
64 // svgClass is the CSS class used to identify the SVG layer.
65 // Defaults to 'embeddedIcon'.
66 function loadIcon(div, iconCls, size, installGlyph, svgClass) {
Simon Hunt7f172cc2015-01-16 17:43:00 -080067 var dim = size || 20,
Simon Hunt97225382015-01-19 13:33:09 -080068 svgCls = svgClass || 'embeddedIcon',
69 gid = glyphMapping[iconCls] || 'unknown',
70 svg, g;
Simon Hunt7f172cc2015-01-16 17:43:00 -080071
Simon Hunt97225382015-01-19 13:33:09 -080072 if (installGlyph) {
73 gs.loadDefs(ensureIconLibDefs(), [gid], true);
74 }
75
76 svg = div.append('svg').attr({
77 'class': svgCls,
Simon Hunt7f172cc2015-01-16 17:43:00 -080078 width: dim,
79 height: dim,
80 viewBox: viewBox
81 });
Simon Hunt97225382015-01-19 13:33:09 -080082
83 g = svg.append('g').attr({
Simon Hunt7f172cc2015-01-16 17:43:00 -080084 'class': 'icon ' + iconCls
85 });
Simon Hunt97225382015-01-19 13:33:09 -080086
Simon Hunt7f172cc2015-01-16 17:43:00 -080087 g.append('rect').attr({
Simon Hunt97225382015-01-19 13:33:09 -080088 width: vboxSize,
89 height: vboxSize,
90 rx: cornerSize
Simon Hunt7f172cc2015-01-16 17:43:00 -080091 });
Simon Hunt97225382015-01-19 13:33:09 -080092
Simon Hunt7f172cc2015-01-16 17:43:00 -080093 g.append('use').attr({
Simon Hunt97225382015-01-19 13:33:09 -080094 width: vboxSize,
95 height: vboxSize,
Simon Hunt7f172cc2015-01-16 17:43:00 -080096 'class': 'glyph',
97 'xlink:href': '#' + gid
98 });
99 }
Simon Hunt7ac7be92015-01-06 10:47:56 -0800100
Simon Hunt97225382015-01-19 13:33:09 -0800101 function loadEmbeddedIcon(div, iconCls, size) {
102 loadIcon(div, iconCls, size, true);
103 }
104
Simon Hunt7ac7be92015-01-06 10:47:56 -0800105 return {
Simon Hunt97225382015-01-19 13:33:09 -0800106 loadIcon: loadIcon,
107 loadEmbeddedIcon: loadEmbeddedIcon
Simon Hunt7ac7be92015-01-06 10:47:56 -0800108 };
109 }]);
110
111}());