Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 1 | /* |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 2 | * Copyright 2018-present Open Networking Foundation |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 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 | */ |
Sean Condon | f4f54a1 | 2018-10-10 23:25:46 +0100 | [diff] [blame] | 16 | import { AfterContentChecked, Directive, Inject } from '@angular/core'; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 17 | import { FnService } from '../util/fn.service'; |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 18 | import { LogService } from '../log.service'; |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 19 | import { MastService } from '../mast/mast.service'; |
| 20 | import { HostListener } from '@angular/core'; |
| 21 | import * as d3 from 'd3'; |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * ONOS GUI -- Widget -- Table Resize Directive |
| 25 | */ |
| 26 | @Directive({ |
Sean Condon | 2aa8609 | 2018-07-16 09:04:05 +0100 | [diff] [blame] | 27 | selector: '[onosTableResize]', |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 28 | }) |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 29 | export class TableResizeDirective implements AfterContentChecked { |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 30 | |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 31 | pdg = 22; |
| 32 | tables: any; |
Sean Condon | 2aa8609 | 2018-07-16 09:04:05 +0100 | [diff] [blame] | 33 | |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 34 | constructor(protected fs: FnService, |
| 35 | protected log: LogService, |
| 36 | protected mast: MastService, |
Sean Condon | 5ca0026 | 2018-09-06 17:55:25 +0100 | [diff] [blame] | 37 | @Inject('Window') private w: any) { |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 38 | |
| 39 | log.info('TableResizeDirective constructed'); |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 40 | } |
| 41 | |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 42 | ngAfterContentChecked() { |
| 43 | this.tables = { |
| 44 | thead: d3.select('div.table-header').select('table'), |
| 45 | tbody: d3.select('div.table-body').select('table') |
| 46 | }; |
| 47 | this.windowSize(this.tables); |
| 48 | } |
| 49 | |
| 50 | windowSize(tables: any) { |
Sean Condon | 2aa8609 | 2018-07-16 09:04:05 +0100 | [diff] [blame] | 51 | const wsz = this.fs.windowSize(0, 30); |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 52 | this.adjustTable(tables, wsz.width, wsz.height); |
Sean Condon | 2aa8609 | 2018-07-16 09:04:05 +0100 | [diff] [blame] | 53 | } |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 54 | |
Sean Condon | 87b7850 | 2018-09-17 20:53:24 +0100 | [diff] [blame] | 55 | @HostListener('window:resize', ['$event.target']) |
Bhavesh | 72ead49 | 2018-07-19 16:29:18 +0530 | [diff] [blame] | 56 | onResize(event: any) { |
| 57 | this.windowSize(this.tables); |
| 58 | return { |
| 59 | h: this.w.innerHeight, |
| 60 | w: this.w.innerWidth |
| 61 | }; |
| 62 | } |
| 63 | |
| 64 | adjustTable(tables: any, width: number, height: number) { |
| 65 | this._width(tables.thead, width + 'px'); |
| 66 | this._width(tables.tbody, width + 'px'); |
| 67 | |
| 68 | this.setHeight(tables.thead, d3.select('div.table-body'), height); |
| 69 | } |
| 70 | |
| 71 | _width(elem, width) { |
| 72 | elem.style('width', width); |
| 73 | } |
| 74 | |
| 75 | setHeight(thead: any, body: any, height: number) { |
| 76 | const h = height - (this.mast.mastHeight + |
| 77 | this.fs.noPxStyle(d3.select('.tabular-header'), 'height') + |
| 78 | this.fs.noPxStyle(thead, 'height') + this.pdg); |
| 79 | body.style('height', h + 'px'); |
| 80 | } |
| 81 | |
Sean Condon | 83fc39f | 2018-04-19 18:56:13 +0100 | [diff] [blame] | 82 | } |