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, |
| 28 | height: 80, |
| 29 | margin: 20, |
| 30 | xtnTime: 750 |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 31 | }; |
| 32 | |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 33 | var panels, |
| 34 | panelLayer; |
| 35 | |
| 36 | |
| 37 | function init() { |
| 38 | panelLayer = d3.select('#floatpanels'); |
| 39 | panelLayer.html(''); |
| 40 | panels = {}; |
| 41 | } |
| 42 | |
| 43 | // helpers for panel |
| 44 | function noop() {} |
| 45 | |
| 46 | function margin(p) { |
| 47 | return p.settings.margin; |
| 48 | } |
| 49 | function noPx(p, what) { |
| 50 | return Number(p.el.style(what).replace(/px$/, '')); |
| 51 | } |
| 52 | function widthVal(p) { |
| 53 | return noPx(p, 'width'); |
| 54 | } |
| 55 | function heightVal(p) { |
| 56 | return noPx(p, 'height'); |
| 57 | } |
| 58 | function pxShow(p) { |
| 59 | return margin(p) + 'px'; |
| 60 | } |
| 61 | function pxHide(p) { |
| 62 | return (-margin(p) - widthVal(p)) + 'px'; |
| 63 | } |
| 64 | |
| 65 | function makePanel(id, settings) { |
| 66 | var p = { |
| 67 | id: id, |
| 68 | settings: settings, |
| 69 | on: false, |
| 70 | el: null |
| 71 | }, |
| 72 | api = { |
| 73 | show: showPanel, |
| 74 | hide: hidePanel, |
| 75 | empty: emptyPanel, |
| 76 | append: appendPanel, |
| 77 | width: panelWidth, |
| 78 | height: panelHeight, |
| 79 | isVisible: panelIsVisible |
| 80 | }; |
| 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); |
| 90 | panelHeight(p.settings.height); |
| 91 | |
| 92 | panels[id] = p; |
| 93 | |
| 94 | function showPanel(cb) { |
| 95 | var endCb = fs.isF(cb) || noop; |
| 96 | p.on = true; |
| 97 | p.el.transition().duration(p.settings.xtnTime) |
| 98 | .each('end', endCb) |
| 99 | .style(p.settings.edge, pxShow(p)) |
| 100 | .style('opacity', 1); |
| 101 | } |
| 102 | |
| 103 | function hidePanel(cb) { |
| 104 | var endCb = fs.isF(cb) || noop; |
| 105 | p.on = false; |
| 106 | p.el.transition().duration(p.settings.xtnTime) |
| 107 | .each('end', endCb) |
| 108 | .style(p.settings.edge, pxHide(p)) |
| 109 | .style('opacity', 0); |
| 110 | } |
| 111 | |
| 112 | function emptyPanel() { |
| 113 | return p.el.html(''); |
| 114 | } |
| 115 | |
| 116 | function appendPanel(what) { |
| 117 | return p.el.append(what); |
| 118 | } |
| 119 | |
| 120 | function panelWidth(w) { |
| 121 | if (w === undefined) { |
| 122 | return widthVal(p); |
| 123 | } |
| 124 | p.el.style('width', w + 'px'); |
| 125 | } |
| 126 | |
| 127 | function panelHeight(h) { |
| 128 | if (h === undefined) { |
| 129 | return heightVal(p); |
| 130 | } |
| 131 | p.el.style('height', h + 'px'); |
| 132 | } |
| 133 | |
| 134 | function panelIsVisible() { |
| 135 | return p.on; |
| 136 | } |
| 137 | |
| 138 | return api; |
| 139 | } |
| 140 | |
| 141 | function removePanel(id) { |
| 142 | panelLayer.select('#' + id).remove(); |
| 143 | delete panels[id]; |
| 144 | } |
| 145 | |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 146 | angular.module('onosLayer') |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 147 | .factory('PanelService', ['$log', 'FnService', function (_$log_, _fs_) { |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 148 | $log = _$log_; |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 149 | fs = _fs_; |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 150 | |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 151 | function createPanel(id, opts) { |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 152 | var settings = angular.extend({}, defaultSettings, opts); |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 153 | if (!id) { |
| 154 | $log.warn('createPanel: no ID given'); |
| 155 | return null; |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 156 | } |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 157 | if (panels[id]) { |
| 158 | $log.warn('Panel with ID "' + id + '" already exists'); |
| 159 | return null; |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 160 | } |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 161 | $log.debug('creating panel:', id, settings); |
| 162 | return makePanel(id, settings); |
| 163 | } |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 164 | |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 165 | function destroyPanel(id) { |
| 166 | if (panels[id]) { |
| 167 | $log.debug('destroying panel:', id); |
| 168 | removePanel(id); |
| 169 | } else { |
| 170 | $log.debug('no panel to destroy:', id); |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 171 | } |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | return { |
Simon Hunt | 54442fa | 2015-01-26 14:17:38 -0800 | [diff] [blame] | 175 | init: init, |
| 176 | createPanel: createPanel, |
| 177 | destroyPanel: destroyPanel |
Simon Hunt | 988934e | 2015-01-23 11:49:24 -0800 | [diff] [blame] | 178 | }; |
| 179 | }]); |
| 180 | |
| 181 | }()); |