blob: 0678583343a3fc61442e5ad1edbfbd92a81c212d [file] [log] [blame]
Sean Condon83fc39f2018-04-19 18:56:13 +01001/*
Sean Condon5ca00262018-09-06 17:55:25 +01002 * Copyright 2018-present Open Networking Foundation
Sean Condon83fc39f2018-04-19 18:56:13 +01003 *
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 */
Bhavesh72ead492018-07-19 16:29:18 +053016import { AfterContentChecked, Directive, ElementRef, Inject } from '@angular/core';
Sean Condon83fc39f2018-04-19 18:56:13 +010017import { FnService } from '../util/fn.service';
Sean Condon5ca00262018-09-06 17:55:25 +010018import { LogService } from '../log.service';
Bhavesh72ead492018-07-19 16:29:18 +053019import { MastService } from '../mast/mast.service';
20import { HostListener } from '@angular/core';
21import * as d3 from 'd3';
Sean Condon83fc39f2018-04-19 18:56:13 +010022
23/**
24 * ONOS GUI -- Widget -- Table Resize Directive
25 */
26@Directive({
Sean Condon2aa86092018-07-16 09:04:05 +010027 selector: '[onosTableResize]',
Sean Condon83fc39f2018-04-19 18:56:13 +010028})
Bhavesh72ead492018-07-19 16:29:18 +053029export class TableResizeDirective implements AfterContentChecked {
Sean Condon83fc39f2018-04-19 18:56:13 +010030
Bhavesh72ead492018-07-19 16:29:18 +053031 pdg = 22;
32 tables: any;
Sean Condon2aa86092018-07-16 09:04:05 +010033
Bhavesh72ead492018-07-19 16:29:18 +053034 constructor(protected fs: FnService,
35 protected log: LogService,
36 protected mast: MastService,
37 protected el: ElementRef,
Sean Condon5ca00262018-09-06 17:55:25 +010038 @Inject('Window') private w: any) {
Bhavesh72ead492018-07-19 16:29:18 +053039
40 log.info('TableResizeDirective constructed');
Sean Condon83fc39f2018-04-19 18:56:13 +010041 }
42
Bhavesh72ead492018-07-19 16:29:18 +053043 ngAfterContentChecked() {
44 this.tables = {
45 thead: d3.select('div.table-header').select('table'),
46 tbody: d3.select('div.table-body').select('table')
47 };
48 this.windowSize(this.tables);
49 }
50
51 windowSize(tables: any) {
Sean Condon2aa86092018-07-16 09:04:05 +010052 const wsz = this.fs.windowSize(0, 30);
Bhavesh72ead492018-07-19 16:29:18 +053053 this.adjustTable(tables, wsz.width, wsz.height);
Sean Condon2aa86092018-07-16 09:04:05 +010054 }
Bhavesh72ead492018-07-19 16:29:18 +053055
Sean Condon87b78502018-09-17 20:53:24 +010056 @HostListener('window:resize', ['$event.target'])
Bhavesh72ead492018-07-19 16:29:18 +053057 onResize(event: any) {
58 this.windowSize(this.tables);
59 return {
60 h: this.w.innerHeight,
61 w: this.w.innerWidth
62 };
63 }
64
65 adjustTable(tables: any, width: number, height: number) {
66 this._width(tables.thead, width + 'px');
67 this._width(tables.tbody, width + 'px');
68
69 this.setHeight(tables.thead, d3.select('div.table-body'), height);
70 }
71
72 _width(elem, width) {
73 elem.style('width', width);
74 }
75
76 setHeight(thead: any, body: any, height: number) {
77 const h = height - (this.mast.mastHeight +
78 this.fs.noPxStyle(d3.select('.tabular-header'), 'height') +
79 this.fs.noPxStyle(thead, 'height') + this.pdg);
80 body.style('height', h + 'px');
81 }
82
Sean Condon83fc39f2018-04-19 18:56:13 +010083}