blob: 4df0d7200d9c769acdcae9ac43ef2d27153eab27 [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,
74 empty: emptyPanel,
75 append: appendPanel,
76 width: panelWidth,
77 height: panelHeight,
Simon Hunt4b668592015-01-29 17:33:53 -080078 isVisible: panelIsVisible,
79 el: panelEl
Simon Hunt54442fa2015-01-26 14:17:38 -080080 };
81
82 p.el = panelLayer.append('div')
83 .attr('id', id)
84 .attr('class', 'floatpanel')
85 .style('opacity', 0);
86
87 // has to be called after el is set
88 p.el.style(p.settings.edge, pxHide(p));
89 panelWidth(p.settings.width);
Simon Hunt626d2102015-01-29 11:54:50 -080090 if (p.settings.height) {
91 panelHeight(p.settings.height);
92 }
Simon Hunt54442fa2015-01-26 14:17:38 -080093
94 panels[id] = p;
95
96 function showPanel(cb) {
97 var endCb = fs.isF(cb) || noop;
98 p.on = true;
99 p.el.transition().duration(p.settings.xtnTime)
100 .each('end', endCb)
101 .style(p.settings.edge, pxShow(p))
102 .style('opacity', 1);
103 }
104
105 function hidePanel(cb) {
106 var endCb = fs.isF(cb) || noop;
107 p.on = false;
108 p.el.transition().duration(p.settings.xtnTime)
109 .each('end', endCb)
110 .style(p.settings.edge, pxHide(p))
111 .style('opacity', 0);
112 }
113
114 function emptyPanel() {
115 return p.el.html('');
116 }
117
118 function appendPanel(what) {
119 return p.el.append(what);
120 }
121
122 function panelWidth(w) {
123 if (w === undefined) {
124 return widthVal(p);
125 }
126 p.el.style('width', w + 'px');
127 }
128
129 function panelHeight(h) {
130 if (h === undefined) {
131 return heightVal(p);
132 }
133 p.el.style('height', h + 'px');
134 }
135
136 function panelIsVisible() {
137 return p.on;
138 }
139
Simon Hunt4b668592015-01-29 17:33:53 -0800140 function panelEl() {
141 return p.el;
142 }
143
Simon Hunt54442fa2015-01-26 14:17:38 -0800144 return api;
145 }
146
147 function removePanel(id) {
148 panelLayer.select('#' + id).remove();
149 delete panels[id];
150 }
151
Simon Hunt988934e2015-01-23 11:49:24 -0800152 angular.module('onosLayer')
Simon Hunt54442fa2015-01-26 14:17:38 -0800153 .factory('PanelService', ['$log', 'FnService', function (_$log_, _fs_) {
Simon Hunt988934e2015-01-23 11:49:24 -0800154 $log = _$log_;
Simon Hunt54442fa2015-01-26 14:17:38 -0800155 fs = _fs_;
Simon Hunt988934e2015-01-23 11:49:24 -0800156
Simon Hunt54442fa2015-01-26 14:17:38 -0800157 function createPanel(id, opts) {
Simon Hunt988934e2015-01-23 11:49:24 -0800158 var settings = angular.extend({}, defaultSettings, opts);
Simon Hunt54442fa2015-01-26 14:17:38 -0800159 if (!id) {
160 $log.warn('createPanel: no ID given');
161 return null;
Simon Hunt988934e2015-01-23 11:49:24 -0800162 }
Simon Hunt54442fa2015-01-26 14:17:38 -0800163 if (panels[id]) {
164 $log.warn('Panel with ID "' + id + '" already exists');
165 return null;
Simon Hunt988934e2015-01-23 11:49:24 -0800166 }
Simon Hunt54442fa2015-01-26 14:17:38 -0800167 $log.debug('creating panel:', id, settings);
168 return makePanel(id, settings);
169 }
Simon Hunt988934e2015-01-23 11:49:24 -0800170
Simon Hunt54442fa2015-01-26 14:17:38 -0800171 function destroyPanel(id) {
172 if (panels[id]) {
173 $log.debug('destroying panel:', id);
174 removePanel(id);
175 } else {
176 $log.debug('no panel to destroy:', id);
Simon Hunt988934e2015-01-23 11:49:24 -0800177 }
Simon Hunt988934e2015-01-23 11:49:24 -0800178 }
179
180 return {
Simon Hunt54442fa2015-01-26 14:17:38 -0800181 init: init,
182 createPanel: createPanel,
183 destroyPanel: destroyPanel
Simon Hunt988934e2015-01-23 11:49:24 -0800184 };
185 }]);
186
187}());