Committed by
Gerrit Code Review
GUI -- ButtonService - modified IconService to load glyphs without icon mapping
- created button and toggle functions in ButtonService with unit tests Change-Id: If4d35e3ed7df8c1b8f7355f63f39d0c5d84db753
Showing
6 changed files
with
253 additions
and
12 deletions
... | @@ -52,16 +52,16 @@ | ... | @@ -52,16 +52,16 @@ |
52 | } | 52 | } |
53 | 53 | ||
54 | // div is a D3 selection of the <DIV> element into which icon should load | 54 | // div is a D3 selection of the <DIV> element into which icon should load |
55 | - // iconCls is the CSS class used to identify the icon | 55 | + // glyphId identifies the glyph to use |
56 | // size is dimension of icon in pixels. Defaults to 20. | 56 | // size is dimension of icon in pixels. Defaults to 20. |
57 | // installGlyph, if truthy, will cause the glyph to be added to | 57 | // installGlyph, if truthy, will cause the glyph to be added to |
58 | // well-known defs element. Defaults to false. | 58 | // well-known defs element. Defaults to false. |
59 | // svgClass is the CSS class used to identify the SVG layer. | 59 | // svgClass is the CSS class used to identify the SVG layer. |
60 | // Defaults to 'embeddedIcon'. | 60 | // Defaults to 'embeddedIcon'. |
61 | - function loadIcon(div, iconCls, size, installGlyph, svgClass) { | 61 | + function loadIcon(div, glyphId, size, installGlyph, svgClass) { |
62 | var dim = size || 20, | 62 | var dim = size || 20, |
63 | svgCls = svgClass || 'embeddedIcon', | 63 | svgCls = svgClass || 'embeddedIcon', |
64 | - gid = glyphMapping[iconCls] || 'unknown', | 64 | + gid = glyphId || 'unknown', |
65 | svg, g; | 65 | svg, g; |
66 | 66 | ||
67 | if (installGlyph) { | 67 | if (installGlyph) { |
... | @@ -76,7 +76,7 @@ | ... | @@ -76,7 +76,7 @@ |
76 | }); | 76 | }); |
77 | 77 | ||
78 | g = svg.append('g').attr({ | 78 | g = svg.append('g').attr({ |
79 | - 'class': 'icon ' + iconCls | 79 | + 'class': 'icon' |
80 | }); | 80 | }); |
81 | 81 | ||
82 | g.append('rect').attr({ | 82 | g.append('rect').attr({ |
... | @@ -91,10 +91,22 @@ | ... | @@ -91,10 +91,22 @@ |
91 | 'class': 'glyph', | 91 | 'class': 'glyph', |
92 | 'xlink:href': '#' + gid | 92 | 'xlink:href': '#' + gid |
93 | }); | 93 | }); |
94 | -} | 94 | + } |
95 | + | ||
96 | + // div is a D3 selection of the <DIV> element into which icon should load | ||
97 | + // iconCls is the CSS class used to identify the icon | ||
98 | + // size is dimension of icon in pixels. Defaults to 20. | ||
99 | + // installGlyph, if truthy, will cause the glyph to be added to | ||
100 | + // well-known defs element. Defaults to false. | ||
101 | + // svgClass is the CSS class used to identify the SVG layer. | ||
102 | + // Defaults to 'embeddedIcon'. | ||
103 | + function loadIconByClass(div, iconCls, size, installGlyph, svgClass) { | ||
104 | + loadIcon(div, glyphMapping[iconCls], size, installGlyph, svgClass); | ||
105 | + div.select('svg g').classed(iconCls, true); | ||
106 | + } | ||
95 | 107 | ||
96 | function loadEmbeddedIcon(div, iconCls, size) { | 108 | function loadEmbeddedIcon(div, iconCls, size) { |
97 | - loadIcon(div, iconCls, size, true); | 109 | + loadIconByClass(div, iconCls, size, true); |
98 | } | 110 | } |
99 | 111 | ||
100 | 112 | ||
... | @@ -198,6 +210,7 @@ | ... | @@ -198,6 +210,7 @@ |
198 | 210 | ||
199 | return { | 211 | return { |
200 | loadIcon: loadIcon, | 212 | loadIcon: loadIcon, |
213 | + loadIconByClass: loadIconByClass, | ||
201 | loadEmbeddedIcon: loadEmbeddedIcon, | 214 | loadEmbeddedIcon: loadEmbeddedIcon, |
202 | addDeviceIcon: addDeviceIcon, | 215 | addDeviceIcon: addDeviceIcon, |
203 | addHostIcon: addHostIcon, | 216 | addHostIcon: addHostIcon, | ... | ... |
1 | +/* | ||
2 | + * Copyright 2015 Open Networking Laboratory | ||
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 -- Widget -- Button Service | ||
19 | + */ | ||
20 | +(function () { | ||
21 | + 'use strict'; | ||
22 | + | ||
23 | + var $log, fs, is; | ||
24 | + | ||
25 | + var btnSize = 30; | ||
26 | + | ||
27 | + function noop() {} | ||
28 | + | ||
29 | + function createDiv(div, cls, id) { | ||
30 | + return div.append('div') | ||
31 | + .classed(cls, true) | ||
32 | + .attr('id', id); | ||
33 | + } | ||
34 | + | ||
35 | + function button(div, id, gid, cb, tooltip) { | ||
36 | + if (!div) { | ||
37 | + $log.warn('Button cannot append to div'); | ||
38 | + return null; | ||
39 | + } | ||
40 | + | ||
41 | + var btnDiv = createDiv(div, 'btn', id), | ||
42 | + svg = btnDiv.append('svg'), | ||
43 | + cbFnc = fs.isF(cb) || noop; | ||
44 | + | ||
45 | + is.loadIcon(btnDiv, gid, btnSize); | ||
46 | + | ||
47 | + btnDiv.on('click', cbFnc); | ||
48 | + | ||
49 | + return { | ||
50 | + id: id, | ||
51 | + click: cbFnc, | ||
52 | + el: btnDiv | ||
53 | + } | ||
54 | + } | ||
55 | + | ||
56 | + function toggle(div, id, gid, cb, tooltip) { | ||
57 | + if (!div) { | ||
58 | + $log.warn('Toggle cannot append to div'); | ||
59 | + return null; | ||
60 | + } | ||
61 | + | ||
62 | + var sel = false, | ||
63 | + togDiv = createDiv(div, 'tog', id), | ||
64 | + svg = togDiv.append('svg'), | ||
65 | + cbFnc = fs.isF(cb) || noop; | ||
66 | + | ||
67 | + is.loadIcon(togDiv, gid, btnSize); | ||
68 | + | ||
69 | + return { | ||
70 | + id: id, | ||
71 | + el: togDiv, | ||
72 | + selected: function () { return sel; }, | ||
73 | + toggle: function (b) { | ||
74 | + if (b === undefined) { | ||
75 | + sel = !sel; | ||
76 | + } else { | ||
77 | + sel = !!b; | ||
78 | + } | ||
79 | + cbFnc(sel); | ||
80 | + } | ||
81 | + } | ||
82 | + } | ||
83 | + | ||
84 | + function radioSet(div, id, rset) { | ||
85 | + return {} | ||
86 | + } | ||
87 | + | ||
88 | + angular.module('onosWidget') | ||
89 | + .factory('ButtonService', ['$log', 'FnService', 'IconService', | ||
90 | + function (_$log_, _fs_, _is_) { | ||
91 | + $log = _$log_; | ||
92 | + fs = _fs_; | ||
93 | + is = _is_; | ||
94 | + | ||
95 | + return { | ||
96 | + button: button, | ||
97 | + toggle: toggle, | ||
98 | + radioSet: radioSet | ||
99 | + }; | ||
100 | + }]); | ||
101 | + | ||
102 | +}()); | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
... | @@ -102,7 +102,7 @@ | ... | @@ -102,7 +102,7 @@ |
102 | return null; | 102 | return null; |
103 | } | 103 | } |
104 | 104 | ||
105 | - for(var i = 0; i < tools.length; i += 1) { | 105 | + for (var i = 0; i < tools.length; i += 1) { |
106 | if (tools[i].t === 'rad' || tools[i].t === 'sep') { | 106 | if (tools[i].t === 'rad' || tools[i].t === 'sep') { |
107 | continue; | 107 | continue; |
108 | } else { | 108 | } else { | ... | ... |
... | @@ -59,6 +59,7 @@ | ... | @@ -59,6 +59,7 @@ |
59 | <script src="fw/widget/widget.js"></script> | 59 | <script src="fw/widget/widget.js"></script> |
60 | <script src="fw/widget/table.js"></script> | 60 | <script src="fw/widget/table.js"></script> |
61 | <script src="fw/widget/toolbar.js"></script> | 61 | <script src="fw/widget/toolbar.js"></script> |
62 | + <script src="fw/widget/button.js"></script> | ||
62 | 63 | ||
63 | <script src="fw/layer/layer.js"></script> | 64 | <script src="fw/layer/layer.js"></script> |
64 | <script src="fw/layer/panel.js"></script> | 65 | <script src="fw/layer/panel.js"></script> | ... | ... |
... | @@ -75,31 +75,31 @@ describe('factory: fw/svg/icon.js', function() { | ... | @@ -75,31 +75,31 @@ describe('factory: fw/svg/icon.js', function() { |
75 | 75 | ||
76 | it('should load an icon into a div', function () { | 76 | it('should load an icon into a div', function () { |
77 | expect(d3Elem.html()).toEqual(''); | 77 | expect(d3Elem.html()).toEqual(''); |
78 | - is.loadIcon(d3Elem, 'deviceOnline'); | 78 | + is.loadIconByClass(d3Elem, 'deviceOnline'); |
79 | verifyIconStructure('deviceOnline', '#checkMark'); | 79 | verifyIconStructure('deviceOnline', '#checkMark'); |
80 | }); | 80 | }); |
81 | 81 | ||
82 | it('should allow us to specify the icon size', function () { | 82 | it('should allow us to specify the icon size', function () { |
83 | expect(d3Elem.html()).toEqual(''); | 83 | expect(d3Elem.html()).toEqual(''); |
84 | - is.loadIcon(d3Elem, 'deviceOffline', 32); | 84 | + is.loadIconByClass(d3Elem, 'deviceOffline', 32); |
85 | verifyIconStructure('deviceOffline', '#xMark', '32'); | 85 | verifyIconStructure('deviceOffline', '#xMark', '32'); |
86 | }); | 86 | }); |
87 | 87 | ||
88 | it('should verify triangleUp icon', function () { | 88 | it('should verify triangleUp icon', function () { |
89 | expect(d3Elem.html()).toEqual(''); | 89 | expect(d3Elem.html()).toEqual(''); |
90 | - is.loadIcon(d3Elem, 'tableColSortAsc', 10); | 90 | + is.loadIconByClass(d3Elem, 'tableColSortAsc', 10); |
91 | verifyIconStructure('tableColSortAsc', '#triangleUp', '10'); | 91 | verifyIconStructure('tableColSortAsc', '#triangleUp', '10'); |
92 | }); | 92 | }); |
93 | 93 | ||
94 | it('should verify triangleDown icon', function () { | 94 | it('should verify triangleDown icon', function () { |
95 | expect(d3Elem.html()).toEqual(''); | 95 | expect(d3Elem.html()).toEqual(''); |
96 | - is.loadIcon(d3Elem, 'tableColSortDesc', 10); | 96 | + is.loadIconByClass(d3Elem, 'tableColSortDesc', 10); |
97 | verifyIconStructure('tableColSortDesc', '#triangleDown', '10'); | 97 | verifyIconStructure('tableColSortDesc', '#triangleDown', '10'); |
98 | }); | 98 | }); |
99 | 99 | ||
100 | it('should verify no icon is displayed', function () { | 100 | it('should verify no icon is displayed', function () { |
101 | expect(d3Elem.html()).toEqual(''); | 101 | expect(d3Elem.html()).toEqual(''); |
102 | - is.loadIcon(d3Elem, 'tableColSortNone', 10); | 102 | + is.loadIconByClass(d3Elem, 'tableColSortNone', 10); |
103 | verifyIconStructure('tableColSortNone', null, '10'); | 103 | verifyIconStructure('tableColSortNone', null, '10'); |
104 | }); | 104 | }); |
105 | 105 | ... | ... |
1 | +/* | ||
2 | + * Copyright 2015 Open Networking Laboratory | ||
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 -- Widget -- Button Service - Unit Tests | ||
19 | + */ | ||
20 | +describe('factory: fw/widget/button.js', function () { | ||
21 | + var $log, fs, bns, gs, | ||
22 | + d3Elem; | ||
23 | + | ||
24 | + beforeEach(module('onosWidget', 'onosSvg')); | ||
25 | + | ||
26 | + beforeEach(inject(function (_$log_, FnService, | ||
27 | + ButtonService, GlyphService) { | ||
28 | + $log = _$log_; | ||
29 | + fs = FnService; | ||
30 | + bns = ButtonService; | ||
31 | + gs = GlyphService; | ||
32 | + })); | ||
33 | + | ||
34 | + beforeEach(function () { | ||
35 | + gs.init(); | ||
36 | + d3Elem = d3.select('body').append('div').attr('id', 'testToolbar'); | ||
37 | + }); | ||
38 | + | ||
39 | + afterEach(function () { | ||
40 | + d3.select('#testToolbar').remove(); | ||
41 | + }); | ||
42 | + | ||
43 | + it('should define ButtonService', function () { | ||
44 | + expect(bns).toBeDefined(); | ||
45 | + }); | ||
46 | + | ||
47 | + it('should define api functions', function () { | ||
48 | + expect(fs.areFunctions(bns, [ | ||
49 | + 'button', 'toggle', 'radioSet' | ||
50 | + ])).toBeTruthy(); | ||
51 | + }); | ||
52 | + | ||
53 | + it('should verify button glyph', function () { | ||
54 | + var btn = bns.button(d3Elem, 'tbar0-btn-0', 'crown', function () {}); | ||
55 | + expect((btn.el).classed('btn')).toBeTruthy(); | ||
56 | + expect((btn.el).attr('id')).toBe('tbar0-btn-0'); | ||
57 | + expect((btn.el).select('svg')).toBeTruthy(); | ||
58 | + expect((btn.el).select('use')).toBeTruthy(); | ||
59 | + expect((btn.el).select('use').classed('glyph')).toBeTruthy(); | ||
60 | + expect((btn.el).select('use').attr('xlink:href')).toBe('#crown'); | ||
61 | + }); | ||
62 | + | ||
63 | + it('should not append button to an undefined div', function () { | ||
64 | + spyOn($log, 'warn'); | ||
65 | + expect(bns.button(undefined, 'id', 'gid', function () {})).toBeNull(); | ||
66 | + expect($log.warn).toHaveBeenCalledWith('Button cannot append to div'); | ||
67 | + }); | ||
68 | + | ||
69 | + it('should verify button callback', function () { | ||
70 | + var count = 0; | ||
71 | + function cb() { count++; } | ||
72 | + var btn = bns.button(d3Elem, 'test', 'nothing', cb); | ||
73 | + expect(count).toBe(0); | ||
74 | + btn.click(); | ||
75 | + expect(count).toBe(1); | ||
76 | + }); | ||
77 | + | ||
78 | + it('should ignore non-function callbacks button', function () { | ||
79 | + var count = 0; | ||
80 | + var btn = bns.button(d3Elem, 'test', 'nothing', 'foo'); | ||
81 | + expect(count).toBe(0); | ||
82 | + btn.click(); | ||
83 | + expect(count).toBe(0); | ||
84 | + }); | ||
85 | + | ||
86 | + it('should verify toggle glyph', function () { | ||
87 | + var tog = bns.toggle(d3Elem, 'tbar0-tog-0', 'crown', function () {}); | ||
88 | + expect((tog.el).classed('tog')).toBeTruthy(); | ||
89 | + expect((tog.el).attr('id')).toBe('tbar0-tog-0'); | ||
90 | + expect((tog.el).select('svg')).toBeTruthy(); | ||
91 | + expect((tog.el).select('use')).toBeTruthy(); | ||
92 | + expect((tog.el).select('use').classed('glyph')).toBeTruthy(); | ||
93 | + expect((tog.el).select('use').attr('xlink:href')).toBe('#crown'); | ||
94 | + }); | ||
95 | + | ||
96 | + it('should toggle the selected state', function () { | ||
97 | + var tog = bns.toggle(d3Elem, 'test', 'nothing'); | ||
98 | + expect(tog.selected()).toBe(false); | ||
99 | + tog.toggle(); | ||
100 | + expect(tog.selected()).toBe(true); | ||
101 | + tog.toggle(); | ||
102 | + expect(tog.selected()).toBe(false); | ||
103 | + }); | ||
104 | + | ||
105 | + it('should set toggle state', function () { | ||
106 | + var tog = bns.toggle(d3Elem, 'test', 'nothing'); | ||
107 | + tog.toggle(true); | ||
108 | + expect(tog.selected()).toBe(true); | ||
109 | + tog.toggle(); | ||
110 | + expect(tog.selected()).toBe(false); | ||
111 | + tog.toggle('truthy string'); | ||
112 | + expect(tog.selected()).toBe(true); | ||
113 | + tog.toggle(null); | ||
114 | + expect(tog.selected()).toBe(false); | ||
115 | + tog.toggle(''); | ||
116 | + expect(tog.selected()).toBe(false); | ||
117 | + }); | ||
118 | + | ||
119 | + it('should not append toggle to an undefined div', function () { | ||
120 | + spyOn($log, 'warn'); | ||
121 | + expect(bns.toggle(undefined, 'id', 'gid', function () {})).toBeNull(); | ||
122 | + expect($log.warn).toHaveBeenCalledWith('Toggle cannot append to div'); | ||
123 | + }); | ||
124 | + | ||
125 | +}); |
-
Please register or login to post a comment