toolbar.js
7.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
* Copyright 2015-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
ONOS GUI -- Widget -- Toolbar Service
*/
// TODO: Augment service to allow toolbars to exist on right edge of screen
// TODO: also - make toolbar more object aware (rows etc.)
(function () {
'use strict';
// injected refs
var $log, fs, ps, bns, is;
// configuration
var arrowSize = 10,
sepWidth = 6,
defaultSettings = {
edge: 'left',
width: 20,
margin: 0,
hideMargin: -20,
top: 'auto',
bottom: '10px',
fade: false,
shown: false
};
// internal state
var tbars = {};
// === Helper functions --------------------------------------
// translate uses 50 because the svg viewbox is 50
function rotateArrowLeft(adiv) {
adiv.select('g')
.attr('transform', 'translate(0 50) rotate(-90)');
}
function rotateArrowRight(adiv) {
adiv.select('g')
.attr('transform', 'translate(50 0) rotate(90)');
}
function createArrow(panel) {
var arrowDiv = panel.append('div')
.classed('tbar-arrow', true);
is.loadIcon(arrowDiv, 'triangleUp', arrowSize, true);
return arrowDiv;
}
function warn(msg, id) {
$log.warn('createToolbar: ' + msg + ': [' + id + ']');
return null;
}
// ==================================
function createToolbar(id, opts) {
if (!id) return warn('no ID given');
if (tbars[id]) return warn('duplicate ID given', id);
var settings = angular.extend({}, defaultSettings, fs.isO(opts)),
items = {},
tbid = 'toolbar-' + id,
panel = ps.createPanel(tbid, settings),
arrowDiv = createArrow(panel),
currentRow = panel.append('div').classed('tbar-row', true),
rowButtonIds = [], // for removable buttons
tbWidth = arrowSize + 2, // empty toolbar width
maxWidth = panel.width();
arrowDiv.on('click', toggle);
// add a descriptor for this toolbar
tbars[id] = {
settings: settings,
items: items,
panel: panel,
panelId: tbid
};
panel.classed('toolbar', true)
.style('top', settings.top)
.style('bottom', settings.bottom);
// Helper functions
function dupId(id, caller) {
if (items[id]) {
$log.warn(caller + ': duplicate ID:', id);
return true;
}
return false;
}
function adjustWidth(btnWidth) {
if (fs.noPxStyle(currentRow, 'width') >= maxWidth) {
tbWidth += btnWidth;
maxWidth = tbWidth;
}
panel.width(tbWidth);
}
// API functions
function addButton(id, gid, cb, tooltip) {
if (dupId(id, 'addButton')) return null;
var bid = tbid + '-' + id,
btn = bns.button(currentRow, bid, gid, cb, tooltip);
items[id] = btn;
adjustWidth(btn.width());
return btn;
}
function addToggle(id, gid, initState, cb, tooltip) {
if (dupId(id, 'addToggle')) return null;
var tid = tbid + '-' + id,
tog = bns.toggle(currentRow, tid, gid, initState, cb, tooltip);
items[id] = tog;
adjustWidth(tog.width());
return tog;
}
function addRadioSet(id, rset) {
if (dupId(id, 'addRadioSet')) return null;
var rid = tbid + '-' + id,
rad = bns.radioSet(currentRow, rid, rset);
items[id] = rad;
adjustWidth(rad.width());
return rad;
}
function addSeparator() {
currentRow.append('div')
.classed('separator', true);
tbWidth += sepWidth;
}
function addRow() {
if (currentRow.select('div').empty()) {
return null;
} else {
panel.append('br');
currentRow = panel.append('div').classed('tbar-row', true);
// return API to allow caller more access to the row
return {
clear: rowClear,
setText: rowSetText,
addButton: rowAddButton,
classed: rowClassed
};
}
}
function rowClear() {
currentRow.selectAll('*').remove();
rowButtonIds.forEach(function (bid) {
delete items[bid];
});
rowButtonIds = [];
}
// installs a div with text into the button row
function rowSetText(text) {
rowClear();
currentRow.append('div').classed('tbar-row-text', true)
.html(text);
}
function rowAddButton(id, gid, cb, tooltip) {
var b = addButton(id, gid, cb, tooltip);
if (b) {
rowButtonIds.push(id);
}
}
function rowClassed(classes, bool) {
currentRow.classed(classes, bool);
}
function show(cb) {
rotateArrowLeft(arrowDiv);
panel.show(cb);
}
function hide(cb) {
rotateArrowRight(arrowDiv);
panel.hide(cb);
}
function toggle(cb) {
if (panel.isVisible()) {
hide(cb);
} else {
show(cb);
}
}
function isVisible() {
return panel.isVisible();
}
return {
addButton: addButton,
addToggle: addToggle,
addRadioSet: addRadioSet,
addSeparator: addSeparator,
addRow: addRow,
show: show,
hide: hide,
toggle: toggle,
isVisible: isVisible
};
}
function destroyToolbar(id) {
var tb = tbars[id];
delete tbars[id];
if (tb) {
ps.destroyPanel(tb.panelId);
}
}
// === Module Definition ===
angular.module('onosWidget')
.factory('ToolbarService',
['$log', 'FnService', 'PanelService', 'ButtonService', 'IconService',
function (_$log_, _fs_, _ps_, _bns_, _is_) {
$log = _$log_;
fs = _fs_;
ps = _ps_;
bns = _bns_;
is = _is_;
// this function is only used in testing
function init() {
tbars = {};
}
return {
init: init,
createToolbar: createToolbar,
destroyToolbar: destroyToolbar
};
}]);
}());