blob: f2b6bf086f98f2b4a684f824e1b6da5be36f3934 [file] [log] [blame]
Simon Hunt988934e2015-01-23 11:49:24 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Simon Hunt988934e2015-01-23 11:49:24 -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 -- Layer -- Panel Service
19 */
20(function () {
21 'use strict';
22
Simon Hunt54442fa2015-01-26 14:17:38 -080023 var $log, fs;
Simon Hunt988934e2015-01-23 11:49:24 -080024
25 var defaultSettings = {
Simon Hunt54442fa2015-01-26 14:17:38 -080026 edge: 'right',
27 width: 200,
Simon Hunt54442fa2015-01-26 14:17:38 -080028 margin: 20,
Bri Prebilic Colebe8b9a42015-02-24 12:12:00 -080029 hideMargin: 20,
30 xtnTime: 750,
31 fade: true
Simon Hunt988934e2015-01-23 11:49:24 -080032 };
33
Simon Hunt54442fa2015-01-26 14:17:38 -080034 var panels,
35 panelLayer;
36
37
38 function init() {
39 panelLayer = d3.select('#floatpanels');
Simon Hunt239f09e2017-05-18 13:10:09 -070040 panelLayer.text('');
Simon Hunt54442fa2015-01-26 14:17:38 -080041 panels = {};
42 }
43
44 // helpers for panel
45 function noop() {}
46
47 function margin(p) {
48 return p.settings.margin;
49 }
Bri Prebilic Colebe8b9a42015-02-24 12:12:00 -080050 function hideMargin(p) {
51 return p.settings.hideMargin;
52 }
Simon Hunt54442fa2015-01-26 14:17:38 -080053 function noPx(p, what) {
54 return Number(p.el.style(what).replace(/px$/, ''));
55 }
56 function widthVal(p) {
57 return noPx(p, 'width');
58 }
59 function heightVal(p) {
60 return noPx(p, 'height');
61 }
62 function pxShow(p) {
63 return margin(p) + 'px';
64 }
65 function pxHide(p) {
Bri Prebilic Colebe8b9a42015-02-24 12:12:00 -080066 return (-hideMargin(p) - widthVal(p) - (noPx(p, 'padding') * 2)) + 'px';
Simon Hunt54442fa2015-01-26 14:17:38 -080067 }
68
69 function makePanel(id, settings) {
70 var p = {
71 id: id,
72 settings: settings,
73 on: false,
74 el: null
75 },
76 api = {
77 show: showPanel,
78 hide: hidePanel,
Simon Hunt5724fb42015-02-05 16:59:40 -080079 toggle: togglePanel,
Simon Hunt54442fa2015-01-26 14:17:38 -080080 empty: emptyPanel,
81 append: appendPanel,
82 width: panelWidth,
83 height: panelHeight,
Simon Hunt8d47a5c2016-06-15 12:56:50 -070084 bbox: panelBBox,
Simon Hunt4b668592015-01-29 17:33:53 -080085 isVisible: panelIsVisible,
Simon Hunt08f841d02015-02-10 14:39:20 -080086 classed: classed,
Simon Hunt4b668592015-01-29 17:33:53 -080087 el: panelEl
Simon Hunt54442fa2015-01-26 14:17:38 -080088 };
89
90 p.el = panelLayer.append('div')
91 .attr('id', id)
92 .attr('class', 'floatpanel')
93 .style('opacity', 0);
94
95 // has to be called after el is set
96 p.el.style(p.settings.edge, pxHide(p));
97 panelWidth(p.settings.width);
Simon Hunt626d2102015-01-29 11:54:50 -080098 if (p.settings.height) {
99 panelHeight(p.settings.height);
100 }
Simon Hunt54442fa2015-01-26 14:17:38 -0800101
102 panels[id] = p;
103
104 function showPanel(cb) {
105 var endCb = fs.isF(cb) || noop;
106 p.on = true;
107 p.el.transition().duration(p.settings.xtnTime)
108 .each('end', endCb)
109 .style(p.settings.edge, pxShow(p))
110 .style('opacity', 1);
111 }
112
113 function hidePanel(cb) {
Bri Prebilic Colebe8b9a42015-02-24 12:12:00 -0800114 var endCb = fs.isF(cb) || noop,
115 endOpacity = p.settings.fade ? 0 : 1;
Simon Hunt54442fa2015-01-26 14:17:38 -0800116 p.on = false;
117 p.el.transition().duration(p.settings.xtnTime)
118 .each('end', endCb)
119 .style(p.settings.edge, pxHide(p))
Bri Prebilic Colebe8b9a42015-02-24 12:12:00 -0800120 .style('opacity', endOpacity);
Simon Hunt54442fa2015-01-26 14:17:38 -0800121 }
122
Simon Hunt5724fb42015-02-05 16:59:40 -0800123 function togglePanel(cb) {
124 if (p.on) {
125 hidePanel(cb);
126 } else {
127 showPanel(cb);
128 }
Simon Huntee7a3ce2015-04-09 13:28:37 -0700129 return p.on;
Simon Hunt5724fb42015-02-05 16:59:40 -0800130 }
131
Simon Hunt54442fa2015-01-26 14:17:38 -0800132 function emptyPanel() {
Simon Hunt239f09e2017-05-18 13:10:09 -0700133 return p.el.text('');
Simon Hunt54442fa2015-01-26 14:17:38 -0800134 }
135
136 function appendPanel(what) {
137 return p.el.append(what);
138 }
139
140 function panelWidth(w) {
141 if (w === undefined) {
142 return widthVal(p);
143 }
144 p.el.style('width', w + 'px');
145 }
146
147 function panelHeight(h) {
148 if (h === undefined) {
149 return heightVal(p);
150 }
151 p.el.style('height', h + 'px');
152 }
153
Simon Hunt8d47a5c2016-06-15 12:56:50 -0700154 function panelBBox() {
155 return p.el.node().getBoundingClientRect();
156 }
157
Simon Hunt54442fa2015-01-26 14:17:38 -0800158 function panelIsVisible() {
159 return p.on;
160 }
161
Simon Hunt08f841d02015-02-10 14:39:20 -0800162 function classed(cls, bool) {
163 return p.el.classed(cls, bool);
164 }
165
Simon Hunt4b668592015-01-29 17:33:53 -0800166 function panelEl() {
167 return p.el;
168 }
169
Simon Hunt54442fa2015-01-26 14:17:38 -0800170 return api;
171 }
172
173 function removePanel(id) {
174 panelLayer.select('#' + id).remove();
175 delete panels[id];
176 }
177
Simon Hunt988934e2015-01-23 11:49:24 -0800178 angular.module('onosLayer')
Simon Hunt75c25682015-11-02 16:49:56 -0800179 .factory('PanelService',
180 ['$log', '$window', 'FnService',
181
182 function (_$log_, _$window_, _fs_) {
Simon Hunt988934e2015-01-23 11:49:24 -0800183 $log = _$log_;
Simon Hunt54442fa2015-01-26 14:17:38 -0800184 fs = _fs_;
Simon Hunt988934e2015-01-23 11:49:24 -0800185
Simon Hunt54442fa2015-01-26 14:17:38 -0800186 function createPanel(id, opts) {
Simon Hunt988934e2015-01-23 11:49:24 -0800187 var settings = angular.extend({}, defaultSettings, opts);
Simon Hunt54442fa2015-01-26 14:17:38 -0800188 if (!id) {
189 $log.warn('createPanel: no ID given');
190 return null;
Simon Hunt988934e2015-01-23 11:49:24 -0800191 }
Simon Hunt54442fa2015-01-26 14:17:38 -0800192 if (panels[id]) {
193 $log.warn('Panel with ID "' + id + '" already exists');
194 return null;
Simon Hunt988934e2015-01-23 11:49:24 -0800195 }
Simon Hunt4deb0e82015-06-10 16:18:25 -0700196 if (fs.debugOn('widget')) {
197 $log.debug('creating panel:', id, settings);
198 }
Simon Hunt54442fa2015-01-26 14:17:38 -0800199 return makePanel(id, settings);
200 }
Simon Hunt988934e2015-01-23 11:49:24 -0800201
Simon Hunt54442fa2015-01-26 14:17:38 -0800202 function destroyPanel(id) {
203 if (panels[id]) {
Simon Hunt4deb0e82015-06-10 16:18:25 -0700204 if (fs.debugOn('widget')) {
205 $log.debug('destroying panel:', id);
206 }
Simon Hunt54442fa2015-01-26 14:17:38 -0800207 removePanel(id);
208 } else {
Simon Hunt4deb0e82015-06-10 16:18:25 -0700209 if (fs.debugOn('widget')) {
210 $log.debug('no panel to destroy:', id);
211 }
Simon Hunt988934e2015-01-23 11:49:24 -0800212 }
Simon Hunt988934e2015-01-23 11:49:24 -0800213 }
214
215 return {
Simon Hunt54442fa2015-01-26 14:17:38 -0800216 init: init,
217 createPanel: createPanel,
218 destroyPanel: destroyPanel
Simon Hunt988934e2015-01-23 11:49:24 -0800219 };
220 }]);
Simon Hunt988934e2015-01-23 11:49:24 -0800221}());