blob: 14a469c32fb22fd855584ed6bc7bf50570069e00 [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 Cole08381ce2015-02-20 17:01:50 -080078
Simon Huntdf510012015-02-26 16:34:37 -080079 // ==================================
Bri Prebilic Cole5000a752015-02-23 17:20:53 -080080
81 function createToolbar(id, opts) {
Simon Huntd3bcef32015-02-27 18:36:42 -080082 if (!id) return warn('no ID given');
Simon Huntdf510012015-02-26 16:34:37 -080083 if (tbars[id]) return warn('duplicate ID given', id);
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -080084
Simon Huntdf510012015-02-26 16:34:37 -080085 var settings = angular.extend({}, defaultSettings, fs.isO(opts)),
Simon Huntd3bcef32015-02-27 18:36:42 -080086 items = {},
Simon Huntdf510012015-02-26 16:34:37 -080087 tbid = 'toolbar-' + id,
88 panel = ps.createPanel(tbid, settings),
89 arrowDiv = createArrow(panel),
90 tbWidth = arrowSize + 2; // empty toolbar width
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -080091
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
Simon Huntdf510012015-02-26 16:34:37 -0800106
Simon Huntd3bcef32015-02-27 18:36:42 -0800107 function dupId(id, caller) {
108 if (items[id]) {
109 $log.warn(caller + ': duplicate ID:', id);
110 return true;
111 }
112 return false;
113 }
114
Simon Huntdf510012015-02-26 16:34:37 -0800115 // API functions
116
117 function addButton(id, gid, cb, tooltip) {
Simon Huntd3bcef32015-02-27 18:36:42 -0800118 if (dupId(id, 'addButton')) return null;
119
Simon Huntdf510012015-02-26 16:34:37 -0800120 var bid = tbid + '-' + id,
121 btn = bns.button(panel, bid, gid, cb, tooltip);
Simon Huntd3bcef32015-02-27 18:36:42 -0800122
123 items[id] = btn;
Simon Huntdf510012015-02-26 16:34:37 -0800124 tbWidth += btn.width();
125 panel.width(tbWidth);
126 return btn;
127 }
128
129 function addToggle(id, gid, initState, cb, tooltip) {
Simon Huntd3bcef32015-02-27 18:36:42 -0800130 if (dupId(id, 'addToggle')) return null;
131
Simon Huntdf510012015-02-26 16:34:37 -0800132 var tid = tbid + '-' + id,
133 tog = bns.toggle(panel, tid, gid, initState, cb, tooltip);
Simon Huntd3bcef32015-02-27 18:36:42 -0800134
135 items[id] = tog;
Simon Huntdf510012015-02-26 16:34:37 -0800136 tbWidth += tog.width();
137 panel.width(tbWidth);
138 return tog;
139 }
140
141 function addRadioSet(id, rset) {
Simon Huntd3bcef32015-02-27 18:36:42 -0800142 if (dupId(id, 'addRadioSet')) return null;
143
Simon Huntdf510012015-02-26 16:34:37 -0800144 var rid = tbid + '-' + id,
145 rad = bns.radioSet(panel, rid, rset);
Simon Huntd3bcef32015-02-27 18:36:42 -0800146
147 items[id] = rad;
Simon Huntdf510012015-02-26 16:34:37 -0800148 tbWidth += rad.width();
149 panel.width(tbWidth);
150 return rad;
151 }
152
153 function addSeparator() {
154 panel.append('div')
155 .classed('separator', true);
156 tbWidth += sepWidth;
157 }
158
159 function show(cb) {
160 rotateArrowLeft(arrowDiv);
161 panel.show(cb);
162 }
163
164 function hide(cb) {
165 rotateArrowRight(arrowDiv);
166 panel.hide(cb);
167 }
168
169 function toggle(cb) {
170 if (panel.isVisible()) {
171 hide(cb);
172 } else {
173 show(cb);
174 }
175 }
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800176
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800177 return {
Bri Prebilic Colec4403322015-02-23 10:29:22 -0800178 addButton: addButton,
179 addToggle: addToggle,
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800180 addRadioSet: addRadioSet,
Bri Prebilic Cole5000a752015-02-23 17:20:53 -0800181 addSeparator: addSeparator,
182
183 show: show,
184 hide: hide,
Simon Huntdf510012015-02-26 16:34:37 -0800185 toggle: toggle
186 };
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800187 }
Simon Hunt69252862015-02-26 11:26:08 -0800188
Bri Prebilic Cole08381ce2015-02-20 17:01:50 -0800189 function destroyToolbar(id) {
Simon Huntdf510012015-02-26 16:34:37 -0800190 var tb = tbars[id];
191 delete tbars[id];
Bri Prebilic Cole5000a752015-02-23 17:20:53 -0800192
Simon Huntdf510012015-02-26 16:34:37 -0800193 if (tb) {
194 ps.destroyPanel(tb.panelId);
Bri Prebilic Cole258f0462015-02-25 14:48:50 -0800195 }
196 }
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -0800197
Simon Huntdf510012015-02-26 16:34:37 -0800198 // === Module Definition ===
199
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -0800200 angular.module('onosWidget')
Simon Hunt69252862015-02-26 11:26:08 -0800201 .factory('ToolbarService',
202 ['$log', 'FnService', 'PanelService', 'ButtonService', 'IconService',
Bri Prebilic Cole2e3f8562015-02-17 17:21:31 -0800203
Simon Hunt69252862015-02-26 11:26:08 -0800204 function (_$log_, _fs_, _ps_, _bns_, _is_) {
205 $log = _$log_;
206 fs = _fs_;
207 ps = _ps_;
208 bns = _bns_;
209 is = _is_;
210
Simon Huntdf510012015-02-26 16:34:37 -0800211 // this function is only used in testing
212 function init() {
213 tbars = {};
214 }
215
Simon Hunt69252862015-02-26 11:26:08 -0800216 return {
217 init: init,
218 createToolbar: createToolbar,
219 destroyToolbar: destroyToolbar
220 };
221 }]);
Bri Prebilic Colec4403322015-02-23 10:29:22 -0800222}());