From ac81ab05ba6b1e18ad96126ab21217bf0bc5754c Mon Sep 17 00:00:00 2001 From: crusader Date: Fri, 21 Sep 2018 18:28:34 +0900 Subject: [PATCH] auto height --- .../ui/directive/auto-height.directive.ts | 61 +++++++++++++++++++ @overflow/commons/ui/directive/index.ts | 3 + package-lock.json | 27 -------- .../component/zone-detail.component.html | 4 +- .../component/zone-detail.component.ts | 10 ++- yarn.lock | 4 ++ 6 files changed, 79 insertions(+), 30 deletions(-) create mode 100644 @overflow/commons/ui/directive/auto-height.directive.ts delete mode 100644 package-lock.json diff --git a/@overflow/commons/ui/directive/auto-height.directive.ts b/@overflow/commons/ui/directive/auto-height.directive.ts new file mode 100644 index 0000000..9b7bfae --- /dev/null +++ b/@overflow/commons/ui/directive/auto-height.directive.ts @@ -0,0 +1,61 @@ +// https://github.com/arthurvaverko/ngx-fill-height/tree/master/src/app/fill-height + +import { Directive, AfterViewInit, Input, HostListener, ElementRef, ChangeDetectorRef, OnInit, OnDestroy } from '@angular/core'; + +@Directive({ + selector: '[of-auto-height]' +}) +export class AutoHeightDirective implements OnInit, OnDestroy, AfterViewInit { + @Input() footerElement = null; + + @HostListener('window:resize', ['$event']) + onResize(event) { + this.calculateAndSetElementHeight(); + } + + constructor( + private elementRef: ElementRef, + private changeDetector: ChangeDetectorRef, + ) { + } + + ngOnInit(): void { + } + + ngAfterViewInit(): void { + this.calculateAndSetElementHeight(); + } + + ngOnDestroy(): void { + } + + /** + * forceCalculate + */ + public forceCalculate() { + this.calculateAndSetElementHeight(); + } + + private calculateAndSetElementHeight() { + this.changeDetector.detectChanges(); + + this.elementRef.nativeElement.style.overflow = 'auto'; + const windowHeight = window.innerHeight; + const elementOffsetTop = this.getElementOffsetTop(); + const elementMarginBottom = this.elementRef.nativeElement.style.marginBottom; + const footerElementMargin = this.getfooterElementMargin(); + + this.elementRef.nativeElement.style.height = windowHeight - footerElementMargin - elementOffsetTop + 'px'; + console.log([windowHeight, elementOffsetTop, elementMarginBottom, footerElementMargin, this.elementRef.nativeElement.style.height]); + } + + private getElementOffsetTop() { + return this.elementRef.nativeElement.getBoundingClientRect().top; + } + + private getfooterElementMargin() { + if (!this.footerElement) { return 0; } + const footerStyle = window.getComputedStyle(this.footerElement); + return parseInt(footerStyle.height, 10); + } +} diff --git a/@overflow/commons/ui/directive/index.ts b/@overflow/commons/ui/directive/index.ts index df4e638..d9b4430 100644 --- a/@overflow/commons/ui/directive/index.ts +++ b/@overflow/commons/ui/directive/index.ts @@ -1,2 +1,5 @@ +import { AutoHeightDirective } from './auto-height.directive'; + export const DIRECTIVES = [ + AutoHeightDirective, ]; diff --git a/package-lock.json b/package-lock.json deleted file mode 100644 index 30eefef..0000000 --- a/package-lock.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "name": "scanner-app", - "version": "0.0.0", - "lockfileVersion": 1, - "requires": true, - "dependencies": { - "ngx-perfect-scrollbar": { - "version": "6.3.1", - "resolved": "https://nexus.loafle.net/repository/npm-all/ngx-perfect-scrollbar/-/ngx-perfect-scrollbar-6.3.1.tgz", - "integrity": "sha512-kgHT0A1pDnZO5CxB4TOwflHb1Q152wZ3Nec0Zf7d29bgA1kAFl6oK8wFmYtGWeaFNhSCExus6TOq3sWN3xRQig==", - "requires": { - "perfect-scrollbar": "1.4.0", - "resize-observer-polyfill": "1.5.0" - } - }, - "perfect-scrollbar": { - "version": "1.4.0", - "resolved": "https://nexus.loafle.net/repository/npm-all/perfect-scrollbar/-/perfect-scrollbar-1.4.0.tgz", - "integrity": "sha512-/2Sk/khljhdrsamjJYS5NjrH+GKEHEwh7zFSiYyxROyYKagkE4kSn2zDQDRTOMo8mpT2jikxx6yI1dG7lNP/hw==" - }, - "resize-observer-polyfill": { - "version": "1.5.0", - "resolved": "https://nexus.loafle.net/repository/npm-all/resize-observer-polyfill/-/resize-observer-polyfill-1.5.0.tgz", - "integrity": "sha512-M2AelyJDVR/oLnToJLtuDJRBBWUGUvvGigj1411hXhAdyFWqMaqHp7TixW3FpiLuVaikIcR1QL+zqoJoZlOgpg==" - } - } -} diff --git a/src/commons/component/zone-detail.component.html b/src/commons/component/zone-detail.component.html index 1756500..b7ba895 100644 --- a/src/commons/component/zone-detail.component.html +++ b/src/commons/component/zone-detail.component.html @@ -14,7 +14,7 @@ - + @@ -44,7 +44,7 @@ -
+
  • diff --git a/src/commons/component/zone-detail.component.ts b/src/commons/component/zone-detail.component.ts index d36236a..41f78d8 100644 --- a/src/commons/component/zone-detail.component.ts +++ b/src/commons/component/zone-detail.component.ts @@ -1,5 +1,6 @@ -import { Component, Input, OnInit, Output, EventEmitter } from '@angular/core'; +import { Component, Input, OnInit, Output, EventEmitter, ViewChild } from '@angular/core'; import { Zone, Host } from '@overflow/model/discovery'; +import { AutoHeightDirective } from '@overflow/commons/ui/directive/auto-height.directive'; const IPCIDR = require('ip-cidr'); @@ -12,6 +13,9 @@ export class ZoneDetailComponent implements OnInit { @Input() zone: Zone; @Output() otherHostSelect = new EventEmitter(); + + @ViewChild(AutoHeightDirective) autoHeightDirective; + ipRange: string; constructor( @@ -31,4 +35,8 @@ export class ZoneDetailComponent implements OnInit { this.otherHostSelect.emit(host); } + onTabViewChange($event) { + this.autoHeightDirective.forceCalculate(); + } + } diff --git a/yarn.lock b/yarn.lock index 8f032c9..35505fe 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2004,6 +2004,10 @@ crypto-random-string@^1.0.0: version "1.0.0" resolved "https://nexus.loafle.net/repository/npm-all/crypto-random-string/-/crypto-random-string-1.0.0.tgz#a230f64f568310e1498009940790ec99545bca7e" +css-element-queries@^1.0.2: + version "1.0.2" + resolved "https://nexus.loafle.net/repository/npm-all/css-element-queries/-/css-element-queries-1.0.2.tgz#a32edfee4a5023688ef4d45f402077306d51d44c" + css-parse@1.7.x: version "1.7.0" resolved "https://nexus.loafle.net/repository/npm-all/css-parse/-/css-parse-1.7.0.tgz#321f6cf73782a6ff751111390fc05e2c657d8c9b"