Sean Condon | 28ecc5f | 2018-06-25 12:50:16 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 | import { FnService } from '../util/fn.service'; |
| 17 | import { LoadingService } from '../layer/loading.service'; |
| 18 | import { LogService } from '../../log.service'; |
| 19 | import { WebSocketService } from '../remote/websocket.service'; |
| 20 | |
| 21 | |
| 22 | const noop = (): any => undefined; |
| 23 | |
| 24 | /** |
| 25 | ********* Static functions ********* |
| 26 | */ |
| 27 | function margin(p) { |
| 28 | return p.settings.margin; |
| 29 | } |
| 30 | |
| 31 | function hideMargin(p) { |
| 32 | return p.settings.hideMargin; |
| 33 | } |
| 34 | |
| 35 | function noPx(p, what) { |
| 36 | return Number(p.el.style(what).replace(/px$/, '')); |
| 37 | } |
| 38 | |
| 39 | function widthVal(p) { |
| 40 | return noPx(p, 'width'); |
| 41 | } |
| 42 | |
| 43 | function heightVal(p) { |
| 44 | return noPx(p, 'height'); |
| 45 | } |
| 46 | |
| 47 | function pxShow(p) { |
| 48 | return margin(p) + 'px'; |
| 49 | } |
| 50 | |
| 51 | function pxHide(p) { |
| 52 | return (-hideMargin(p) - widthVal(p) - (noPx(p, 'padding') * 2)) + 'px'; |
| 53 | } |
| 54 | |
| 55 | |
| 56 | /** |
| 57 | * Base model of panel view - implemented by Panel components |
| 58 | */ |
| 59 | export interface PanelBase { |
| 60 | showPanel(cb: any): void; |
| 61 | hidePanel(cb: any): void; |
| 62 | togglePanel(cb: any): void; |
| 63 | emptyPanel(): void; |
| 64 | appendPanel(what: any): void; |
| 65 | panelWidth(w: number): number; |
| 66 | panelHeight(h: number): number; |
| 67 | panelBBox(): string; |
| 68 | panelIsVisible(): boolean; |
| 69 | classed(cls: any, bool: boolean): boolean; |
| 70 | panelEl(): any; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * ONOS GUI -- Widget -- Panel Base class |
| 75 | * |
| 76 | * Replacing the panel service in the old implementation |
| 77 | */ |
| 78 | export abstract class PanelBaseImpl implements PanelBase { |
| 79 | |
| 80 | protected on: boolean; |
| 81 | protected el: any; |
| 82 | |
| 83 | constructor( |
| 84 | protected fs: FnService, |
| 85 | protected ls: LoadingService, |
| 86 | protected log: LogService, |
| 87 | protected wss: WebSocketService, |
| 88 | protected settings: any |
| 89 | ) { |
| 90 | // this.log.debug('Panel base class constructed'); |
| 91 | } |
| 92 | |
| 93 | showPanel(cb) { |
| 94 | const endCb = this.fs.isF(cb) || noop; |
| 95 | this.on = true; |
| 96 | this.el.transition().duration(this.settings.xtnTime) |
| 97 | .each('end', endCb) |
| 98 | .style(this.settings.edge, pxShow(this)) |
| 99 | .style('opacity', 1); |
| 100 | } |
| 101 | |
| 102 | hidePanel(cb) { |
| 103 | const endCb = this.fs.isF(cb) || noop; |
| 104 | const endOpacity = this.settings.fade ? 0 : 1; |
| 105 | this.on = false; |
| 106 | this.el.transition().duration(this.settings.xtnTime) |
| 107 | .each('end', endCb) |
| 108 | .style(this.settings.edge, pxHide(this)) |
| 109 | .style('opacity', endOpacity); |
| 110 | } |
| 111 | |
| 112 | togglePanel(cb): boolean { |
| 113 | if (this.on) { |
| 114 | this.hidePanel(cb); |
| 115 | } else { |
| 116 | this.showPanel(cb); |
| 117 | } |
| 118 | return this.on; |
| 119 | } |
| 120 | |
| 121 | emptyPanel(): string { |
| 122 | return this.el.text(''); |
| 123 | } |
| 124 | |
| 125 | appendPanel(what) { |
| 126 | return this.el.append(what); |
| 127 | } |
| 128 | |
| 129 | panelWidth(w: number): number { |
| 130 | if (w === undefined) { |
| 131 | return widthVal(this); |
| 132 | } |
| 133 | this.el.style('width', w + 'px'); |
| 134 | } |
| 135 | |
| 136 | panelHeight(h: number): number { |
| 137 | if (h === undefined) { |
| 138 | return heightVal(this); |
| 139 | } |
| 140 | this.el.style('height', h + 'px'); |
| 141 | } |
| 142 | |
| 143 | panelBBox(): string { |
| 144 | return this.el.node().getBoundingClientRect(); |
| 145 | } |
| 146 | |
| 147 | panelIsVisible(): boolean { |
| 148 | return this.on; |
| 149 | } |
| 150 | |
| 151 | classed(cls, bool): boolean { |
| 152 | return this.el.classed(cls, bool); |
| 153 | } |
| 154 | |
| 155 | panelEl() { |
| 156 | return this.el; |
| 157 | } |
| 158 | |
| 159 | |
| 160 | /** |
| 161 | * A dummy implementation of the lionFn until the response is received and the LION |
| 162 | * bundle is received from the WebSocket |
| 163 | */ |
| 164 | dummyLion(key: string): string { |
| 165 | return '%' + key + '%'; |
| 166 | } |
| 167 | } |