|
@@ -1,4 +1,4 @@
|
|
|
-import { Component, OnDestroy, OnInit, HostListener } from '@angular/core';
|
|
|
|
|
|
|
+import {Component, OnDestroy, OnInit, HostListener, AfterViewInit, NgZone, ElementRef, ViewChild} from '@angular/core';
|
|
|
import { ActivatedRoute } from '@angular/router';
|
|
import { ActivatedRoute } from '@angular/router';
|
|
|
import * as moment from 'moment-timezone';
|
|
import * as moment from 'moment-timezone';
|
|
|
import { GraphLoader } from '../../shared/graph-loading/graphloader';
|
|
import { GraphLoader } from '../../shared/graph-loading/graphloader';
|
|
@@ -16,7 +16,7 @@ import type { View } from 'vega';
|
|
|
templateUrl: './sensor.component.html',
|
|
templateUrl: './sensor.component.html',
|
|
|
styleUrls: ['./sensor.component.scss']
|
|
styleUrls: ['./sensor.component.scss']
|
|
|
})
|
|
})
|
|
|
-export class SensorComponent implements OnInit, OnDestroy {
|
|
|
|
|
|
|
+export class SensorComponent implements OnInit, OnDestroy, AfterViewInit {
|
|
|
|
|
|
|
|
sensorId: number;
|
|
sensorId: number;
|
|
|
unitId: number;
|
|
unitId: number;
|
|
@@ -32,6 +32,7 @@ export class SensorComponent implements OnInit, OnDestroy {
|
|
|
showIntervalError = false;
|
|
showIntervalError = false;
|
|
|
private resizeObserver!: ResizeObserver; // Will watch the container size
|
|
private resizeObserver!: ResizeObserver; // Will watch the container size
|
|
|
graphView;
|
|
graphView;
|
|
|
|
|
+ @ViewChild('viewPanel') viewElement!: ElementRef;
|
|
|
|
|
|
|
|
constructor(
|
|
constructor(
|
|
|
private activatedRoute: ActivatedRoute,
|
|
private activatedRoute: ActivatedRoute,
|
|
@@ -39,6 +40,7 @@ export class SensorComponent implements OnInit, OnDestroy {
|
|
|
private toastService: ToastService,
|
|
private toastService: ToastService,
|
|
|
private sensorsService: SensorsService,
|
|
private sensorsService: SensorsService,
|
|
|
private route: ActivatedRoute,
|
|
private route: ActivatedRoute,
|
|
|
|
|
+ private ngZone: NgZone
|
|
|
) {
|
|
) {
|
|
|
this.getInitData();
|
|
this.getInitData();
|
|
|
this.sensorsService.getUnitSensors({ unit_id: this.unitId }).subscribe(sensors => {
|
|
this.sensorsService.getUnitSensors({ unit_id: this.unitId }).subscribe(sensors => {
|
|
@@ -49,16 +51,25 @@ export class SensorComponent implements OnInit, OnDestroy {
|
|
|
}, err => this.toastService.showError(err.error.message));
|
|
}, err => this.toastService.showError(err.error.message));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ ngOnInit(): void {
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
ngAfterViewInit() {
|
|
ngAfterViewInit() {
|
|
|
- window.addEventListener('resize', () => console.log('raw'))
|
|
|
|
|
|
|
+ this.setupResizeObserver();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- /*
|
|
|
|
|
- Fires on every resize event
|
|
|
|
|
- */
|
|
|
|
|
- @HostListener('window:resize', ['$event'])
|
|
|
|
|
- public onWindowResize(event: UIEvent): void {
|
|
|
|
|
- this.onResize();
|
|
|
|
|
|
|
+ private setupResizeObserver() {
|
|
|
|
|
+ this.resizeObserver = new ResizeObserver((entries) => {
|
|
|
|
|
+ // Use NgZone.run if you need to update Angular variables/UI or requestAnimationFrame to sync with the browser paint
|
|
|
|
|
+ this.ngZone.run(() => {
|
|
|
|
|
+ this.onResize();
|
|
|
|
|
+ });
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // Start observing the native element
|
|
|
|
|
+ if (this.viewElement) {
|
|
|
|
|
+ this.resizeObserver.observe(this.viewElement.nativeElement);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -71,7 +82,7 @@ export class SensorComponent implements OnInit, OnDestroy {
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
this.sensorId = parseInt(this.activatedRoute.snapshot.paramMap.get('sensorId'), 10);
|
|
this.sensorId = parseInt(this.activatedRoute.snapshot.paramMap.get('sensorId'), 10);
|
|
|
- this.unitId = parseInt(this.activatedRoute.snapshot.paramMap.get('unitId'), 10);
|
|
|
|
|
|
|
+ this.unitId = parseInt(this.activatedRoute.snapshot.paramMap.get('unitId'), 10);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -79,9 +90,9 @@ export class SensorComponent implements OnInit, OnDestroy {
|
|
|
*/
|
|
*/
|
|
|
ngOnDestroy(): void {
|
|
ngOnDestroy(): void {
|
|
|
this.subscription.forEach(subs => subs.unsubscribe());
|
|
this.subscription.forEach(subs => subs.unsubscribe());
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- ngOnInit(): void {
|
|
|
|
|
|
|
+ if (this.resizeObserver) {
|
|
|
|
|
+ this.resizeObserver.disconnect();
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -149,11 +160,18 @@ export class SensorComponent implements OnInit, OnDestroy {
|
|
|
|
|
|
|
|
private onResize(): void {
|
|
private onResize(): void {
|
|
|
// resize graph width if window changes size
|
|
// resize graph width if window changes size
|
|
|
- const box = document.getElementById('view');
|
|
|
|
|
-
|
|
|
|
|
|
|
+ const box = this.viewElement.nativeElement;
|
|
|
if (this.graphView) {
|
|
if (this.graphView) {
|
|
|
const newWidth = box.getBoundingClientRect().width - 50;
|
|
const newWidth = box.getBoundingClientRect().width - 50;
|
|
|
- this.graphView.width(newWidth).height(300).runAsync();
|
|
|
|
|
|
|
+ this.graphView.width(newWidth).height(300).run();
|
|
|
|
|
+ console.log('Requested width : ', newWidth);
|
|
|
|
|
+ console.log('Got width: ', this.graphView.width());
|
|
|
|
|
+
|
|
|
|
|
+ // 3. Check if Vega "stole" pixels. If it did, force it again.
|
|
|
|
|
+ if (this.graphView.width() < newWidth - 50) {
|
|
|
|
|
+ this.graphView.width(newWidth).height(300).runAsync();
|
|
|
|
|
+ console.log('Calling resize twice');
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|