Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 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 Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 23 | var $log, fs; |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 24 | |
| 25 | var defaultSettings = { |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 26 | edge: 'right', |
| 27 | width: 200, |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 28 | margin: 20, |
Bri Prebilic Cole | be8b9a4 | 2015-02-24 12:12:00 -0800 | [diff] [blame] | 29 | hideMargin: 20, |
| 30 | xtnTime: 750, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 31 | fade: true, |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 32 | }; |
| 33 | |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 34 | var panels, |
| 35 | panelLayer; |
| 36 | |
| 37 | |
| 38 | function init() { |
| 39 | panelLayer = d3.select('#floatpanels'); |
Simon Hunt | 239f09e | 2017-05-18 13:10:09 -0700 | [diff] [blame] | 40 | panelLayer.text(''); |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 41 | panels = {}; |
| 42 | } |
| 43 | |
| 44 | // helpers for panel |
| 45 | function noop() {} |
| 46 | |
| 47 | function margin(p) { |
| 48 | return p.settings.margin; |
| 49 | } |
Bri Prebilic Cole | be8b9a4 | 2015-02-24 12:12:00 -0800 | [diff] [blame] | 50 | function hideMargin(p) { |
| 51 | return p.settings.hideMargin; |
| 52 | } |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 53 | 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 Cole | be8b9a4 | 2015-02-24 12:12:00 -0800 | [diff] [blame] | 66 | return (-hideMargin(p) - widthVal(p) - (noPx(p, 'padding') * 2)) + 'px'; |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | function makePanel(id, settings) { |
| 70 | var p = { |
| 71 | id: id, |
| 72 | settings: settings, |
| 73 | on: false, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 74 | el: null, |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 75 | }, |
| 76 | api = { |
| 77 | show: showPanel, |
| 78 | hide: hidePanel, |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 79 | toggle: togglePanel, |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 80 | empty: emptyPanel, |
| 81 | append: appendPanel, |
| 82 | width: panelWidth, |
| 83 | height: panelHeight, |
Simon Hunt | 8d47a5c | 2016-06-15 12:56:50 -0700 | [diff] [blame] | 84 | bbox: panelBBox, |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 85 | isVisible: panelIsVisible, |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 86 | classed: classed, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 87 | el: panelEl, |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 88 | }; |
| 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 Hunt | 626d210 | 2015-01-29 11:54:50 -0800 | [diff] [blame] | 98 | if (p.settings.height) { |
| 99 | panelHeight(p.settings.height); |
| 100 | } |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 101 | |
| 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 Cole | be8b9a4 | 2015-02-24 12:12:00 -0800 | [diff] [blame] | 114 | var endCb = fs.isF(cb) || noop, |
| 115 | endOpacity = p.settings.fade ? 0 : 1; |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 116 | p.on = false; |
| 117 | p.el.transition().duration(p.settings.xtnTime) |
| 118 | .each('end', endCb) |
| 119 | .style(p.settings.edge, pxHide(p)) |
Bri Prebilic Cole | be8b9a4 | 2015-02-24 12:12:00 -0800 | [diff] [blame] | 120 | .style('opacity', endOpacity); |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 123 | function togglePanel(cb) { |
| 124 | if (p.on) { |
| 125 | hidePanel(cb); |
| 126 | } else { |
| 127 | showPanel(cb); |
| 128 | } |
Simon Hunt | ee7a3ce | 2015-04-09 13:28:37 -0700 | [diff] [blame] | 129 | return p.on; |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 130 | } |
| 131 | |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 132 | function emptyPanel() { |
Simon Hunt | 239f09e | 2017-05-18 13:10:09 -0700 | [diff] [blame] | 133 | return p.el.text(''); |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 134 | } |
| 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 Hunt | 8d47a5c | 2016-06-15 12:56:50 -0700 | [diff] [blame] | 154 | function panelBBox() { |
| 155 | return p.el.node().getBoundingClientRect(); |
| 156 | } |
| 157 | |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 158 | function panelIsVisible() { |
| 159 | return p.on; |
| 160 | } |
| 161 | |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 162 | function classed(cls, bool) { |
| 163 | return p.el.classed(cls, bool); |
| 164 | } |
| 165 | |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 166 | function panelEl() { |
| 167 | return p.el; |
| 168 | } |
| 169 | |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 170 | return api; |
| 171 | } |
| 172 | |
| 173 | function removePanel(id) { |
| 174 | panelLayer.select('#' + id).remove(); |
| 175 | delete panels[id]; |
| 176 | } |
| 177 | |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 178 | angular.module('onosLayer') |
Simon Hunt | 75c2568 | 2015-11-02 16:49:56 -0800 | [diff] [blame] | 179 | .factory('PanelService', |
| 180 | ['$log', '$window', 'FnService', |
| 181 | |
| 182 | function (_$log_, _$window_, _fs_) { |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 183 | $log = _$log_; |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 184 | fs = _fs_; |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 185 | |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 186 | function createPanel(id, opts) { |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 187 | var settings = angular.extend({}, defaultSettings, opts); |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 188 | if (!id) { |
| 189 | $log.warn('createPanel: no ID given'); |
| 190 | return null; |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 191 | } |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 192 | if (panels[id]) { |
| 193 | $log.warn('Panel with ID "' + id + '" already exists'); |
| 194 | return null; |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 195 | } |
Simon Hunt | 4deb0e8 | 2015-06-10 16:18:25 -0700 | [diff] [blame] | 196 | if (fs.debugOn('widget')) { |
| 197 | $log.debug('creating panel:', id, settings); |
| 198 | } |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 199 | return makePanel(id, settings); |
| 200 | } |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 201 | |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 202 | function destroyPanel(id) { |
| 203 | if (panels[id]) { |
Simon Hunt | 4deb0e8 | 2015-06-10 16:18:25 -0700 | [diff] [blame] | 204 | if (fs.debugOn('widget')) { |
| 205 | $log.debug('destroying panel:', id); |
| 206 | } |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 207 | removePanel(id); |
| 208 | } else { |
Simon Hunt | 4deb0e8 | 2015-06-10 16:18:25 -0700 | [diff] [blame] | 209 | if (fs.debugOn('widget')) { |
| 210 | $log.debug('no panel to destroy:', id); |
| 211 | } |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 212 | } |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | return { |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 216 | init: init, |
| 217 | createPanel: createPanel, |
Steven Burrows | 1c2a968 | 2017-07-14 16:52:46 +0100 | [diff] [blame] | 218 | destroyPanel: destroyPanel, |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 219 | }; |
| 220 | }]); |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 221 | }()); |