blob: aef71eee8d0d2317c2e5d39171b02380421f10ce [file] [log] [blame]
Simon Hunt988934e2015-01-23 11:49:24 -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 -- 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');
40 panelLayer.html('');
41 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 Hunt4b668592015-01-29 17:33:53 -080084 isVisible: panelIsVisible,
Simon Hunt08f841d02015-02-10 14:39:20 -080085 classed: classed,
Simon Hunt4b668592015-01-29 17:33:53 -080086 el: panelEl
Simon Hunt54442fa2015-01-26 14:17:38 -080087 };
88
89 p.el = panelLayer.append('div')
90 .attr('id', id)
91 .attr('class', 'floatpanel')
92 .style('opacity', 0);
93
94 // has to be called after el is set
95 p.el.style(p.settings.edge, pxHide(p));
96 panelWidth(p.settings.width);
Simon Hunt626d2102015-01-29 11:54:50 -080097 if (p.settings.height) {
98 panelHeight(p.settings.height);
99 }
Simon Hunt54442fa2015-01-26 14:17:38 -0800100
101 panels[id] = p;
102
103 function showPanel(cb) {
104 var endCb = fs.isF(cb) || noop;
105 p.on = true;
106 p.el.transition().duration(p.settings.xtnTime)
107 .each('end', endCb)
108 .style(p.settings.edge, pxShow(p))
109 .style('opacity', 1);
110 }
111
112 function hidePanel(cb) {
Bri Prebilic Colebe8b9a42015-02-24 12:12:00 -0800113 var endCb = fs.isF(cb) || noop,
114 endOpacity = p.settings.fade ? 0 : 1;
Simon Hunt54442fa2015-01-26 14:17:38 -0800115 p.on = false;
116 p.el.transition().duration(p.settings.xtnTime)
117 .each('end', endCb)
118 .style(p.settings.edge, pxHide(p))
Bri Prebilic Colebe8b9a42015-02-24 12:12:00 -0800119 .style('opacity', endOpacity);
Simon Hunt54442fa2015-01-26 14:17:38 -0800120 }
121
Simon Hunt5724fb42015-02-05 16:59:40 -0800122 function togglePanel(cb) {
123 if (p.on) {
124 hidePanel(cb);
125 } else {
126 showPanel(cb);
127 }
Simon Huntee7a3ce2015-04-09 13:28:37 -0700128 return p.on;
Simon Hunt5724fb42015-02-05 16:59:40 -0800129 }
130
Simon Hunt54442fa2015-01-26 14:17:38 -0800131 function emptyPanel() {
132 return p.el.html('');
133 }
134
135 function appendPanel(what) {
136 return p.el.append(what);
137 }
138
139 function panelWidth(w) {
140 if (w === undefined) {
141 return widthVal(p);
142 }
143 p.el.style('width', w + 'px');
144 }
145
146 function panelHeight(h) {
147 if (h === undefined) {
148 return heightVal(p);
149 }
150 p.el.style('height', h + 'px');
151 }
152
153 function panelIsVisible() {
154 return p.on;
155 }
156
Simon Hunt08f841d02015-02-10 14:39:20 -0800157 function classed(cls, bool) {
158 return p.el.classed(cls, bool);
159 }
160
Simon Hunt4b668592015-01-29 17:33:53 -0800161 function panelEl() {
162 return p.el;
163 }
164
Simon Hunt54442fa2015-01-26 14:17:38 -0800165 return api;
166 }
167
168 function removePanel(id) {
169 panelLayer.select('#' + id).remove();
170 delete panels[id];
171 }
172
Simon Hunt988934e2015-01-23 11:49:24 -0800173 angular.module('onosLayer')
Simon Hunt54442fa2015-01-26 14:17:38 -0800174 .factory('PanelService', ['$log', 'FnService', function (_$log_, _fs_) {
Simon Hunt988934e2015-01-23 11:49:24 -0800175 $log = _$log_;
Simon Hunt54442fa2015-01-26 14:17:38 -0800176 fs = _fs_;
Simon Hunt988934e2015-01-23 11:49:24 -0800177
Simon Hunt54442fa2015-01-26 14:17:38 -0800178 function createPanel(id, opts) {
Simon Hunt988934e2015-01-23 11:49:24 -0800179 var settings = angular.extend({}, defaultSettings, opts);
Simon Hunt54442fa2015-01-26 14:17:38 -0800180 if (!id) {
181 $log.warn('createPanel: no ID given');
182 return null;
Simon Hunt988934e2015-01-23 11:49:24 -0800183 }
Simon Hunt54442fa2015-01-26 14:17:38 -0800184 if (panels[id]) {
185 $log.warn('Panel with ID "' + id + '" already exists');
186 return null;
Simon Hunt988934e2015-01-23 11:49:24 -0800187 }
Simon Hunt4deb0e82015-06-10 16:18:25 -0700188 if (fs.debugOn('widget')) {
189 $log.debug('creating panel:', id, settings);
190 }
Simon Hunt54442fa2015-01-26 14:17:38 -0800191 return makePanel(id, settings);
192 }
Simon Hunt988934e2015-01-23 11:49:24 -0800193
Simon Hunt54442fa2015-01-26 14:17:38 -0800194 function destroyPanel(id) {
195 if (panels[id]) {
Simon Hunt4deb0e82015-06-10 16:18:25 -0700196 if (fs.debugOn('widget')) {
197 $log.debug('destroying panel:', id);
198 }
Simon Hunt54442fa2015-01-26 14:17:38 -0800199 removePanel(id);
200 } else {
Simon Hunt4deb0e82015-06-10 16:18:25 -0700201 if (fs.debugOn('widget')) {
202 $log.debug('no panel to destroy:', id);
203 }
Simon Hunt988934e2015-01-23 11:49:24 -0800204 }
Simon Hunt988934e2015-01-23 11:49:24 -0800205 }
206
207 return {
Simon Hunt54442fa2015-01-26 14:17:38 -0800208 init: init,
209 createPanel: createPanel,
210 destroyPanel: destroyPanel
Simon Hunt988934e2015-01-23 11:49:24 -0800211 };
212 }]);
213
214}());