blob: 74867e50b8297e7a3160d5cc9dce183d3085a355 [file] [log] [blame]
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -08001/*
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 -- Toolbar Service
19 */
Simon Huntdf510012015-02-26 16:34:37 -080020// TODO: Augment service to allow toolbars to exist on right edge of screen
21
22
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -080023(function () {
24 'use strict';
25
Simon Huntdf510012015-02-26 16:34:37 -080026 // injected refs
Bri Prebilic Cole5000a752015-02-23 17:20:53 -080027 var $log, fs, ps, bns, is;
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -080028
Simon Huntdf510012015-02-26 16:34:37 -080029 // configuration
30 var arrowSize = 10,
31 sepWidth = 6,
Bri Prebilic Cole5000a752015-02-23 17:20:53 -080032 defaultSettings = {
33 edge: 'left',
Bri Prebilic Cole258f0462015-02-25 14:48:50 -080034 width: 20,
Bri Prebilic Colebe8b9a42015-02-24 12:12:00 -080035 margin: 0,
36 hideMargin: -20,
Bri Prebilic Colef7280d02015-02-24 16:20:47 -080037 top: '80%',
Bri Prebilic Cole258f0462015-02-25 14:48:50 -080038 fade: false,
39 shown: false
Simon Huntdf510012015-02-26 16:34:37 -080040 };
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -080041
Simon Huntdf510012015-02-26 16:34:37 -080042 // internal state
43 var tbars = {};
44
Bri Prebilic Cole751804e2015-02-18 15:44:28 -080045
Bri Prebilic Cole5000a752015-02-23 17:20:53 -080046 // === Helper functions --------------------------------------
47
Bri Prebilic Colebe8b9a42015-02-24 12:12:00 -080048 // translate uses 50 because the svg viewbox is 50
Simon Huntdf510012015-02-26 16:34:37 -080049 function rotateArrowLeft(adiv) {
50 adiv.select('g')
Bri Prebilic Cole5000a752015-02-23 17:20:53 -080051 .attr('transform', 'translate(0 50) rotate(-90)');
52 }
Simon Huntdf510012015-02-26 16:34:37 -080053 function rotateArrowRight(adiv) {
54 adiv.select('g')
Bri Prebilic Colebe8b9a42015-02-24 12:12:00 -080055 .attr('transform', 'translate(50 0) rotate(90)');
Bri Prebilic Cole5000a752015-02-23 17:20:53 -080056 }
Simon Huntdf510012015-02-26 16:34:37 -080057
58 function createArrow(panel) {
59 var arrowDiv = panel.append('div')
Bri Prebilic Cole5000a752015-02-23 17:20:53 -080060 .classed('tbarArrow', true)
Simon Huntdf510012015-02-26 16:34:37 -080061 .style({
62 'position': 'absolute',
Bri Prebilic Colebe8b9a42015-02-24 12:12:00 -080063 'top': '53%',
Bri Prebilic Cole258f0462015-02-25 14:48:50 -080064 'left': '96%',
65 'margin-right': '-4%',
Bri Prebilic Cole5000a752015-02-23 17:20:53 -080066 'transform': 'translate(-50%, -50%)',
Simon Huntdf510012015-02-26 16:34:37 -080067 'cursor': 'pointer'
68 });
69 is.loadIcon(arrowDiv, 'triangleUp', arrowSize, true);
70 return arrowDiv;
Bri Prebilic Cole5000a752015-02-23 17:20:53 -080071 }
72
Simon Huntdf510012015-02-26 16:34:37 -080073 function warn(msg, id) {
74 $log.warn('createToolbar: ' + msg + ': [' + id + ']');
75 return null;
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -080076 }
77
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -070078 function noPxWidth(elem) {
79 return Number(elem.style('width').replace(/px$/, ''));
80 }
81
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -080082
Simon Huntdf510012015-02-26 16:34:37 -080083 // ==================================
Bri Prebilic Cole5000a752015-02-23 17:20:53 -080084
85 function createToolbar(id, opts) {
Simon Huntd3bcef32015-02-27 18:36:42 -080086 if (!id) return warn('no ID given');
Simon Huntdf510012015-02-26 16:34:37 -080087 if (tbars[id]) return warn('duplicate ID given', id);
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -080088
Simon Huntdf510012015-02-26 16:34:37 -080089 var settings = angular.extend({}, defaultSettings, fs.isO(opts)),
Simon Huntd3bcef32015-02-27 18:36:42 -080090 items = {},
Simon Huntdf510012015-02-26 16:34:37 -080091 tbid = 'toolbar-' + id,
92 panel = ps.createPanel(tbid, settings),
93 arrowDiv = createArrow(panel),
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -070094 currentRow = panel.append('div').classed('tbar-row', true),
95 tbWidth = arrowSize + 2, // empty toolbar width
96 maxWidth = panel.width();
Simon Huntdf510012015-02-26 16:34:37 -080097
98 arrowDiv.on('click', toggle);
99
100 // add a descriptor for this toolbar
101 tbars[id] = {
102 settings: settings,
Simon Huntd3bcef32015-02-27 18:36:42 -0800103 items: items,
Simon Huntdf510012015-02-26 16:34:37 -0800104 panel: panel,
105 panelId: tbid
106 };
107
108 panel.classed('toolbar', true)
Bri Prebilic Colef7280d02015-02-24 16:20:47 -0800109 .style('top', settings.top);
Bri Prebilic Cole5000a752015-02-23 17:20:53 -0800110
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700111 // Helper functions
Simon Huntdf510012015-02-26 16:34:37 -0800112
Simon Huntd3bcef32015-02-27 18:36:42 -0800113 function dupId(id, caller) {
114 if (items[id]) {
115 $log.warn(caller + ': duplicate ID:', id);
116 return true;
117 }
118 return false;
119 }
120
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700121 function adjustWidth(btnWidth) {
122 if (noPxWidth(currentRow) >= maxWidth) {
123 tbWidth += btnWidth;
124 maxWidth = tbWidth;
125 }
126 panel.width(tbWidth);
127 }
128
Simon Huntdf510012015-02-26 16:34:37 -0800129 // API functions
130
131 function addButton(id, gid, cb, tooltip) {
Simon Huntd3bcef32015-02-27 18:36:42 -0800132 if (dupId(id, 'addButton')) return null;
133
Simon Huntdf510012015-02-26 16:34:37 -0800134 var bid = tbid + '-' + id,
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700135 btn = bns.button(currentRow, bid, gid, cb, tooltip);
Simon Huntd3bcef32015-02-27 18:36:42 -0800136
137 items[id] = btn;
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700138 adjustWidth(btn.width());
Simon Huntdf510012015-02-26 16:34:37 -0800139 return btn;
140 }
141
142 function addToggle(id, gid, initState, cb, tooltip) {
Simon Huntd3bcef32015-02-27 18:36:42 -0800143 if (dupId(id, 'addToggle')) return null;
144
Simon Huntdf510012015-02-26 16:34:37 -0800145 var tid = tbid + '-' + id,
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700146 tog = bns.toggle(currentRow, tid, gid, initState, cb, tooltip);
Simon Huntd3bcef32015-02-27 18:36:42 -0800147
148 items[id] = tog;
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700149 adjustWidth(tog.width());
Simon Huntdf510012015-02-26 16:34:37 -0800150 return tog;
151 }
152
153 function addRadioSet(id, rset) {
Simon Huntd3bcef32015-02-27 18:36:42 -0800154 if (dupId(id, 'addRadioSet')) return null;
155
Simon Huntdf510012015-02-26 16:34:37 -0800156 var rid = tbid + '-' + id,
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700157 rad = bns.radioSet(currentRow, rid, rset);
Simon Huntd3bcef32015-02-27 18:36:42 -0800158
159 items[id] = rad;
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700160 adjustWidth(rad.width());
Simon Huntdf510012015-02-26 16:34:37 -0800161 return rad;
162 }
163
164 function addSeparator() {
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700165 currentRow.append('div')
Simon Huntdf510012015-02-26 16:34:37 -0800166 .classed('separator', true);
167 tbWidth += sepWidth;
168 }
169
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700170 function addRow() {
Bri Prebilic Coledb4b87b2015-03-25 09:18:42 -0700171 if (currentRow.select('div').empty()) {
172 return null;
173 } else {
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700174 panel.append('br');
175 currentRow = panel.append('div').classed('tbar-row', true);
176 }
177 }
178
Simon Huntdf510012015-02-26 16:34:37 -0800179 function show(cb) {
180 rotateArrowLeft(arrowDiv);
181 panel.show(cb);
182 }
183
184 function hide(cb) {
185 rotateArrowRight(arrowDiv);
186 panel.hide(cb);
187 }
188
189 function toggle(cb) {
190 if (panel.isVisible()) {
191 hide(cb);
192 } else {
193 show(cb);
194 }
195 }
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800196
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800197 return {
Bri Prebilic Colec4403322015-02-23 10:29:22 -0800198 addButton: addButton,
199 addToggle: addToggle,
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800200 addRadioSet: addRadioSet,
Bri Prebilic Cole5000a752015-02-23 17:20:53 -0800201 addSeparator: addSeparator,
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700202 addRow: addRow,
Bri Prebilic Cole5000a752015-02-23 17:20:53 -0800203
204 show: show,
205 hide: hide,
Simon Huntdf510012015-02-26 16:34:37 -0800206 toggle: toggle
207 };
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800208 }
Simon Hunt69252862015-02-26 11:26:08 -0800209
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800210 function destroyToolbar(id) {
Simon Huntdf510012015-02-26 16:34:37 -0800211 var tb = tbars[id];
212 delete tbars[id];
Bri Prebilic Cole5000a752015-02-23 17:20:53 -0800213
Simon Huntdf510012015-02-26 16:34:37 -0800214 if (tb) {
215 ps.destroyPanel(tb.panelId);
Bri Prebilic Cole258f0462015-02-25 14:48:50 -0800216 }
217 }
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -0800218
Simon Huntdf510012015-02-26 16:34:37 -0800219 // === Module Definition ===
220
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -0800221 angular.module('onosWidget')
Simon Hunt69252862015-02-26 11:26:08 -0800222 .factory('ToolbarService',
223 ['$log', 'FnService', 'PanelService', 'ButtonService', 'IconService',
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -0800224
Simon Hunt69252862015-02-26 11:26:08 -0800225 function (_$log_, _fs_, _ps_, _bns_, _is_) {
226 $log = _$log_;
227 fs = _fs_;
228 ps = _ps_;
229 bns = _bns_;
230 is = _is_;
231
Simon Huntdf510012015-02-26 16:34:37 -0800232 // this function is only used in testing
233 function init() {
234 tbars = {};
235 }
236
Simon Hunt69252862015-02-26 11:26:08 -0800237 return {
238 init: init,
239 createToolbar: createToolbar,
240 destroyToolbar: destroyToolbar
241 };
242 }]);
Bri Prebilic Colec4403322015-02-23 10:29:22 -0800243}());