blob: 5d3a9ebbbe5f6d317f066395b77895dbaf970917 [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 */
Sean Condonf4f54a12018-10-10 23:25:46 +010016import { AfterContentChecked, Directive, 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,
Sean Condon5ca00262018-09-06 17:55:25 +010037 @Inject('Window') private w: any) {
Bhavesh72ead492018-07-19 16:29:18 +053038
39 log.info('TableResizeDirective constructed');
Sean Condon83fc39f2018-04-19 18:56:13 +010040 }
41
Bhavesh72ead492018-07-19 16:29:18 +053042 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 Condon2aa86092018-07-16 09:04:05 +010051 const wsz = this.fs.windowSize(0, 30);
Bhavesh72ead492018-07-19 16:29:18 +053052 this.adjustTable(tables, wsz.width, wsz.height);
Sean Condon2aa86092018-07-16 09:04:05 +010053 }
Bhavesh72ead492018-07-19 16:29:18 +053054
Sean Condon87b78502018-09-17 20:53:24 +010055 @HostListener('window:resize', ['$event.target'])
Bhavesh72ead492018-07-19 16:29:18 +053056 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 Condon83fc39f2018-04-19 18:56:13 +010082}