Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 1 | /* |
| 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 Hunt | 7f172cc | 2015-01-16 17:43:00 -0800 | [diff] [blame^] | 25 | var $log, fs; |
| 26 | |
| 27 | var viewBoxDim = 50, |
| 28 | viewBox = '0 0 ' + viewBoxDim + ' ' + viewBoxDim; |
| 29 | |
| 30 | // maps icon id to the glyph id it uses. |
| 31 | // note: icon id maps to a CSS class for styling that icon |
| 32 | var glyphMapping = { |
| 33 | deviceOnline: 'checkMark', |
| 34 | deviceOffline: 'xMark' |
| 35 | }; |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 36 | |
| 37 | angular.module('onosSvg') |
Simon Hunt | 7f172cc | 2015-01-16 17:43:00 -0800 | [diff] [blame^] | 38 | .factory('IconService', ['$log', 'FnService', function (_$log_, _fs_) { |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 39 | $log = _$log_; |
Simon Hunt | 7f172cc | 2015-01-16 17:43:00 -0800 | [diff] [blame^] | 40 | fs = _fs_; |
| 41 | |
| 42 | // div is a D3 selection of the <DIV> element into which icon should load |
| 43 | // iconCls is the CSS class used to identify the icon |
| 44 | // size is dimension of icon in pixels. Defaults to 20. |
| 45 | function loadIcon(div, iconCls, size) { |
| 46 | var dim = size || 20, |
| 47 | gid = glyphMapping[iconCls] || 'unknown'; |
| 48 | |
| 49 | var svg = div.append('svg').attr({ |
| 50 | width: dim, |
| 51 | height: dim, |
| 52 | viewBox: viewBox |
| 53 | }); |
| 54 | var g = svg.append('g').attr({ |
| 55 | 'class': 'icon ' + iconCls |
| 56 | }); |
| 57 | g.append('rect').attr({ |
| 58 | width: viewBoxDim, |
| 59 | height: viewBoxDim, |
| 60 | rx: 4 |
| 61 | }); |
| 62 | g.append('use').attr({ |
| 63 | width: viewBoxDim, |
| 64 | height: viewBoxDim, |
| 65 | 'class': 'glyph', |
| 66 | 'xlink:href': '#' + gid |
| 67 | }); |
| 68 | } |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 69 | |
| 70 | return { |
Simon Hunt | 7f172cc | 2015-01-16 17:43:00 -0800 | [diff] [blame^] | 71 | loadIcon: loadIcon |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 72 | }; |
| 73 | }]); |
| 74 | |
| 75 | }()); |