Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 1 | /* |
| 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 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, |
| 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'); |
| 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 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, |
| 74 | el: null |
| 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 | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 84 | isVisible: panelIsVisible, |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 85 | classed: classed, |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 86 | el: panelEl |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 87 | }; |
| 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 Hunt | 626d210 | 2015-01-29 11:54:50 -0800 | [diff] [blame] | 97 | if (p.settings.height) { |
| 98 | panelHeight(p.settings.height); |
| 99 | } |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 100 | |
| 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 Cole | be8b9a4 | 2015-02-24 12:12:00 -0800 | [diff] [blame] | 113 | var endCb = fs.isF(cb) || noop, |
| 114 | endOpacity = p.settings.fade ? 0 : 1; |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 115 | p.on = false; |
| 116 | p.el.transition().duration(p.settings.xtnTime) |
| 117 | .each('end', endCb) |
| 118 | .style(p.settings.edge, pxHide(p)) |
Bri Prebilic Cole | be8b9a4 | 2015-02-24 12:12:00 -0800 | [diff] [blame] | 119 | .style('opacity', endOpacity); |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Simon Hunt | 5724fb4 | 2015-02-05 16:59:40 -0800 | [diff] [blame] | 122 | function togglePanel(cb) { |
| 123 | if (p.on) { |
| 124 | hidePanel(cb); |
| 125 | } else { |
| 126 | showPanel(cb); |
| 127 | } |
| 128 | } |
| 129 | |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 130 | function emptyPanel() { |
| 131 | return p.el.html(''); |
| 132 | } |
| 133 | |
| 134 | function appendPanel(what) { |
| 135 | return p.el.append(what); |
| 136 | } |
| 137 | |
| 138 | function panelWidth(w) { |
| 139 | if (w === undefined) { |
| 140 | return widthVal(p); |
| 141 | } |
| 142 | p.el.style('width', w + 'px'); |
| 143 | } |
| 144 | |
| 145 | function panelHeight(h) { |
| 146 | if (h === undefined) { |
| 147 | return heightVal(p); |
| 148 | } |
| 149 | p.el.style('height', h + 'px'); |
| 150 | } |
| 151 | |
| 152 | function panelIsVisible() { |
| 153 | return p.on; |
| 154 | } |
| 155 | |
Simon Hunt | 08f841d0 | 2015-02-10 14:39:20 -0800 | [diff] [blame] | 156 | function classed(cls, bool) { |
| 157 | return p.el.classed(cls, bool); |
| 158 | } |
| 159 | |
Simon Hunt | 4b66859 | 2015-01-29 17:33:53 -0800 | [diff] [blame] | 160 | function panelEl() { |
| 161 | return p.el; |
| 162 | } |
| 163 | |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 164 | return api; |
| 165 | } |
| 166 | |
| 167 | function removePanel(id) { |
| 168 | panelLayer.select('#' + id).remove(); |
| 169 | delete panels[id]; |
| 170 | } |
| 171 | |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 172 | angular.module('onosLayer') |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 173 | .factory('PanelService', ['$log', 'FnService', function (_$log_, _fs_) { |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 174 | $log = _$log_; |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 175 | fs = _fs_; |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 176 | |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 177 | function createPanel(id, opts) { |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 178 | var settings = angular.extend({}, defaultSettings, opts); |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 179 | if (!id) { |
| 180 | $log.warn('createPanel: no ID given'); |
| 181 | return null; |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 182 | } |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 183 | if (panels[id]) { |
| 184 | $log.warn('Panel with ID "' + id + '" already exists'); |
| 185 | return null; |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 186 | } |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 187 | $log.debug('creating panel:', id, settings); |
| 188 | return makePanel(id, settings); |
| 189 | } |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 190 | |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 191 | function destroyPanel(id) { |
| 192 | if (panels[id]) { |
| 193 | $log.debug('destroying panel:', id); |
| 194 | removePanel(id); |
| 195 | } else { |
| 196 | $log.debug('no panel to destroy:', id); |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 197 | } |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | return { |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 201 | init: init, |
| 202 | createPanel: createPanel, |
| 203 | destroyPanel: destroyPanel |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 204 | }; |
| 205 | }]); |
| 206 | |
| 207 | }()); |