blob: 00086dc8b5f58af9871c6fa16180ddbb372e300e [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 Coled8745462015-06-01 16:08:57 -070037 top: 'auto',
38 bottom: '10px',
Bri Prebilic Cole258f0462015-02-25 14:48:50 -080039 fade: false,
40 shown: false
Simon Huntdf510012015-02-26 16:34:37 -080041 };
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -080042
Simon Huntdf510012015-02-26 16:34:37 -080043 // internal state
44 var tbars = {};
45
Bri Prebilic Cole751804e2015-02-18 15:44:28 -080046
Bri Prebilic Cole5000a752015-02-23 17:20:53 -080047 // === Helper functions --------------------------------------
48
Bri Prebilic Colebe8b9a42015-02-24 12:12:00 -080049 // translate uses 50 because the svg viewbox is 50
Simon Huntdf510012015-02-26 16:34:37 -080050 function rotateArrowLeft(adiv) {
51 adiv.select('g')
Bri Prebilic Cole5000a752015-02-23 17:20:53 -080052 .attr('transform', 'translate(0 50) rotate(-90)');
53 }
Simon Huntdf510012015-02-26 16:34:37 -080054 function rotateArrowRight(adiv) {
55 adiv.select('g')
Bri Prebilic Colebe8b9a42015-02-24 12:12:00 -080056 .attr('transform', 'translate(50 0) rotate(90)');
Bri Prebilic Cole5000a752015-02-23 17:20:53 -080057 }
Simon Huntdf510012015-02-26 16:34:37 -080058
59 function createArrow(panel) {
60 var arrowDiv = panel.append('div')
Bri Prebilic Colea1bb7a22015-08-04 16:54:08 -070061 .classed('tbar-arrow', true);
Simon Huntdf510012015-02-26 16:34:37 -080062 is.loadIcon(arrowDiv, 'triangleUp', arrowSize, true);
63 return arrowDiv;
Bri Prebilic Cole5000a752015-02-23 17:20:53 -080064 }
65
Simon Huntdf510012015-02-26 16:34:37 -080066 function warn(msg, id) {
67 $log.warn('createToolbar: ' + msg + ': [' + id + ']');
68 return null;
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -080069 }
70
Simon Huntdf510012015-02-26 16:34:37 -080071 // ==================================
Bri Prebilic Cole5000a752015-02-23 17:20:53 -080072
73 function createToolbar(id, opts) {
Simon Huntd3bcef32015-02-27 18:36:42 -080074 if (!id) return warn('no ID given');
Simon Huntdf510012015-02-26 16:34:37 -080075 if (tbars[id]) return warn('duplicate ID given', id);
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -080076
Simon Huntdf510012015-02-26 16:34:37 -080077 var settings = angular.extend({}, defaultSettings, fs.isO(opts)),
Simon Huntd3bcef32015-02-27 18:36:42 -080078 items = {},
Simon Huntdf510012015-02-26 16:34:37 -080079 tbid = 'toolbar-' + id,
80 panel = ps.createPanel(tbid, settings),
81 arrowDiv = createArrow(panel),
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -070082 currentRow = panel.append('div').classed('tbar-row', true),
83 tbWidth = arrowSize + 2, // empty toolbar width
84 maxWidth = panel.width();
Simon Huntdf510012015-02-26 16:34:37 -080085
86 arrowDiv.on('click', toggle);
87
88 // add a descriptor for this toolbar
89 tbars[id] = {
90 settings: settings,
Simon Huntd3bcef32015-02-27 18:36:42 -080091 items: items,
Simon Huntdf510012015-02-26 16:34:37 -080092 panel: panel,
93 panelId: tbid
94 };
95
96 panel.classed('toolbar', true)
Bri Prebilic Coled8745462015-06-01 16:08:57 -070097 .style('top', settings.top)
98 .style('bottom', settings.bottom);
Bri Prebilic Cole5000a752015-02-23 17:20:53 -080099
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700100 // Helper functions
Simon Huntdf510012015-02-26 16:34:37 -0800101
Simon Huntd3bcef32015-02-27 18:36:42 -0800102 function dupId(id, caller) {
103 if (items[id]) {
104 $log.warn(caller + ': duplicate ID:', id);
105 return true;
106 }
107 return false;
108 }
109
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700110 function adjustWidth(btnWidth) {
Bri Prebilic Cole08d08d42015-04-01 14:37:02 -0700111 if (fs.noPxStyle(currentRow, 'width') >= maxWidth) {
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700112 tbWidth += btnWidth;
113 maxWidth = tbWidth;
114 }
115 panel.width(tbWidth);
116 }
117
Simon Huntdf510012015-02-26 16:34:37 -0800118 // API functions
119
120 function addButton(id, gid, cb, tooltip) {
Simon Huntd3bcef32015-02-27 18:36:42 -0800121 if (dupId(id, 'addButton')) return null;
122
Simon Huntdf510012015-02-26 16:34:37 -0800123 var bid = tbid + '-' + id,
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700124 btn = bns.button(currentRow, bid, gid, cb, tooltip);
Simon Huntd3bcef32015-02-27 18:36:42 -0800125
126 items[id] = btn;
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700127 adjustWidth(btn.width());
Simon Huntdf510012015-02-26 16:34:37 -0800128 return btn;
129 }
130
131 function addToggle(id, gid, initState, cb, tooltip) {
Simon Huntd3bcef32015-02-27 18:36:42 -0800132 if (dupId(id, 'addToggle')) return null;
133
Simon Huntdf510012015-02-26 16:34:37 -0800134 var tid = tbid + '-' + id,
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700135 tog = bns.toggle(currentRow, tid, gid, initState, cb, tooltip);
Simon Huntd3bcef32015-02-27 18:36:42 -0800136
137 items[id] = tog;
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700138 adjustWidth(tog.width());
Simon Huntdf510012015-02-26 16:34:37 -0800139 return tog;
140 }
141
142 function addRadioSet(id, rset) {
Simon Huntd3bcef32015-02-27 18:36:42 -0800143 if (dupId(id, 'addRadioSet')) return null;
144
Simon Huntdf510012015-02-26 16:34:37 -0800145 var rid = tbid + '-' + id,
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700146 rad = bns.radioSet(currentRow, rid, rset);
Simon Huntd3bcef32015-02-27 18:36:42 -0800147
148 items[id] = rad;
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700149 adjustWidth(rad.width());
Simon Huntdf510012015-02-26 16:34:37 -0800150 return rad;
151 }
152
153 function addSeparator() {
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700154 currentRow.append('div')
Simon Huntdf510012015-02-26 16:34:37 -0800155 .classed('separator', true);
156 tbWidth += sepWidth;
157 }
158
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700159 function addRow() {
Bri Prebilic Coledb4b87b2015-03-25 09:18:42 -0700160 if (currentRow.select('div').empty()) {
161 return null;
162 } else {
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700163 panel.append('br');
164 currentRow = panel.append('div').classed('tbar-row', true);
165 }
166 }
167
Simon Huntdf510012015-02-26 16:34:37 -0800168 function show(cb) {
169 rotateArrowLeft(arrowDiv);
170 panel.show(cb);
171 }
172
173 function hide(cb) {
174 rotateArrowRight(arrowDiv);
175 panel.hide(cb);
176 }
177
178 function toggle(cb) {
179 if (panel.isVisible()) {
180 hide(cb);
181 } else {
182 show(cb);
183 }
184 }
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800185
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800186 return {
Bri Prebilic Colec4403322015-02-23 10:29:22 -0800187 addButton: addButton,
188 addToggle: addToggle,
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800189 addRadioSet: addRadioSet,
Bri Prebilic Cole5000a752015-02-23 17:20:53 -0800190 addSeparator: addSeparator,
Bri Prebilic Cole812f6c02015-03-24 17:10:33 -0700191 addRow: addRow,
Bri Prebilic Cole5000a752015-02-23 17:20:53 -0800192
193 show: show,
194 hide: hide,
Simon Huntdf510012015-02-26 16:34:37 -0800195 toggle: toggle
196 };
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800197 }
Simon Hunt69252862015-02-26 11:26:08 -0800198
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800199 function destroyToolbar(id) {
Simon Huntdf510012015-02-26 16:34:37 -0800200 var tb = tbars[id];
201 delete tbars[id];
Bri Prebilic Cole5000a752015-02-23 17:20:53 -0800202
Simon Huntdf510012015-02-26 16:34:37 -0800203 if (tb) {
204 ps.destroyPanel(tb.panelId);
Bri Prebilic Cole258f0462015-02-25 14:48:50 -0800205 }
206 }
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -0800207
Simon Huntdf510012015-02-26 16:34:37 -0800208 // === Module Definition ===
209
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -0800210 angular.module('onosWidget')
Simon Hunt69252862015-02-26 11:26:08 -0800211 .factory('ToolbarService',
212 ['$log', 'FnService', 'PanelService', 'ButtonService', 'IconService',
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -0800213
Simon Hunt69252862015-02-26 11:26:08 -0800214 function (_$log_, _fs_, _ps_, _bns_, _is_) {
215 $log = _$log_;
216 fs = _fs_;
217 ps = _ps_;
218 bns = _bns_;
219 is = _is_;
220
Simon Huntdf510012015-02-26 16:34:37 -0800221 // this function is only used in testing
222 function init() {
223 tbars = {};
224 }
225
Simon Hunt69252862015-02-26 11:26:08 -0800226 return {
227 init: init,
228 createToolbar: createToolbar,
229 destroyToolbar: destroyToolbar
230 };
231 }]);
Bri Prebilic Colec4403322015-02-23 10:29:22 -0800232}());