blob: 46934f100efddb889bd246b3e34acf64daad9e14 [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,
29 xtnTime: 750
Simon Hunt988934e2015-01-23 11:49:24 -080030 };
31
Simon Hunt54442fa2015-01-26 14:17:38 -080032 var panels,
33 panelLayer;
34
35
36 function init() {
37 panelLayer = d3.select('#floatpanels');
38 panelLayer.html('');
39 panels = {};
40 }
41
42 // helpers for panel
43 function noop() {}
44
45 function margin(p) {
46 return p.settings.margin;
47 }
48 function noPx(p, what) {
49 return Number(p.el.style(what).replace(/px$/, ''));
50 }
51 function widthVal(p) {
52 return noPx(p, 'width');
53 }
54 function heightVal(p) {
55 return noPx(p, 'height');
56 }
57 function pxShow(p) {
58 return margin(p) + 'px';
59 }
60 function pxHide(p) {
61 return (-margin(p) - widthVal(p)) + 'px';
62 }
63
64 function makePanel(id, settings) {
65 var p = {
66 id: id,
67 settings: settings,
68 on: false,
69 el: null
70 },
71 api = {
72 show: showPanel,
73 hide: hidePanel,
Simon Hunt5724fb42015-02-05 16:59:40 -080074 toggle: togglePanel,
Simon Hunt54442fa2015-01-26 14:17:38 -080075 empty: emptyPanel,
76 append: appendPanel,
77 width: panelWidth,
78 height: panelHeight,
Simon Hunt4b668592015-01-29 17:33:53 -080079 isVisible: panelIsVisible,
Simon Hunt08f841d02015-02-10 14:39:20 -080080 classed: classed,
Simon Hunt4b668592015-01-29 17:33:53 -080081 el: panelEl
Simon Hunt54442fa2015-01-26 14:17:38 -080082 };
83
84 p.el = panelLayer.append('div')
85 .attr('id', id)
86 .attr('class', 'floatpanel')
87 .style('opacity', 0);
88
89 // has to be called after el is set
90 p.el.style(p.settings.edge, pxHide(p));
91 panelWidth(p.settings.width);
Simon Hunt626d2102015-01-29 11:54:50 -080092 if (p.settings.height) {
93 panelHeight(p.settings.height);
94 }
Simon Hunt54442fa2015-01-26 14:17:38 -080095
96 panels[id] = p;
97
98 function showPanel(cb) {
99 var endCb = fs.isF(cb) || noop;
100 p.on = true;
101 p.el.transition().duration(p.settings.xtnTime)
102 .each('end', endCb)
103 .style(p.settings.edge, pxShow(p))
104 .style('opacity', 1);
105 }
106
107 function hidePanel(cb) {
108 var endCb = fs.isF(cb) || noop;
109 p.on = false;
110 p.el.transition().duration(p.settings.xtnTime)
111 .each('end', endCb)
112 .style(p.settings.edge, pxHide(p))
113 .style('opacity', 0);
114 }
115
Simon Hunt5724fb42015-02-05 16:59:40 -0800116 function togglePanel(cb) {
117 if (p.on) {
118 hidePanel(cb);
119 } else {
120 showPanel(cb);
121 }
122 }
123
Simon Hunt54442fa2015-01-26 14:17:38 -0800124 function emptyPanel() {
125 return p.el.html('');
126 }
127
128 function appendPanel(what) {
129 return p.el.append(what);
130 }
131
132 function panelWidth(w) {
133 if (w === undefined) {
134 return widthVal(p);
135 }
136 p.el.style('width', w + 'px');
137 }
138
139 function panelHeight(h) {
140 if (h === undefined) {
141 return heightVal(p);
142 }
143 p.el.style('height', h + 'px');
144 }
145
146 function panelIsVisible() {
147 return p.on;
148 }
149
Simon Hunt08f841d02015-02-10 14:39:20 -0800150 function classed(cls, bool) {
151 return p.el.classed(cls, bool);
152 }
153
Simon Hunt4b668592015-01-29 17:33:53 -0800154 function panelEl() {
155 return p.el;
156 }
157
Simon Hunt54442fa2015-01-26 14:17:38 -0800158 return api;
159 }
160
161 function removePanel(id) {
162 panelLayer.select('#' + id).remove();
163 delete panels[id];
164 }
165
Simon Hunt988934e2015-01-23 11:49:24 -0800166 angular.module('onosLayer')
Simon Hunt54442fa2015-01-26 14:17:38 -0800167 .factory('PanelService', ['$log', 'FnService', function (_$log_, _fs_) {
Simon Hunt988934e2015-01-23 11:49:24 -0800168 $log = _$log_;
Simon Hunt54442fa2015-01-26 14:17:38 -0800169 fs = _fs_;
Simon Hunt988934e2015-01-23 11:49:24 -0800170
Simon Hunt54442fa2015-01-26 14:17:38 -0800171 function createPanel(id, opts) {
Simon Hunt988934e2015-01-23 11:49:24 -0800172 var settings = angular.extend({}, defaultSettings, opts);
Simon Hunt54442fa2015-01-26 14:17:38 -0800173 if (!id) {
174 $log.warn('createPanel: no ID given');
175 return null;
Simon Hunt988934e2015-01-23 11:49:24 -0800176 }
Simon Hunt54442fa2015-01-26 14:17:38 -0800177 if (panels[id]) {
178 $log.warn('Panel with ID "' + id + '" already exists');
179 return null;
Simon Hunt988934e2015-01-23 11:49:24 -0800180 }
Simon Hunt54442fa2015-01-26 14:17:38 -0800181 $log.debug('creating panel:', id, settings);
182 return makePanel(id, settings);
183 }
Simon Hunt988934e2015-01-23 11:49:24 -0800184
Simon Hunt54442fa2015-01-26 14:17:38 -0800185 function destroyPanel(id) {
186 if (panels[id]) {
187 $log.debug('destroying panel:', id);
188 removePanel(id);
189 } else {
190 $log.debug('no panel to destroy:', id);
Simon Hunt988934e2015-01-23 11:49:24 -0800191 }
Simon Hunt988934e2015-01-23 11:49:24 -0800192 }
193
194 return {
Simon Hunt54442fa2015-01-26 14:17:38 -0800195 init: init,
196 createPanel: createPanel,
197 destroyPanel: destroyPanel
Simon Hunt988934e2015-01-23 11:49:24 -0800198 };
199 }]);
200
201}());