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 -- Glyph Service |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 19 | */ |
| 20 | (function () { |
| 21 | 'use strict'; |
| 22 | |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 23 | // injected references |
| 24 | var $log, fs, sus; |
| 25 | |
| 26 | // internal state |
| 27 | var glyphs = d3.map(), |
Simon Hunt | bc39f6d | 2015-01-06 17:34:28 -0800 | [diff] [blame] | 28 | msgGS = 'GlyphService.'; |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 29 | |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 30 | // ---------------------------------------------------------------------- |
| 31 | // Base set of Glyphs... |
| 32 | |
| 33 | var birdViewBox = '352 224 113 112', |
| 34 | |
| 35 | birdData = { |
| 36 | bird: "M427.7,300.4 c-6.9,0.6-13.1,5-19.2,7.1c-18.1,6.2-33.9," + |
| 37 | "9.1-56.5,4.7c24.6,17.2,36.6,13,63.7,0.1c-0.5,0.6-0.7,1.3-1.3," + |
| 38 | "1.9c1.4-0.4,2.4-1.7,3.4-2.2c-0.4,0.7-0.9,1.5-1.4,1.9c2.2-0.6," + |
| 39 | "3.7-2.3,5.9-3.9c-2.4,2.1-4.2,5-6,8c-1.5,2.5-3.1,4.8-5.1,6.9c-1," + |
| 40 | "1-1.9,1.9-2.9,2.9c-1.4,1.3-2.9,2.5-5.1,2.9c1.7,0.1,3.6-0.3,6.5" + |
| 41 | "-1.9c-1.6,2.4-7.1,6.2-9.9,7.2c10.5-2.6,19.2-15.9,25.7-18c18.3" + |
| 42 | "-5.9,13.8-3.4,27-14.2c1.6-1.3,3-1,5.1-0.8c1.1,0.1,2.1,0.3,3.2," + |
| 43 | "0.5c0.8,0.2,1.4,0.4,2.2,0.8l1.8,0.9c-1.9-4.5-2.3-4.1-5.9-6c-2.3" + |
| 44 | "-1.3-3.3-3.8-6.2-4.9c-7.1-2.6-11.9,11.7-11.7-5c0.1-8,4.2-14.4," + |
| 45 | "6.4-22c1.1-3.8,2.3-7.6,2.4-11.5c0.1-2.3,0-4.7-0.4-7c-2-11.2-8.4" + |
| 46 | "-21.5-19.7-24.8c-1-0.3-1.1-0.3-0.9,0c9.6,17.1,7.2,38.3,3.1,54.2" + |
| 47 | "C429.9,285.5,426.7,293.2,427.7,300.4z" |
| 48 | }, |
| 49 | |
| 50 | glyphViewBox = '0 0 110 110', |
| 51 | |
| 52 | glyphData = { |
| 53 | unknown: "M35,40a5,5,0,0,1,5-5h30a5,5,0,0,1,5,5v30a5,5,0,0,1-5,5" + |
| 54 | "h-30a5,5,0,0,1-5-5z", |
| 55 | |
| 56 | node: "M15,100a5,5,0,0,1-5-5v-65a5,5,0,0,1,5-5h80a5,5,0,0,1,5,5" + |
| 57 | "v65a5,5,0,0,1-5,5zM14,22.5l11-11a10,3,0,0,1,10-2h40a10,3,0,0,1," + |
| 58 | "10,2l11,11zM16,35a5,5,0,0,1,10,0a5,5,0,0,1-10,0z", |
| 59 | |
| 60 | switch: "M10,20a10,10,0,0,1,10-10h70a10,10,0,0,1,10,10v70a10,10," + |
| 61 | "0,0,1-10,10h-70a10,10,0,0,1-10-10zM60,26l12,0,0-8,18,13-18,13,0" + |
| 62 | "-8-12,0zM60,60l12,0,0-8,18,13-18,13,0-8-12,0zM50,40l-12,0,0-8" + |
| 63 | "-18,13,18,13,0-8,12,0zM50,74l-12,0,0-8-18,13,18,13,0-8,12,0z", |
| 64 | |
| 65 | roadm: "M10,35l25-25h40l25,25v40l-25,25h-40l-25-25zM58,26l12,0,0" + |
| 66 | "-8,18,13-18,13,0-8-12,0zM58,60l12,0,0-8,18,13-18,13,0-8-12,0z" + |
| 67 | "M52,40l-12,0,0-8-18,13,18,13,0-8,12,0zM52,74l-12,0,0-8-18,13," + |
| 68 | "18,13,0-8,12,0z", |
| 69 | |
| 70 | endstation: "M10,15a5,5,0,0,1,5-5h65a5,5,0,0,1,5,5v80a5,5,0,0,1" + |
| 71 | "-5,5h-65a5,5,0,0,1-5-5zM87.5,14l11,11a3,10,0,0,1,2,10v40a3,10," + |
| 72 | "0,0,1,-2,10l-11,11zM17,19a2,2,0,0,1,2-2h56a2,2,0,0,1,2,2v26a2," + |
| 73 | "2,0,0,1-2,2h-56a2,2,0,0,1-2-2zM20,20h54v10h-54zM20,33h54v10h" + |
| 74 | "-54zM42,70a5,5,0,0,1,10,0a5,5,0,0,1-10,0z", |
| 75 | |
| 76 | router: "M10,55A45,45,0,0,1,100,55A45,45,0,0,1,10,55M20,50l12,0," + |
| 77 | "0-8,18,13-18,13,0-8-12,0zM90,50l-12,0,0-8-18,13,18,13,0-8,12,0z" + |
| 78 | "M50,47l0-12-8,0,13-18,13,18-8,0,0,12zM50,63l0,12-8,0,13,18,13" + |
| 79 | "-18-8,0,0-12z", |
| 80 | |
| 81 | bgpSpeaker: "M10,40a45,35,0,0,1,90,0Q100,77,55,100Q10,77,10,40z" + |
| 82 | "M50,29l12,0,0-8,18,13-18,13,0-8-12,0zM60,57l-12,0,0-8-18,13," + |
| 83 | "18,13,0-8,12,0z", |
| 84 | |
| 85 | chain: "M60.4,77.6c-4.9,5.2-9.6,11.3-15.3,16.3c-8.6,7.5-20.4,6.8" + |
| 86 | "-28-0.8c-7.7-7.7-8.4-19.6-0.8-28.4c6.5-7.4,13.5-14.4,20.9-20.9" + |
| 87 | "c7.5-6.7,19.2-6.7,26.5-0.8c3.5,2.8,4.4,6.1,2.2,8.7c-2.7,3.1" + |
| 88 | "-5.5,2.5-8.5,0.3c-4.7-3.4-9.7-3.2-14,0.9C37.1,58.7,31,64.8," + |
| 89 | "25.2,71c-4.2,4.4-4.2,10.6-0.6,14.3c3.7,3.7,9.7,3.7,14.3-0.4" + |
| 90 | "c2.9-2.5,5.3-5.5,8.3-8c1-0.9,3-1.1,4.4-0.9C54.8,76.3,57.9,77.1," + |
| 91 | "60.4,77.6zM49.2,32.2c5-5.2,9.7-10.9,15.2-15.7c12.8-11,31.2" + |
| 92 | "-4.9,34.3,11.2C100,34.2,98.3,40.2,94,45c-6.7,7.4-13.7,14.6" + |
| 93 | "-21.2,21.2C65.1,73,53.2,72.7,46,66.5c-3.2-2.8-3.9-5.8-1.6-8.4" + |
| 94 | "c2.6-2.9,5.3-2.4,8.2-0.3c5.2,3.7,10,3.3,14.7-1.1c5.8-5.6,11.6" + |
| 95 | "-11.3,17.2-17.2c4.6-4.8,4.9-11.1,0.9-15c-3.9-3.9-10.1-3.4-15," + |
| 96 | "1.2c-3.1,2.9-5.7,7.4-9.3,8.5C57.6,35.3,53,33,49.2,32.2z", |
| 97 | |
| 98 | crown: "M99.5,21.6c0,3-2.3,5.4-5.1,5.4c-0.3,0-0.7,0-1-0.1c-1.8," + |
| 99 | "4-4.8,10-7.2,17.3c-3.4,10.6-0.9,26.2,2.7,27.3C90.4,72,91.3," + |
| 100 | "75,88,75H22.7c-3.3,0-2.4-3-0.9-3.5c3.6-1.1,6.1-16.7,2.7-27.3" + |
| 101 | "c-2.4-7.4-5.4-13.5-7.2-17.5c-0.5,0.2-1,0.3-1.6,0.3c-2.8,0" + |
| 102 | "-5.1-2.4-5.1-5.4c0-3,2.3-5.4,5.1-5.4c2.8,0,5.1,2.4,5.1,5.4c0," + |
| 103 | "1-0.2,1.9-0.7,2.7c0.7,0.8,1.4,1.6,2.4,2.6c8.8,8.9,11.9,12.7," + |
| 104 | "18.1,11.7c6.5-1,11-8.2,13.3-14.1c-2-0.8-3.3-2.7-3.3-5.1c0-3," + |
| 105 | "2.3-5.4,5.1-5.4c2.8,0,5.1,2.4,5.1,5.4c0,2.5-1.6,4.5-3.7,5.2" + |
| 106 | "c2.3,5.9,6.8,13,13.2,14c6.2,1,9.3-2.7,18.1-11.7c0.7-0.7,1.4" + |
| 107 | "-1.5,2-2.1c-0.6-0.9-1-2-1-3.1c0-3,2.3-5.4,5.1-5.4C97.2,16.2," + |
| 108 | "99.5,18.6,99.5,21.6zM92,87.9c0,2.2-1.7,4.1-3.8,4.1H22.4c" + |
| 109 | "-2.1,0-4.4-1.9-4.4-4.1v-3.3c0-2.2,2.3-4.5,4.4-4.5h65.8c2.1," + |
| 110 | "0,3.8,2.3,3.8,4.5V87.9z" |
| 111 | }, |
| 112 | |
| 113 | badgeViewBox = '0 0 10 10', |
| 114 | |
| 115 | badgeData = { |
| 116 | uiAttached: "M2,2.5a.5,.5,0,0,1,.5-.5h5a.5,.5,0,0,1,.5,.5v3" + |
| 117 | "a.5,.5,0,0,1-.5,.5h-5a.5,.5,0,0,1-.5-.5zM2.5,2.8a.3,.3,0,0,1," + |
| 118 | ".3-.3h4.4a.3,.3,0,0,1,.3,.3v2.4a.3,.3,0,0,1-.3,.3h-4.4" + |
Bri Prebilic Cole | 94a856e | 2015-01-19 15:16:40 -0800 | [diff] [blame] | 119 | "a.3,.3,0,0,1-.3-.3zM2,6.55h6l1,1.45h-8z", |
| 120 | |
Simon Hunt | 66f9e6e | 2015-01-19 20:05:29 -0800 | [diff] [blame] | 121 | checkMark: "M2.6,4.5c0,0,0.7-0.4,1.2,0.3l1.0," + |
| 122 | "1.8c0,0,2.7-5.4,2.8-5.7c0,0,0.5-0.9,1.4-0.1c0," + |
| 123 | "0,0.5,0.5,0,1.3S6.8,7.3,5.6,9.2c0,0-0.4," + |
| 124 | "0.5-1.2,0.1S2.2,5.4,2.2,5.4S2.2,4.7,2.6,4.5z", |
Bri Prebilic Cole | 94a856e | 2015-01-19 15:16:40 -0800 | [diff] [blame] | 125 | |
Simon Hunt | 66f9e6e | 2015-01-19 20:05:29 -0800 | [diff] [blame] | 126 | xMark: "M9.0,7.2C8.2,6.9,7.4,6.1,6.7,5.2c0.4-0.5," + |
| 127 | "0.7-0.8,0.8-1.0C7.8,3.5,9.4,1.6,8.1,1.1" + |
| 128 | "C6.8,0.6,6.6,1.7,6.6,1.7C6.4,2.1,6.0,2.7,5.4," + |
| 129 | "3.4C4.9,2.5,4.5,1.9,4.5,1.9" + |
| 130 | "S3.8,0.2,2.9,0.7C1.9,1.1,2.3,2.3,2.3,2.3c0.3,1.1,0.8,2.1,1.4,2.9" + |
| 131 | "C2.5,6.4,1.3,7.4,1.3,7.4S0.8,7.8,0.8,8.1C0.9,8.3,0.9,9.6,2.4,9.1" + |
| 132 | "C3.1,8.8,4.1,7.9,5.1,7.0c1.3,1.3,2.5,1.9,2.5,1.9s0.5,0.5,1.4-0.2" + |
Bri Prebilic Cole | 1dc32e6 | 2015-02-03 09:44:33 -0800 | [diff] [blame] | 133 | "C9.8,7.9,9.0,7.2,9.0,7.2z", |
| 134 | |
| 135 | triangleUp: "M0.5,6.2c0,0,3.8-3.8,4.2-4.2C5,1.7,5.3,2,5.3,2l4.3," + |
| 136 | "4.3c0,0,0.4,0.4-0.1,0.4c-1.7,0-8.2,0-8.8,0C0,6.6,0.5,6.2,0.5,6.2z", |
| 137 | |
| 138 | triangleDown: "M9.5,4.2c0,0-3.8,3.8-4.2,4.2c-0.3,0.3-0.5,0-0.5," + |
| 139 | "0L0.5,4.2c0,0-0.4-0.4,0.1-0.4c1.7,0,8.2,0,8.8,0C10,3.8,9.5,4.2," + |
| 140 | "9.5,4.2z" |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | // ---------------------------------------------------------------------- |
| 144 | |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 145 | function clear() { |
| 146 | // start with a fresh map |
| 147 | glyphs = d3.map(); |
| 148 | } |
| 149 | |
| 150 | function init() { |
| 151 | clear(); |
| 152 | register(birdViewBox, birdData); |
| 153 | register(glyphViewBox, glyphData); |
| 154 | register(badgeViewBox, badgeData); |
| 155 | } |
| 156 | |
| 157 | function register(viewBox, data, overwrite) { |
| 158 | var dmap = d3.map(data), |
| 159 | dups = [], |
| 160 | ok; |
| 161 | |
| 162 | dmap.forEach(function (key, value) { |
| 163 | if (!overwrite && glyphs.get(key)) { |
| 164 | dups.push(key); |
| 165 | } else { |
| 166 | glyphs.set(key, {id: key, vb: viewBox, d: value}); |
| 167 | } |
| 168 | }); |
| 169 | ok = (dups.length == 0); |
| 170 | if (!ok) { |
| 171 | dups.forEach(function (id) { |
| 172 | $log.warn(msgGS + 'register(): ID collision: "'+id+'"'); |
| 173 | }); |
| 174 | } |
| 175 | return ok; |
| 176 | } |
| 177 | |
| 178 | function ids() { |
| 179 | return glyphs.keys(); |
| 180 | } |
| 181 | |
| 182 | function glyph(id) { |
| 183 | return glyphs.get(id); |
| 184 | } |
| 185 | |
| 186 | // Note: defs should be a D3 selection of a single <defs> element |
| 187 | function loadDefs(defs, glyphIds, noClear) { |
| 188 | var list = fs.isA(glyphIds) || ids(), |
| 189 | clearCache = !noClear; |
| 190 | |
| 191 | if (clearCache) { |
| 192 | // remove all existing content |
| 193 | defs.html(null); |
| 194 | } |
| 195 | |
| 196 | // load up the requested glyphs |
| 197 | list.forEach(function (id) { |
| 198 | var g = glyph(id); |
| 199 | if (g) { |
| 200 | if (noClear) { |
| 201 | // quick exit if symbol is already present |
| 202 | if (defs.select('symbol#' + g.id).size() > 0) { |
| 203 | return; |
| 204 | } |
| 205 | } |
| 206 | defs.append('symbol') |
| 207 | .attr({ id: g.id, viewBox: g.vb }) |
| 208 | .append('path').attr('d', g.d); |
| 209 | } |
| 210 | }); |
| 211 | } |
| 212 | |
Simon Hunt | edf5c0e | 2015-01-29 15:00:53 -0800 | [diff] [blame] | 213 | // trans can specify translation [x,y] |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 214 | function addGlyph(elem, glyphId, size, overlay, trans) { |
| 215 | var sz = size || 40, |
| 216 | ovr = !!overlay, |
| 217 | xns = fs.isA(trans), |
| 218 | atr = { |
| 219 | width: sz, |
| 220 | height: sz, |
| 221 | 'class': 'glyph', |
| 222 | 'xlink:href': '#' + glyphId |
| 223 | }; |
| 224 | |
| 225 | if (xns) { |
| 226 | atr.transform = sus.translate(trans); |
| 227 | } |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 228 | return elem.append('use').attr(atr).classed('overlay', ovr); |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | // ---------------------------------------------------------------------- |
| 232 | |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 233 | angular.module('onosSvg') |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 234 | .factory('GlyphService', |
| 235 | ['$log', 'FnService', 'SvgUtilService', |
| 236 | |
| 237 | function (_$log_, _fs_, _sus_) { |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 238 | $log = _$log_; |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 239 | fs = _fs_; |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 240 | sus = _sus_; |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 241 | |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 242 | return { |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 243 | clear: clear, |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 244 | init: init, |
| 245 | register: register, |
| 246 | ids: ids, |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 247 | glyph: glyph, |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 248 | loadDefs: loadDefs, |
| 249 | addGlyph: addGlyph |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 250 | }; |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 251 | }] |
| 252 | ); |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 253 | |
| 254 | }()); |