Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 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 |
Simon Hunt | 4afa61d | 2016-02-23 09:31:43 -0800 | [diff] [blame] | 24 | var $log, fs, sus, gd; |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 25 | |
| 26 | // internal state |
Simon Hunt | 4afa61d | 2016-02-23 09:31:43 -0800 | [diff] [blame] | 27 | var glyphs = d3.map(), |
| 28 | api; |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 29 | |
Simon Hunt | 4afa61d | 2016-02-23 09:31:43 -0800 | [diff] [blame] | 30 | // constants |
Simon Hunt | 9c1c45e | 2015-04-10 13:38:27 -0700 | [diff] [blame] | 31 | var msgGS = 'GlyphService.', |
| 32 | rg = "registerGlyphs(): ", |
| 33 | rgs = "registerGlyphSet(): "; |
| 34 | |
| 35 | // ---------------------------------------------------------------------- |
| 36 | |
| 37 | function warn(msg) { |
| 38 | $log.warn(msgGS + msg); |
| 39 | } |
| 40 | |
| 41 | function addToMap(key, value, vbox, overwrite, dups) { |
| 42 | if (!overwrite && glyphs.get(key)) { |
| 43 | dups.push(key); |
| 44 | } else { |
| 45 | glyphs.set(key, {id: key, vb: vbox, d: value}); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | function reportDups(dups, which) { |
| 50 | var ok = (dups.length == 0), |
| 51 | msg = 'ID collision: '; |
| 52 | |
| 53 | if (!ok) { |
| 54 | dups.forEach(function (id) { |
| 55 | warn(which + msg + '"' + id + '"'); |
| 56 | }); |
| 57 | } |
| 58 | return ok; |
| 59 | } |
| 60 | |
| 61 | function reportMissVb(missing, which) { |
| 62 | var ok = (missing.length == 0), |
| 63 | msg = 'Missing viewbox property: '; |
| 64 | |
| 65 | if (!ok) { |
| 66 | missing.forEach(function (vbk) { |
| 67 | warn(which + msg + '"' + vbk + '"'); |
| 68 | }); |
| 69 | } |
| 70 | return ok; |
| 71 | } |
| 72 | |
| 73 | // ---------------------------------------------------------------------- |
| 74 | // === API functions === |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 75 | |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 76 | function clear() { |
| 77 | // start with a fresh map |
| 78 | glyphs = d3.map(); |
| 79 | } |
| 80 | |
| 81 | function init() { |
Simon Hunt | 4afa61d | 2016-02-23 09:31:43 -0800 | [diff] [blame] | 82 | gd.registerCoreGlyphs(api); |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 83 | } |
| 84 | |
Simon Hunt | 9c1c45e | 2015-04-10 13:38:27 -0700 | [diff] [blame] | 85 | function registerGlyphs(data, overwrite) { |
| 86 | var dups = [], |
| 87 | missvb = []; |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 88 | |
Simon Hunt | 9c1c45e | 2015-04-10 13:38:27 -0700 | [diff] [blame] | 89 | angular.forEach(data, function (value, key) { |
| 90 | var vbk = '_' + key, |
| 91 | vb = data[vbk]; |
| 92 | |
| 93 | if (key[0] !== '_') { |
| 94 | if (!vb) { |
| 95 | missvb.push(vbk); |
| 96 | return; |
| 97 | } |
| 98 | addToMap(key, value, vb, overwrite, dups); |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 99 | } |
| 100 | }); |
Simon Hunt | 9c1c45e | 2015-04-10 13:38:27 -0700 | [diff] [blame] | 101 | return reportDups(dups, rg) && reportMissVb(missvb, rg); |
| 102 | } |
| 103 | |
| 104 | function registerGlyphSet(data, overwrite) { |
| 105 | var dups = [], |
| 106 | vb = data._viewbox; |
| 107 | |
| 108 | if (!vb) { |
| 109 | warn(rgs + 'no "_viewbox" property found'); |
| 110 | return false; |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 111 | } |
Simon Hunt | 9c1c45e | 2015-04-10 13:38:27 -0700 | [diff] [blame] | 112 | |
| 113 | angular.forEach(data, function (value, key) { |
| 114 | if (key[0] !== '_') { |
| 115 | addToMap(key, value, vb, overwrite, dups); |
| 116 | } |
| 117 | }); |
| 118 | return reportDups(dups, rgs); |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | function ids() { |
| 122 | return glyphs.keys(); |
| 123 | } |
| 124 | |
| 125 | function glyph(id) { |
| 126 | return glyphs.get(id); |
| 127 | } |
| 128 | |
Simon Hunt | 044f2807 | 2015-10-08 12:38:16 -0700 | [diff] [blame] | 129 | function glyphDefined(id) { |
| 130 | return glyphs.has(id); |
| 131 | } |
| 132 | |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 133 | // Note: defs should be a D3 selection of a single <defs> element |
| 134 | function loadDefs(defs, glyphIds, noClear) { |
| 135 | var list = fs.isA(glyphIds) || ids(), |
| 136 | clearCache = !noClear; |
| 137 | |
| 138 | if (clearCache) { |
| 139 | // remove all existing content |
| 140 | defs.html(null); |
| 141 | } |
| 142 | |
| 143 | // load up the requested glyphs |
| 144 | list.forEach(function (id) { |
| 145 | var g = glyph(id); |
| 146 | if (g) { |
| 147 | if (noClear) { |
| 148 | // quick exit if symbol is already present |
| 149 | if (defs.select('symbol#' + g.id).size() > 0) { |
| 150 | return; |
| 151 | } |
| 152 | } |
| 153 | defs.append('symbol') |
| 154 | .attr({ id: g.id, viewBox: g.vb }) |
| 155 | .append('path').attr('d', g.d); |
| 156 | } |
| 157 | }); |
| 158 | } |
| 159 | |
Simon Hunt | edf5c0e | 2015-01-29 15:00:53 -0800 | [diff] [blame] | 160 | // trans can specify translation [x,y] |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 161 | function addGlyph(elem, glyphId, size, overlay, trans) { |
| 162 | var sz = size || 40, |
| 163 | ovr = !!overlay, |
| 164 | xns = fs.isA(trans), |
| 165 | atr = { |
| 166 | width: sz, |
| 167 | height: sz, |
| 168 | 'class': 'glyph', |
| 169 | 'xlink:href': '#' + glyphId |
| 170 | }; |
| 171 | |
| 172 | if (xns) { |
| 173 | atr.transform = sus.translate(trans); |
| 174 | } |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 175 | return elem.append('use').attr(atr).classed('overlay', ovr); |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | // ---------------------------------------------------------------------- |
| 179 | |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 180 | angular.module('onosSvg') |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 181 | .factory('GlyphService', |
Simon Hunt | 4afa61d | 2016-02-23 09:31:43 -0800 | [diff] [blame] | 182 | ['$log', 'FnService', 'SvgUtilService', 'GlyphDataService', |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 183 | |
Simon Hunt | 4afa61d | 2016-02-23 09:31:43 -0800 | [diff] [blame] | 184 | function (_$log_, _fs_, _sus_, _gd_) { |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 185 | $log = _$log_; |
Simon Hunt | 58f23bb | 2015-01-16 16:32:24 -0800 | [diff] [blame] | 186 | fs = _fs_; |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 187 | sus = _sus_; |
Simon Hunt | 4afa61d | 2016-02-23 09:31:43 -0800 | [diff] [blame] | 188 | gd = _gd_; |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 189 | |
Simon Hunt | 4afa61d | 2016-02-23 09:31:43 -0800 | [diff] [blame] | 190 | api = { |
Simon Hunt | cacce34 | 2015-01-07 16:13:05 -0800 | [diff] [blame] | 191 | clear: clear, |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 192 | init: init, |
Simon Hunt | 9c1c45e | 2015-04-10 13:38:27 -0700 | [diff] [blame] | 193 | registerGlyphs: registerGlyphs, |
| 194 | registerGlyphSet: registerGlyphSet, |
Simon Hunt | 51fc40b | 2015-01-06 13:56:12 -0800 | [diff] [blame] | 195 | ids: ids, |
Simon Hunt | 6e45980 | 2015-01-06 15:05:42 -0800 | [diff] [blame] | 196 | glyph: glyph, |
Simon Hunt | 044f2807 | 2015-10-08 12:38:16 -0700 | [diff] [blame] | 197 | glyphDefined: glyphDefined, |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 198 | loadDefs: loadDefs, |
| 199 | addGlyph: addGlyph |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 200 | }; |
Simon Hunt | 4afa61d | 2016-02-23 09:31:43 -0800 | [diff] [blame] | 201 | return api; |
Simon Hunt | c9b7316 | 2015-01-29 14:02:15 -0800 | [diff] [blame] | 202 | }] |
Simon Hunt | 72e44bf | 2015-07-21 21:34:20 -0700 | [diff] [blame] | 203 | ) |
| 204 | .run(['$log', function ($log) { |
| 205 | $log.debug('Clearing glyph cache'); |
| 206 | clear(); |
| 207 | }]); |
Simon Hunt | 7ac7be9 | 2015-01-06 10:47:56 -0800 | [diff] [blame] | 208 | |
| 209 | }()); |