Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 1 | /* |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 2 | * Copyright 2018-present Open Networking Foundation |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [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 | import { Injectable } from '@angular/core'; |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 17 | import { FnService } from '../util/fn.service'; |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 18 | import { LogService } from '../log.service'; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 19 | import * as gds from './glyphdata.service'; |
| 20 | import * as d3 from 'd3'; |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 21 | import { SvgUtilService } from './svgutil.service'; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 22 | |
| 23 | // constants |
| 24 | const msgGS = 'GlyphService.'; |
| 25 | const rg = 'registerGlyphs(): '; |
| 26 | const rgs = 'registerGlyphSet(): '; |
| 27 | |
| 28 | /** |
| 29 | * ONOS GUI -- SVG -- Glyph Service |
| 30 | */ |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 31 | @Injectable({ |
| 32 | providedIn: 'root', |
| 33 | }) |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 34 | export class GlyphService { |
| 35 | // internal state |
| 36 | glyphs = d3.map(); |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 37 | api: Object; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 38 | |
| 39 | constructor( |
| 40 | private fs: FnService, |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 41 | // private gd: GlyphDataService, |
| 42 | private log: LogService, |
| 43 | private sus: SvgUtilService |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 44 | ) { |
| 45 | this.clear(); |
| 46 | this.init(); |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 47 | this.api = { |
| 48 | registerGlyphs: this.registerGlyphs, |
| 49 | registerGlyphSet: this.registerGlyphSet, |
| 50 | ids: this.ids, |
| 51 | glyph: this.glyph, |
| 52 | glyphDefined: this.glyphDefined, |
| 53 | loadDefs: this.loadDefs, |
| 54 | addGlyph: this.addGlyph, |
| 55 | }; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 56 | this.log.debug('GlyphService constructed'); |
| 57 | } |
| 58 | |
| 59 | warn(msg: string): void { |
| 60 | this.log.warn(msgGS + msg); |
| 61 | } |
| 62 | |
| 63 | addToMap(key, value, vbox, overwrite: boolean, dups) { |
| 64 | if (!overwrite && this.glyphs.get(key)) { |
| 65 | dups.push(key); |
| 66 | } else { |
| 67 | this.glyphs.set(key, { id: key, vb: vbox, d: value }); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | reportDups(dups: string[], which: string): boolean { |
| 72 | const ok: boolean = (dups.length === 0); |
| 73 | const msg = 'ID collision: '; |
| 74 | |
| 75 | if (!ok) { |
| 76 | dups.forEach((id) => { |
| 77 | this.warn(which + msg + '"' + id + '"'); |
| 78 | }); |
| 79 | } |
| 80 | return ok; |
| 81 | } |
| 82 | |
| 83 | reportMissVb(missing: string[], which: string): boolean { |
| 84 | const ok: boolean = (missing.length === 0); |
| 85 | const msg = 'Missing viewbox property: '; |
| 86 | |
| 87 | if (!ok) { |
| 88 | missing.forEach((vbk) => { |
| 89 | this.warn(which + msg + '"' + vbk + '"'); |
| 90 | }); |
| 91 | } |
| 92 | return ok; |
| 93 | } |
| 94 | |
| 95 | clear() { |
| 96 | // start with a fresh map |
| 97 | this.glyphs = d3.map(); |
| 98 | } |
| 99 | |
| 100 | init() { |
| 101 | this.log.info('Registering glyphs'); |
| 102 | this.registerGlyphs(gds.logos); |
| 103 | this.registerGlyphSet(gds.glyphDataSet); |
| 104 | this.registerGlyphSet(gds.badgeDataSet); |
| 105 | this.registerGlyphs(gds.spriteData); |
| 106 | this.registerGlyphSet(gds.mojoDataSet); |
| 107 | this.registerGlyphs(gds.extraGlyphs); |
| 108 | } |
| 109 | |
| 110 | registerGlyphs(data: Map<string, string>, overwrite: boolean = false): boolean { |
| 111 | const dups: string[] = []; |
| 112 | const missvb: string[] = []; |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 113 | for (const [key, value] of data.entries()) { |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 114 | const vbk = '_' + key; |
| 115 | const vb = data.get(vbk); |
| 116 | |
| 117 | if (key[0] !== '_') { |
| 118 | if (!vb) { |
| 119 | missvb.push(vbk); |
| 120 | continue; |
| 121 | } |
| 122 | this.addToMap(key, value, vb, overwrite, dups); |
| 123 | } |
| 124 | } |
| 125 | return this.reportDups(dups, rg) && this.reportMissVb(missvb, rg); |
| 126 | } |
| 127 | |
| 128 | registerGlyphSet(data: Map<string, string>, overwrite: boolean = false): boolean { |
| 129 | const dups: string[] = []; |
| 130 | const vb: string = data.get('_viewbox'); |
| 131 | |
| 132 | if (!vb) { |
| 133 | this.warn(rgs + 'no "_viewbox" property found'); |
| 134 | return false; |
| 135 | } |
| 136 | |
Sean Condon | 49e15be | 2018-05-16 16:58:29 +0100 | [diff] [blame] | 137 | for (const [key, value] of data.entries()) { |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 138 | // angular.forEach(data, function (value, key) { |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 139 | if (key[0] !== '_') { |
| 140 | this.addToMap(key, value, vb, overwrite, dups); |
| 141 | } |
| 142 | } |
| 143 | return this.reportDups(dups, rgs); |
| 144 | } |
| 145 | |
| 146 | ids() { |
| 147 | return this.glyphs.keys(); |
| 148 | } |
| 149 | |
| 150 | glyph(id) { |
| 151 | return this.glyphs.get(id); |
| 152 | } |
| 153 | |
| 154 | glyphDefined(id) { |
| 155 | return this.glyphs.has(id); |
| 156 | } |
| 157 | |
| 158 | |
| 159 | /** |
| 160 | * Load definitions of a glyph |
| 161 | * |
| 162 | * Note: defs should be a D3 selection of a single <defs> element |
| 163 | */ |
Sean Condon | 4747ece | 2019-05-04 20:17:02 +0100 | [diff] [blame] | 164 | loadDefs(defs, glyphIds: string[], noClear: boolean, asName?: string[]) { |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 165 | const list = this.fs.isA(glyphIds) || this.ids(); |
| 166 | |
| 167 | if (!noClear) { |
| 168 | // remove all existing content |
| 169 | defs.html(null); |
| 170 | } |
| 171 | |
| 172 | // load up the requested glyphs |
Sean Condon | 4747ece | 2019-05-04 20:17:02 +0100 | [diff] [blame] | 173 | list.forEach((id, idx) => { |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 174 | const g = this.glyph(id); |
Sean Condon | 4747ece | 2019-05-04 20:17:02 +0100 | [diff] [blame] | 175 | let asNameStr: string = asName[idx]; |
| 176 | if (!asNameStr) { |
| 177 | asNameStr = id; |
| 178 | } |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 179 | if (g) { |
| 180 | if (noClear) { |
| 181 | // quick exit if symbol is already present |
| 182 | // TODO: check if this should be a continue or break instead |
Sean Condon | 4747ece | 2019-05-04 20:17:02 +0100 | [diff] [blame] | 183 | if (defs.select('symbol#' + asNameStr).size() > 0) { |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 184 | return; |
| 185 | } |
| 186 | } |
| 187 | defs.append('symbol') |
Sean Condon | 4747ece | 2019-05-04 20:17:02 +0100 | [diff] [blame] | 188 | .attr('id', asNameStr) |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 189 | .attr('viewBox', g.vb) |
| 190 | .append('path') |
| 191 | .attr('d', g.d); |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 192 | } |
| 193 | }); |
| 194 | } |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 195 | |
| 196 | addGlyph(elem: any, glyphId: string, size: number, overlay: any, trans: any) { |
Sean Condon | 55c3053 | 2018-10-29 12:26:57 +0000 | [diff] [blame] | 197 | const sz = size || 40; |
| 198 | const ovr = !!overlay; |
| 199 | const xns = this.fs.isA(trans); |
| 200 | |
| 201 | const glyphUse = elem |
| 202 | .append('use') |
| 203 | .attr('width', sz) |
| 204 | .attr('height', sz) |
| 205 | .attr('class', 'glyph') |
| 206 | .attr('xlink:href', '#' + glyphId) |
| 207 | .classed('overlay', ovr); |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 208 | |
| 209 | if (xns) { |
Sean Condon | 55c3053 | 2018-10-29 12:26:57 +0000 | [diff] [blame] | 210 | glyphUse.attr('transform', this.sus.translate(trans)); |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 211 | } |
Sean Condon | 55c3053 | 2018-10-29 12:26:57 +0000 | [diff] [blame] | 212 | |
| 213 | return glyphUse; |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 214 | } |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 215 | } |