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