blob: 3fe44970a70afc8f9653263f43f1e7fcf5c11523 [file] [log] [blame]
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -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 -- Widget -- Toolbar Service
19 */
Simon Huntdf510012015-02-26 16:34:37 -080020// TODO: Augment service to allow toolbars to exist on right edge of screen
Simon Hunt8d22c4b2015-08-06 16:24:43 -070021// TODO: also - make toolbar more object aware (rows etc.)
Simon Huntdf510012015-02-26 16:34:37 -080022
23
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -080024(function () {
25 'use strict';
26
Simon Huntdf510012015-02-26 16:34:37 -080027 // injected refs
Bri Prebilic Cole5000a752015-02-23 17:20:53 -080028 var $log, fs, ps, bns, is;
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -080029
Simon Huntdf510012015-02-26 16:34:37 -080030 // configuration
31 var arrowSize = 10,
32 sepWidth = 6,
Bri Prebilic Cole5000a752015-02-23 17:20:53 -080033 defaultSettings = {
34 edge: 'left',
Bri Prebilic Cole258f0462015-02-25 14:48:50 -080035 width: 20,
Bri Prebilic Colebe8b9a42015-02-24 12:12:00 -080036 margin: 0,
37 hideMargin: -20,
Bri Prebilic Coled8745462015-06-01 16:08:57 -070038 top: 'auto',
39 bottom: '10px',
Bri Prebilic Cole258f0462015-02-25 14:48:50 -080040 fade: false,
41 shown: false
Simon Huntdf510012015-02-26 16:34:37 -080042 };
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -080043
Simon Huntdf510012015-02-26 16:34:37 -080044 // internal state
45 var tbars = {};
46
Bri Prebilic Cole751804e2015-02-18 15:44:28 -080047
Bri Prebilic Cole5000a752015-02-23 17:20:53 -080048 // === Helper functions --------------------------------------
49
Bri Prebilic Colebe8b9a42015-02-24 12:12:00 -080050 // translate uses 50 because the svg viewbox is 50
Simon Huntdf510012015-02-26 16:34:37 -080051 function rotateArrowLeft(adiv) {
52 adiv.select('g')
Bri Prebilic Cole5000a752015-02-23 17:20:53 -080053 .attr('transform', 'translate(0 50) rotate(-90)');
54 }
Simon Huntdf510012015-02-26 16:34:37 -080055 function rotateArrowRight(adiv) {
56 adiv.select('g')
Bri Prebilic Colebe8b9a42015-02-24 12:12:00 -080057 .attr('transform', 'translate(50 0) rotate(90)');
Bri Prebilic Cole5000a752015-02-23 17:20:53 -080058 }
Simon Huntdf510012015-02-26 16:34:37 -080059
60 function createArrow(panel) {
61 var arrowDiv = panel.append('div')
Bri Prebilic Coledd805572015-08-04 16:54:08 -070062 .classed('tbar-arrow', true);
Simon Huntdf510012015-02-26 16:34:37 -080063 is.loadIcon(arrowDiv, 'triangleUp', arrowSize, true);
64 return arrowDiv;
Bri Prebilic Cole5000a752015-02-23 17:20:53 -080065 }
66
Simon Huntdf510012015-02-26 16:34:37 -080067 function warn(msg, id) {
68 $log.warn('createToolbar: ' + msg + ': [' + id + ']');
69 return null;
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -080070 }
71
Simon Huntdf510012015-02-26 16:34:37 -080072 // ==================================
Bri Prebilic Cole5000a752015-02-23 17:20:53 -080073
74 function createToolbar(id, opts) {
Simon Huntd3bcef32015-02-27 18:36:42 -080075 if (!id) return warn('no ID given');
Simon Huntdf510012015-02-26 16:34:37 -080076 if (tbars[id]) return warn('duplicate ID given', id);
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -080077
Simon Huntdf510012015-02-26 16:34:37 -080078 var settings = angular.extend({}, defaultSettings, fs.isO(opts)),
Simon Huntd3bcef32015-02-27 18:36:42 -080079 items = {},
Simon Huntdf510012015-02-26 16:34:37 -080080 tbid = 'toolbar-' + id,
81 panel = ps.createPanel(tbid, settings),
82 arrowDiv = createArrow(panel),
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -070083 currentRow = panel.append('div').classed('tbar-row', true),
Simon Hunt8d22c4b2015-08-06 16:24:43 -070084 rowButtonIds = [], // for removable buttons
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -070085 tbWidth = arrowSize + 2, // empty toolbar width
86 maxWidth = panel.width();
Simon Huntdf510012015-02-26 16:34:37 -080087
88 arrowDiv.on('click', toggle);
89
90 // add a descriptor for this toolbar
91 tbars[id] = {
92 settings: settings,
Simon Huntd3bcef32015-02-27 18:36:42 -080093 items: items,
Simon Huntdf510012015-02-26 16:34:37 -080094 panel: panel,
95 panelId: tbid
96 };
97
98 panel.classed('toolbar', true)
Bri Prebilic Coled8745462015-06-01 16:08:57 -070099 .style('top', settings.top)
100 .style('bottom', settings.bottom);
Bri Prebilic Cole5000a752015-02-23 17:20:53 -0800101
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700102 // Helper functions
Simon Huntdf510012015-02-26 16:34:37 -0800103
Simon Huntd3bcef32015-02-27 18:36:42 -0800104 function dupId(id, caller) {
105 if (items[id]) {
106 $log.warn(caller + ': duplicate ID:', id);
107 return true;
108 }
109 return false;
110 }
111
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700112 function adjustWidth(btnWidth) {
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -0700113 if (fs.noPxStyle(currentRow, 'width') >= maxWidth) {
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700114 tbWidth += btnWidth;
115 maxWidth = tbWidth;
116 }
117 panel.width(tbWidth);
118 }
119
Simon Huntdf510012015-02-26 16:34:37 -0800120 // API functions
121
122 function addButton(id, gid, cb, tooltip) {
Simon Huntd3bcef32015-02-27 18:36:42 -0800123 if (dupId(id, 'addButton')) return null;
124
Simon Huntdf510012015-02-26 16:34:37 -0800125 var bid = tbid + '-' + id,
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700126 btn = bns.button(currentRow, bid, gid, cb, tooltip);
Simon Huntd3bcef32015-02-27 18:36:42 -0800127
128 items[id] = btn;
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700129 adjustWidth(btn.width());
Simon Huntdf510012015-02-26 16:34:37 -0800130 return btn;
131 }
132
133 function addToggle(id, gid, initState, cb, tooltip) {
Simon Huntd3bcef32015-02-27 18:36:42 -0800134 if (dupId(id, 'addToggle')) return null;
135
Simon Huntdf510012015-02-26 16:34:37 -0800136 var tid = tbid + '-' + id,
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700137 tog = bns.toggle(currentRow, tid, gid, initState, cb, tooltip);
Simon Huntd3bcef32015-02-27 18:36:42 -0800138
139 items[id] = tog;
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700140 adjustWidth(tog.width());
Simon Huntdf510012015-02-26 16:34:37 -0800141 return tog;
142 }
143
144 function addRadioSet(id, rset) {
Simon Huntd3bcef32015-02-27 18:36:42 -0800145 if (dupId(id, 'addRadioSet')) return null;
146
Simon Huntdf510012015-02-26 16:34:37 -0800147 var rid = tbid + '-' + id,
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700148 rad = bns.radioSet(currentRow, rid, rset);
Simon Huntd3bcef32015-02-27 18:36:42 -0800149
150 items[id] = rad;
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700151 adjustWidth(rad.width());
Simon Huntdf510012015-02-26 16:34:37 -0800152 return rad;
153 }
154
155 function addSeparator() {
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700156 currentRow.append('div')
Simon Huntdf510012015-02-26 16:34:37 -0800157 .classed('separator', true);
158 tbWidth += sepWidth;
159 }
160
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700161 function addRow() {
Bri Prebilic Coledb4b87b2015-03-25 09:18:42 -0700162 if (currentRow.select('div').empty()) {
163 return null;
164 } else {
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700165 panel.append('br');
166 currentRow = panel.append('div').classed('tbar-row', true);
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700167
168 // return API to allow caller more access to the row
169 return {
170 clear: rowClear,
171 setText: rowSetText,
172 addButton: rowAddButton,
173 classed: rowClassed
174 };
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700175 }
176 }
177
Simon Hunt8d22c4b2015-08-06 16:24:43 -0700178 function rowClear() {
179 currentRow.selectAll('*').remove();
180 rowButtonIds.forEach(function (bid) {
181 delete items[bid];
182 });
183 rowButtonIds = [];
184 }
185
186 // installs a div with text into the button row
187 function rowSetText(text) {
188 rowClear();
189 currentRow.append('div').classed('tbar-row-text', true)
190 .html(text);
191 }
192
193 function rowAddButton(id, gid, cb, tooltip) {
194 var b = addButton(id, gid, cb, tooltip);
195 if (b) {
196 rowButtonIds.push(id);
197 }
198 }
199
200 function rowClassed(classes, bool) {
201 currentRow.classed(classes, bool);
202 }
203
Simon Huntdf510012015-02-26 16:34:37 -0800204 function show(cb) {
205 rotateArrowLeft(arrowDiv);
206 panel.show(cb);
207 }
208
209 function hide(cb) {
210 rotateArrowRight(arrowDiv);
211 panel.hide(cb);
212 }
213
214 function toggle(cb) {
215 if (panel.isVisible()) {
216 hide(cb);
217 } else {
218 show(cb);
219 }
220 }
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800221
Thomas Vachuska0af26912016-03-21 21:37:30 -0700222 function isVisible() {
223 return panel.isVisible();
224 }
225
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800226 return {
Bri Prebilic Colec4403322015-02-23 10:29:22 -0800227 addButton: addButton,
228 addToggle: addToggle,
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800229 addRadioSet: addRadioSet,
Bri Prebilic Cole5000a752015-02-23 17:20:53 -0800230 addSeparator: addSeparator,
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700231 addRow: addRow,
Bri Prebilic Cole5000a752015-02-23 17:20:53 -0800232
233 show: show,
234 hide: hide,
Thomas Vachuska0af26912016-03-21 21:37:30 -0700235 toggle: toggle,
236 isVisible: isVisible
Simon Huntdf510012015-02-26 16:34:37 -0800237 };
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800238 }
Simon Hunt69252862015-02-26 11:26:08 -0800239
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800240 function destroyToolbar(id) {
Simon Huntdf510012015-02-26 16:34:37 -0800241 var tb = tbars[id];
242 delete tbars[id];
Bri Prebilic Cole5000a752015-02-23 17:20:53 -0800243
Simon Huntdf510012015-02-26 16:34:37 -0800244 if (tb) {
245 ps.destroyPanel(tb.panelId);
Bri Prebilic Cole258f0462015-02-25 14:48:50 -0800246 }
247 }
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -0800248
Simon Huntdf510012015-02-26 16:34:37 -0800249 // === Module Definition ===
250
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -0800251 angular.module('onosWidget')
Simon Hunt69252862015-02-26 11:26:08 -0800252 .factory('ToolbarService',
253 ['$log', 'FnService', 'PanelService', 'ButtonService', 'IconService',
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -0800254
Simon Hunt69252862015-02-26 11:26:08 -0800255 function (_$log_, _fs_, _ps_, _bns_, _is_) {
256 $log = _$log_;
257 fs = _fs_;
258 ps = _ps_;
259 bns = _bns_;
260 is = _is_;
261
Simon Huntdf510012015-02-26 16:34:37 -0800262 // this function is only used in testing
263 function init() {
264 tbars = {};
265 }
266
Simon Hunt69252862015-02-26 11:26:08 -0800267 return {
268 init: init,
269 createToolbar: createToolbar,
270 destroyToolbar: destroyToolbar
271 };
272 }]);
Bri Prebilic Colec4403322015-02-23 10:29:22 -0800273}());