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