Simon Hunt

GUI -- implemeted GlyphService.register().

Change-Id: I769294b428d9eb5414b45d1099baee1ab3814528
...@@ -23,7 +23,8 @@ ...@@ -23,7 +23,8 @@
23 'use strict'; 23 'use strict';
24 24
25 var $log, 25 var $log,
26 - glyphs = d3.map(); 26 + glyphs = d3.map(),
27 + msgGS = 'GlyphService.';
27 28
28 // ---------------------------------------------------------------------- 29 // ----------------------------------------------------------------------
29 // Base set of Glyphs... 30 // Base set of Glyphs...
...@@ -119,13 +120,6 @@ ...@@ -119,13 +120,6 @@
119 120
120 // ---------------------------------------------------------------------- 121 // ----------------------------------------------------------------------
121 122
122 - function reg(vbox, data) {
123 - d3.map(data).keys().forEach(function (key) {
124 - glyphs.set(key, {id: key, vb: vbox, d: data[key]});
125 - });
126 - }
127 -
128 -
129 angular.module('onosSvg') 123 angular.module('onosSvg')
130 .factory('GlyphService', ['$log', function (_$log_) { 124 .factory('GlyphService', ['$log', function (_$log_) {
131 $log = _$log_; 125 $log = _$log_;
...@@ -133,14 +127,30 @@ ...@@ -133,14 +127,30 @@
133 function init() { 127 function init() {
134 // start with a fresh map 128 // start with a fresh map
135 glyphs = d3.map(); 129 glyphs = d3.map();
136 - reg(birdViewBox, birdData); 130 + register(birdViewBox, birdData);
137 - reg(glyphViewBox, glyphData); 131 + register(glyphViewBox, glyphData);
138 - reg(badgeViewBox, badgeData); 132 + register(badgeViewBox, badgeData);
139 } 133 }
140 134
141 function register(viewBox, data, overwrite) { 135 function register(viewBox, data, overwrite) {
142 - // TODO: register specified glyph definitions 136 + var dmap = d3.map(data),
143 - 137 + dups = [],
138 + ok, msg;
139 +
140 + dmap.forEach(function (key, value) {
141 + if (!overwrite && glyphs.get(key)) {
142 + dups.push(key);
143 + } else {
144 + glyphs.set(key, {id: key, vb: viewBox, d: value});
145 + }
146 + });
147 + ok = (dups.length == 0);
148 + if (!ok) {
149 + dups.forEach(function (id) {
150 + $log.warn(msgGS + 'register(): ID collision: "'+id+'"');
151 + });
152 + }
153 + return ok;
144 } 154 }
145 155
146 function ids() { 156 function ids() {
......
...@@ -22,7 +22,8 @@ ...@@ -22,7 +22,8 @@
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;
24 24
25 - var vbBird = '352 224 113 112', 25 + var numBaseGlyphs = 11,
26 + vbBird = '352 224 113 112',
26 vbGlyph = '0 0 110 110', 27 vbGlyph = '0 0 110 110',
27 vbBadge = '0 0 10 10'; 28 vbBadge = '0 0 10 10';
28 29
...@@ -50,7 +51,7 @@ describe('factory: fw/svg/glyph.js', function() { ...@@ -50,7 +51,7 @@ describe('factory: fw/svg/glyph.js', function() {
50 51
51 it('should load the base set of glyphs', function () { 52 it('should load the base set of glyphs', function () {
52 gs.init(); 53 gs.init();
53 - expect(gs.ids().length).toEqual(11); 54 + expect(gs.ids().length).toEqual(numBaseGlyphs);
54 }); 55 });
55 56
56 function verifyGlyphLoaded(id, vbox, prefix) { 57 function verifyGlyphLoaded(id, vbox, prefix) {
...@@ -107,4 +108,64 @@ describe('factory: fw/svg/glyph.js', function() { ...@@ -107,4 +108,64 @@ describe('factory: fw/svg/glyph.js', function() {
107 gs.init(); 108 gs.init();
108 verifyGlyphLoaded('uiAttached', vbBadge, 'M2,2.5a.5,.5'); 109 verifyGlyphLoaded('uiAttached', vbBadge, 'M2,2.5a.5,.5');
109 }); 110 });
111 +
112 + // define some glyphs that we want to install
113 +
114 + var testVbox = '0 0 1 1',
115 + dTriangle = 'M.5,.2l.3,.6,h-.6z',
116 + dDiamond = 'M.2,.5l.3,-.3l.3,.3l-.3,.3z',
117 + newGlyphs = {
118 + triangle: dTriangle,
119 + diamond: dDiamond
120 + },
121 + dupGlyphs = {
122 + router: dTriangle,
123 + switch: dDiamond
124 + },
125 + idCollision = 'GlyphService.register(): ID collision: ';
126 +
127 + it('should install new glyphs', function () {
128 + gs.init();
129 + expect(gs.ids().length).toEqual(numBaseGlyphs);
130 + spyOn($log, 'warn');
131 +
132 + var ok = gs.register(testVbox, newGlyphs);
133 + expect(ok).toBeTruthy();
134 + expect($log.warn).not.toHaveBeenCalled();
135 +
136 + expect(gs.ids().length).toEqual(numBaseGlyphs + 2);
137 + verifyGlyphLoaded('triangle', testVbox, 'M.5,.2');
138 + verifyGlyphLoaded('diamond', testVbox, 'M.2,.5');
139 + });
140 +
141 + it('should not overwrite glyphs with dup IDs', function () {
142 + gs.init();
143 + expect(gs.ids().length).toEqual(numBaseGlyphs);
144 + spyOn($log, 'warn');
145 +
146 + var ok = gs.register(testVbox, dupGlyphs);
147 + expect(ok).toBeFalsy();
148 + expect($log.warn).toHaveBeenCalledWith(idCollision + '"switch"');
149 + expect($log.warn).toHaveBeenCalledWith(idCollision + '"router"');
150 +
151 + expect(gs.ids().length).toEqual(numBaseGlyphs);
152 + // verify original glyphs still exist...
153 + verifyGlyphLoaded('router', vbGlyph, 'M10,55A45,45');
154 + verifyGlyphLoaded('switch', vbGlyph, 'M10,20a10');
155 + });
156 +
157 + it('should replace glyphs if asked nicely', function () {
158 + gs.init();
159 + expect(gs.ids().length).toEqual(numBaseGlyphs);
160 + spyOn($log, 'warn');
161 +
162 + var ok = gs.register(testVbox, dupGlyphs, true);
163 + expect(ok).toBeTruthy();
164 + expect($log.warn).not.toHaveBeenCalled();
165 +
166 + expect(gs.ids().length).toEqual(numBaseGlyphs);
167 + // verify glyphs have been overwritten...
168 + verifyGlyphLoaded('router', testVbox, 'M.5,.2');
169 + verifyGlyphLoaded('switch', testVbox, 'M.2,.5');
170 + });
110 }); 171 });
......