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 | 9722538 | 2015-01-19 13:33:09 -0800 | [diff] [blame^] | 25 | var $log, fs, gs; |
Simon Hunt | 7f172cc | 2015-01-16 17:43:00 -0800 | [diff] [blame] | 26 | |
Simon Hunt | 9722538 | 2015-01-19 13:33:09 -0800 | [diff] [blame^] | 27 | var vboxSize = 50, |
| 28 | cornerSize = vboxSize / 10, |
| 29 | viewBox = '0 0 ' + vboxSize + ' ' + vboxSize; |
Simon Hunt | 7f172cc | 2015-01-16 17:43:00 -0800 | [diff] [blame] | 30 | |
| 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 Hunt | 9722538 | 2015-01-19 13:33:09 -0800 | [diff] [blame^] | 34 | deviceOnline: 'crown', |
| 35 | deviceOffline: 'chain' |
| 36 | //deviceOnline: 'checkMark', |
| 37 | //deviceOffline: 'xMark' |
Simon Hunt | 7f172cc | 2015-01-16 17:43:00 -0800 | [diff] [blame] | 38 | }; |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 39 | |
Simon Hunt | 9722538 | 2015-01-19 13:33:09 -0800 | [diff] [blame^] | 40 | 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 Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 52 | angular.module('onosSvg') |
Simon Hunt | 9722538 | 2015-01-19 13:33:09 -0800 | [diff] [blame^] | 53 | .factory('IconService', ['$log', 'FnService', 'GlyphService', |
| 54 | function (_$log_, _fs_, _gs_) { |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 55 | $log = _$log_; |
Simon Hunt | 7f172cc | 2015-01-16 17:43:00 -0800 | [diff] [blame] | 56 | fs = _fs_; |
Simon Hunt | 9722538 | 2015-01-19 13:33:09 -0800 | [diff] [blame^] | 57 | gs = _gs_; |
Simon Hunt | 7f172cc | 2015-01-16 17:43:00 -0800 | [diff] [blame] | 58 | |
| 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 Hunt | 9722538 | 2015-01-19 13:33:09 -0800 | [diff] [blame^] | 62 | // 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 Hunt | 7f172cc | 2015-01-16 17:43:00 -0800 | [diff] [blame] | 67 | var dim = size || 20, |
Simon Hunt | 9722538 | 2015-01-19 13:33:09 -0800 | [diff] [blame^] | 68 | svgCls = svgClass || 'embeddedIcon', |
| 69 | gid = glyphMapping[iconCls] || 'unknown', |
| 70 | svg, g; |
Simon Hunt | 7f172cc | 2015-01-16 17:43:00 -0800 | [diff] [blame] | 71 | |
Simon Hunt | 9722538 | 2015-01-19 13:33:09 -0800 | [diff] [blame^] | 72 | if (installGlyph) { |
| 73 | gs.loadDefs(ensureIconLibDefs(), [gid], true); |
| 74 | } |
| 75 | |
| 76 | svg = div.append('svg').attr({ |
| 77 | 'class': svgCls, |
Simon Hunt | 7f172cc | 2015-01-16 17:43:00 -0800 | [diff] [blame] | 78 | width: dim, |
| 79 | height: dim, |
| 80 | viewBox: viewBox |
| 81 | }); |
Simon Hunt | 9722538 | 2015-01-19 13:33:09 -0800 | [diff] [blame^] | 82 | |
| 83 | g = svg.append('g').attr({ |
Simon Hunt | 7f172cc | 2015-01-16 17:43:00 -0800 | [diff] [blame] | 84 | 'class': 'icon ' + iconCls |
| 85 | }); |
Simon Hunt | 9722538 | 2015-01-19 13:33:09 -0800 | [diff] [blame^] | 86 | |
Simon Hunt | 7f172cc | 2015-01-16 17:43:00 -0800 | [diff] [blame] | 87 | g.append('rect').attr({ |
Simon Hunt | 9722538 | 2015-01-19 13:33:09 -0800 | [diff] [blame^] | 88 | width: vboxSize, |
| 89 | height: vboxSize, |
| 90 | rx: cornerSize |
Simon Hunt | 7f172cc | 2015-01-16 17:43:00 -0800 | [diff] [blame] | 91 | }); |
Simon Hunt | 9722538 | 2015-01-19 13:33:09 -0800 | [diff] [blame^] | 92 | |
Simon Hunt | 7f172cc | 2015-01-16 17:43:00 -0800 | [diff] [blame] | 93 | g.append('use').attr({ |
Simon Hunt | 9722538 | 2015-01-19 13:33:09 -0800 | [diff] [blame^] | 94 | width: vboxSize, |
| 95 | height: vboxSize, |
Simon Hunt | 7f172cc | 2015-01-16 17:43:00 -0800 | [diff] [blame] | 96 | 'class': 'glyph', |
| 97 | 'xlink:href': '#' + gid |
| 98 | }); |
| 99 | } |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 100 | |
Simon Hunt | 9722538 | 2015-01-19 13:33:09 -0800 | [diff] [blame^] | 101 | function loadEmbeddedIcon(div, iconCls, size) { |
| 102 | loadIcon(div, iconCls, size, true); |
| 103 | } |
| 104 | |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 105 | return { |
Simon Hunt | 9722538 | 2015-01-19 13:33:09 -0800 | [diff] [blame^] | 106 | loadIcon: loadIcon, |
| 107 | loadEmbeddedIcon: loadEmbeddedIcon |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 108 | }; |
| 109 | }]); |
| 110 | |
| 111 | }()); |