blob: 7b9b6b0c7a4b87a6544b753383757280d7c973aa [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
Simon Huntdf510012015-02-26 16:34:37 -080078 // ==================================
Bri Prebilic Cole5000a752015-02-23 17:20:53 -080079
80 function createToolbar(id, opts) {
Simon Huntd3bcef32015-02-27 18:36:42 -080081 if (!id) return warn('no ID given');
Simon Huntdf510012015-02-26 16:34:37 -080082 if (tbars[id]) return warn('duplicate ID given', id);
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -080083
Simon Huntdf510012015-02-26 16:34:37 -080084 var settings = angular.extend({}, defaultSettings, fs.isO(opts)),
Simon Huntd3bcef32015-02-27 18:36:42 -080085 items = {},
Simon Huntdf510012015-02-26 16:34:37 -080086 tbid = 'toolbar-' + id,
87 panel = ps.createPanel(tbid, settings),
88 arrowDiv = createArrow(panel),
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -070089 currentRow = panel.append('div').classed('tbar-row', true),
90 tbWidth = arrowSize + 2, // empty toolbar width
91 maxWidth = panel.width();
Simon Huntdf510012015-02-26 16:34:37 -080092
93 arrowDiv.on('click', toggle);
94
95 // add a descriptor for this toolbar
96 tbars[id] = {
97 settings: settings,
Simon Huntd3bcef32015-02-27 18:36:42 -080098 items: items,
Simon Huntdf510012015-02-26 16:34:37 -080099 panel: panel,
100 panelId: tbid
101 };
102
103 panel.classed('toolbar', true)
Bri Prebilic Colef7280d02015-02-24 16:20:47 -0800104 .style('top', settings.top);
Bri Prebilic Cole5000a752015-02-23 17:20:53 -0800105
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700106 // Helper functions
Simon Huntdf510012015-02-26 16:34:37 -0800107
Simon Huntd3bcef32015-02-27 18:36:42 -0800108 function dupId(id, caller) {
109 if (items[id]) {
110 $log.warn(caller + ': duplicate ID:', id);
111 return true;
112 }
113 return false;
114 }
115
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700116 function adjustWidth(btnWidth) {
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -0700117 if (fs.noPxStyle(currentRow, 'width') >= maxWidth) {
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700118 tbWidth += btnWidth;
119 maxWidth = tbWidth;
120 }
121 panel.width(tbWidth);
122 }
123
Simon Huntdf510012015-02-26 16:34:37 -0800124 // API functions
125
126 function addButton(id, gid, cb, tooltip) {
Simon Huntd3bcef32015-02-27 18:36:42 -0800127 if (dupId(id, 'addButton')) return null;
128
Simon Huntdf510012015-02-26 16:34:37 -0800129 var bid = tbid + '-' + id,
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700130 btn = bns.button(currentRow, bid, gid, cb, tooltip);
Simon Huntd3bcef32015-02-27 18:36:42 -0800131
132 items[id] = btn;
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700133 adjustWidth(btn.width());
Simon Huntdf510012015-02-26 16:34:37 -0800134 return btn;
135 }
136
137 function addToggle(id, gid, initState, cb, tooltip) {
Simon Huntd3bcef32015-02-27 18:36:42 -0800138 if (dupId(id, 'addToggle')) return null;
139
Simon Huntdf510012015-02-26 16:34:37 -0800140 var tid = tbid + '-' + id,
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700141 tog = bns.toggle(currentRow, tid, gid, initState, cb, tooltip);
Simon Huntd3bcef32015-02-27 18:36:42 -0800142
143 items[id] = tog;
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700144 adjustWidth(tog.width());
Simon Huntdf510012015-02-26 16:34:37 -0800145 return tog;
146 }
147
148 function addRadioSet(id, rset) {
Simon Huntd3bcef32015-02-27 18:36:42 -0800149 if (dupId(id, 'addRadioSet')) return null;
150
Simon Huntdf510012015-02-26 16:34:37 -0800151 var rid = tbid + '-' + id,
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700152 rad = bns.radioSet(currentRow, rid, rset);
Simon Huntd3bcef32015-02-27 18:36:42 -0800153
154 items[id] = rad;
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700155 adjustWidth(rad.width());
Simon Huntdf510012015-02-26 16:34:37 -0800156 return rad;
157 }
158
159 function addSeparator() {
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700160 currentRow.append('div')
Simon Huntdf510012015-02-26 16:34:37 -0800161 .classed('separator', true);
162 tbWidth += sepWidth;
163 }
164
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700165 function addRow() {
Bri Prebilic Coledb4b87b2015-03-25 09:18:42 -0700166 if (currentRow.select('div').empty()) {
167 return null;
168 } else {
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700169 panel.append('br');
170 currentRow = panel.append('div').classed('tbar-row', true);
171 }
172 }
173
Simon Huntdf510012015-02-26 16:34:37 -0800174 function show(cb) {
175 rotateArrowLeft(arrowDiv);
176 panel.show(cb);
177 }
178
179 function hide(cb) {
180 rotateArrowRight(arrowDiv);
181 panel.hide(cb);
182 }
183
184 function toggle(cb) {
185 if (panel.isVisible()) {
186 hide(cb);
187 } else {
188 show(cb);
189 }
190 }
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800191
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800192 return {
Bri Prebilic Colec4403322015-02-23 10:29:22 -0800193 addButton: addButton,
194 addToggle: addToggle,
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800195 addRadioSet: addRadioSet,
Bri Prebilic Cole5000a752015-02-23 17:20:53 -0800196 addSeparator: addSeparator,
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700197 addRow: addRow,
Bri Prebilic Cole5000a752015-02-23 17:20:53 -0800198
199 show: show,
200 hide: hide,
Simon Huntdf510012015-02-26 16:34:37 -0800201 toggle: toggle
202 };
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800203 }
Simon Hunt69252862015-02-26 11:26:08 -0800204
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800205 function destroyToolbar(id) {
Simon Huntdf510012015-02-26 16:34:37 -0800206 var tb = tbars[id];
207 delete tbars[id];
Bri Prebilic Cole5000a752015-02-23 17:20:53 -0800208
Simon Huntdf510012015-02-26 16:34:37 -0800209 if (tb) {
210 ps.destroyPanel(tb.panelId);
Bri Prebilic Cole258f0462015-02-25 14:48:50 -0800211 }
212 }
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -0800213
Simon Huntdf510012015-02-26 16:34:37 -0800214 // === Module Definition ===
215
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -0800216 angular.module('onosWidget')
Simon Hunt69252862015-02-26 11:26:08 -0800217 .factory('ToolbarService',
218 ['$log', 'FnService', 'PanelService', 'ButtonService', 'IconService',
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -0800219
Simon Hunt69252862015-02-26 11:26:08 -0800220 function (_$log_, _fs_, _ps_, _bns_, _is_) {
221 $log = _$log_;
222 fs = _fs_;
223 ps = _ps_;
224 bns = _bns_;
225 is = _is_;
226
Simon Huntdf510012015-02-26 16:34:37 -0800227 // this function is only used in testing
228 function init() {
229 tbars = {};
230 }
231
Simon Hunt69252862015-02-26 11:26:08 -0800232 return {
233 init: init,
234 createToolbar: createToolbar,
235 destroyToolbar: destroyToolbar
236 };
237 }]);
Bri Prebilic Colec4403322015-02-23 10:29:22 -0800238}());