Simon Hunt

GUI -- implemeted GlyphService.loadDefs().

Change-Id: I2d93164dd6d1968982d3034b8c7bc50041d9bad9
...@@ -161,9 +161,17 @@ ...@@ -161,9 +161,17 @@
161 return glyphs.get(id); 161 return glyphs.get(id);
162 } 162 }
163 163
164 + // Note: defs should be a D3 selection of a single <defs> element
164 function loadDefs(defs) { 165 function loadDefs(defs) {
165 - // TODO: clear defs element, then load all glyph definitions 166 + // remove all existing content
166 - 167 + defs.html(null);
168 +
169 + // load up the currently registered glyphs
170 + glyphs.values().forEach(function (g) {
171 + defs.append('symbol')
172 + .attr({ id: g.id, viewBox: g.vb })
173 + .append('path').attr('d', g.d);
174 + });
167 } 175 }
168 176
169 return { 177 return {
......
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
20 @author Simon Hunt 20 @author Simon Hunt
21 */ 21 */
22 describe('factory: fw/svg/glyph.js', function() { 22 describe('factory: fw/svg/glyph.js', function() {
23 - var $log, fs, gs; 23 + var $log, fs, gs, d3Elem;
24 24
25 var numBaseGlyphs = 11, 25 var numBaseGlyphs = 11,
26 vbBird = '352 224 113 112', 26 vbBird = '352 224 113 112',
...@@ -33,8 +33,13 @@ describe('factory: fw/svg/glyph.js', function() { ...@@ -33,8 +33,13 @@ describe('factory: fw/svg/glyph.js', function() {
33 $log = _$log_; 33 $log = _$log_;
34 fs = FnService; 34 fs = FnService;
35 gs = GlyphService; 35 gs = GlyphService;
36 + d3Elem = d3.select('body').append('defs').attr('id', 'myDefs');
36 })); 37 }));
37 38
39 + afterEach(function () {
40 + d3.select('#myDefs').remove();
41 + });
42 +
38 it('should define GlyphService', function () { 43 it('should define GlyphService', function () {
39 expect(gs).toBeDefined(); 44 expect(gs).toBeDefined();
40 }); 45 });
...@@ -168,4 +173,35 @@ describe('factory: fw/svg/glyph.js', function() { ...@@ -168,4 +173,35 @@ describe('factory: fw/svg/glyph.js', function() {
168 verifyGlyphLoaded('router', testVbox, 'M.5,.2'); 173 verifyGlyphLoaded('router', testVbox, 'M.5,.2');
169 verifyGlyphLoaded('switch', testVbox, 'M.2,.5'); 174 verifyGlyphLoaded('switch', testVbox, 'M.2,.5');
170 }); 175 });
176 +
177 + function verifyPathPrefix(elem, prefix) {
178 + var plen = prefix.length,
179 + d = elem.select('path').attr('d');
180 + expect(d.slice(0, plen)).toEqual(prefix);
181 + }
182 +
183 + it('should load base glyphs into the DOM', function () {
184 + gs.init();
185 + gs.loadDefs(d3Elem);
186 + expect(d3Elem.selectAll('symbol').size()).toEqual(numBaseGlyphs);
187 +
188 + // verify bgpSpeaker
189 + var bs = d3Elem.select('#bgpSpeaker');
190 + expect(bs.size()).toEqual(1);
191 + expect(bs.attr('viewBox')).toEqual(vbGlyph);
192 + verifyPathPrefix(bs, 'M10,40a45,35');
193 + });
194 +
195 + it('should load custom glyphs into the DOM', function () {
196 + gs.init();
197 + gs.register(testVbox, newGlyphs);
198 + gs.loadDefs(d3Elem);
199 + expect(d3Elem.selectAll('symbol').size()).toEqual(numBaseGlyphs + 2);
200 +
201 + // verify diamond
202 + var dia = d3Elem.select('#diamond');
203 + expect(dia.size()).toEqual(1);
204 + expect(dia.attr('viewBox')).toEqual(testVbox);
205 + verifyPathPrefix(dia, 'M.2,.5l.3,-.3');
206 + });
171 }); 207 });
......