blob: a3339298cea31a4f2affafabe9736433e683d046 [file] [log] [blame]
Simon Hunt7ac7be92015-01-06 10:47:56 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
Simon Hunt7ac7be92015-01-06 10:47:56 -08003 *
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 Hunt7ac7be92015-01-06 10:47:56 -080019 */
20(function () {
21 'use strict';
22
Simon Huntc9b73162015-01-29 14:02:15 -080023 // injected references
Simon Hunt4afa61d2016-02-23 09:31:43 -080024 var $log, fs, sus, gd;
Simon Huntc9b73162015-01-29 14:02:15 -080025
26 // internal state
Simon Hunt4afa61d2016-02-23 09:31:43 -080027 var glyphs = d3.map(),
28 api;
Simon Hunt7ac7be92015-01-06 10:47:56 -080029
Simon Hunt4afa61d2016-02-23 09:31:43 -080030 // constants
Simon Hunt9c1c45e2015-04-10 13:38:27 -070031 var msgGS = 'GlyphService.',
Steven Burrows1c2a9682017-07-14 16:52:46 +010032 rg = 'registerGlyphs(): ',
33 rgs = 'registerGlyphSet(): ';
Simon Hunt9c1c45e2015-04-10 13:38:27 -070034
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 {
Steven Burrows1c2a9682017-07-14 16:52:46 +010045 glyphs.set(key, { id: key, vb: vbox, d: value });
Simon Hunt9c1c45e2015-04-10 13:38:27 -070046 }
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 Hunt6e459802015-01-06 15:05:42 -080075
Simon Huntc9b73162015-01-29 14:02:15 -080076 function clear() {
77 // start with a fresh map
78 glyphs = d3.map();
79 }
80
81 function init() {
Simon Hunt4afa61d2016-02-23 09:31:43 -080082 gd.registerCoreGlyphs(api);
Simon Huntc9b73162015-01-29 14:02:15 -080083 }
84
Simon Hunt9c1c45e2015-04-10 13:38:27 -070085 function registerGlyphs(data, overwrite) {
86 var dups = [],
87 missvb = [];
Simon Huntc9b73162015-01-29 14:02:15 -080088
Simon Hunt9c1c45e2015-04-10 13:38:27 -070089 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 Huntc9b73162015-01-29 14:02:15 -080099 }
100 });
Simon Hunt9c1c45e2015-04-10 13:38:27 -0700101 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 Huntc9b73162015-01-29 14:02:15 -0800111 }
Simon Hunt9c1c45e2015-04-10 13:38:27 -0700112
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 Huntc9b73162015-01-29 14:02:15 -0800119 }
120
121 function ids() {
122 return glyphs.keys();
123 }
124
125 function glyph(id) {
126 return glyphs.get(id);
127 }
128
Simon Hunt044f28072015-10-08 12:38:16 -0700129 function glyphDefined(id) {
130 return glyphs.has(id);
131 }
132
Simon Huntc9b73162015-01-29 14:02:15 -0800133 // 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 Huntedf5c0e2015-01-29 15:00:53 -0800160 // trans can specify translation [x,y]
Simon Huntc9b73162015-01-29 14:02:15 -0800161 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',
Steven Burrows1c2a9682017-07-14 16:52:46 +0100169 'xlink:href': '#' + glyphId,
Simon Huntc9b73162015-01-29 14:02:15 -0800170 };
171
172 if (xns) {
173 atr.transform = sus.translate(trans);
174 }
Simon Hunt4b668592015-01-29 17:33:53 -0800175 return elem.append('use').attr(atr).classed('overlay', ovr);
Simon Huntc9b73162015-01-29 14:02:15 -0800176 }
177
178 // ----------------------------------------------------------------------
179
Simon Hunt7ac7be92015-01-06 10:47:56 -0800180 angular.module('onosSvg')
Simon Huntc9b73162015-01-29 14:02:15 -0800181 .factory('GlyphService',
Simon Hunt4afa61d2016-02-23 09:31:43 -0800182 ['$log', 'FnService', 'SvgUtilService', 'GlyphDataService',
Simon Huntc9b73162015-01-29 14:02:15 -0800183
Simon Hunt4afa61d2016-02-23 09:31:43 -0800184 function (_$log_, _fs_, _sus_, _gd_) {
Simon Hunt7ac7be92015-01-06 10:47:56 -0800185 $log = _$log_;
Simon Hunt58f23bb2015-01-16 16:32:24 -0800186 fs = _fs_;
Simon Huntc9b73162015-01-29 14:02:15 -0800187 sus = _sus_;
Simon Hunt4afa61d2016-02-23 09:31:43 -0800188 gd = _gd_;
Simon Hunt51fc40b2015-01-06 13:56:12 -0800189
Simon Hunt4afa61d2016-02-23 09:31:43 -0800190 api = {
Simon Huntcacce342015-01-07 16:13:05 -0800191 clear: clear,
Simon Hunt51fc40b2015-01-06 13:56:12 -0800192 init: init,
Simon Hunt9c1c45e2015-04-10 13:38:27 -0700193 registerGlyphs: registerGlyphs,
194 registerGlyphSet: registerGlyphSet,
Simon Hunt51fc40b2015-01-06 13:56:12 -0800195 ids: ids,
Simon Hunt6e459802015-01-06 15:05:42 -0800196 glyph: glyph,
Simon Hunt044f28072015-10-08 12:38:16 -0700197 glyphDefined: glyphDefined,
Simon Huntc9b73162015-01-29 14:02:15 -0800198 loadDefs: loadDefs,
Steven Burrows1c2a9682017-07-14 16:52:46 +0100199 addGlyph: addGlyph,
Simon Hunt7ac7be92015-01-06 10:47:56 -0800200 };
Simon Hunt4afa61d2016-02-23 09:31:43 -0800201 return api;
Simon Huntc9b73162015-01-29 14:02:15 -0800202 }]
Simon Hunt72e44bf2015-07-21 21:34:20 -0700203 )
204 .run(['$log', function ($log) {
205 $log.debug('Clearing glyph cache');
206 clear();
207 }]);
Simon Hunt7ac7be92015-01-06 10:47:56 -0800208
209}());