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, |
| 29 | xtnTime: 750 |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 30 | }; |
| 31 | |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 32 | 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, |
| 78 | isVisible: panelIsVisible |
| 79 | }; |
| 80 | |
| 81 | p.el = panelLayer.append('div') |
| 82 | .attr('id', id) |
| 83 | .attr('class', 'floatpanel') |
| 84 | .style('opacity', 0); |
| 85 | |
| 86 | // has to be called after el is set |
| 87 | p.el.style(p.settings.edge, pxHide(p)); |
| 88 | panelWidth(p.settings.width); |
Simon Hunt | 626d210 | 2015-01-29 11:54:50 -0800 | [diff] [blame] | 89 | if (p.settings.height) { |
| 90 | panelHeight(p.settings.height); |
| 91 | } |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 92 | |
| 93 | panels[id] = p; |
| 94 | |
| 95 | function showPanel(cb) { |
| 96 | var endCb = fs.isF(cb) || noop; |
| 97 | p.on = true; |
| 98 | p.el.transition().duration(p.settings.xtnTime) |
| 99 | .each('end', endCb) |
| 100 | .style(p.settings.edge, pxShow(p)) |
| 101 | .style('opacity', 1); |
| 102 | } |
| 103 | |
| 104 | function hidePanel(cb) { |
| 105 | var endCb = fs.isF(cb) || noop; |
| 106 | p.on = false; |
| 107 | p.el.transition().duration(p.settings.xtnTime) |
| 108 | .each('end', endCb) |
| 109 | .style(p.settings.edge, pxHide(p)) |
| 110 | .style('opacity', 0); |
| 111 | } |
| 112 | |
| 113 | function emptyPanel() { |
| 114 | return p.el.html(''); |
| 115 | } |
| 116 | |
| 117 | function appendPanel(what) { |
| 118 | return p.el.append(what); |
| 119 | } |
| 120 | |
| 121 | function panelWidth(w) { |
| 122 | if (w === undefined) { |
| 123 | return widthVal(p); |
| 124 | } |
| 125 | p.el.style('width', w + 'px'); |
| 126 | } |
| 127 | |
| 128 | function panelHeight(h) { |
| 129 | if (h === undefined) { |
| 130 | return heightVal(p); |
| 131 | } |
| 132 | p.el.style('height', h + 'px'); |
| 133 | } |
| 134 | |
| 135 | function panelIsVisible() { |
| 136 | return p.on; |
| 137 | } |
| 138 | |
| 139 | return api; |
| 140 | } |
| 141 | |
| 142 | function removePanel(id) { |
| 143 | panelLayer.select('#' + id).remove(); |
| 144 | delete panels[id]; |
| 145 | } |
| 146 | |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 147 | angular.module('onosLayer') |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 148 | .factory('PanelService', ['$log', 'FnService', function (_$log_, _fs_) { |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 149 | $log = _$log_; |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 150 | fs = _fs_; |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 151 | |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 152 | function createPanel(id, opts) { |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 153 | var settings = angular.extend({}, defaultSettings, opts); |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 154 | if (!id) { |
| 155 | $log.warn('createPanel: no ID given'); |
| 156 | return null; |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 157 | } |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 158 | if (panels[id]) { |
| 159 | $log.warn('Panel with ID "' + id + '" already exists'); |
| 160 | return null; |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 161 | } |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 162 | $log.debug('creating panel:', id, settings); |
| 163 | return makePanel(id, settings); |
| 164 | } |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 165 | |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 166 | function destroyPanel(id) { |
| 167 | if (panels[id]) { |
| 168 | $log.debug('destroying panel:', id); |
| 169 | removePanel(id); |
| 170 | } else { |
| 171 | $log.debug('no panel to destroy:', id); |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 172 | } |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | return { |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 176 | init: init, |
| 177 | createPanel: createPanel, |
| 178 | destroyPanel: destroyPanel |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 179 | }; |
| 180 | }]); |
| 181 | |
| 182 | }()); |