ohayolibs
Version:
Ohayo is a set of essential modules for ohayojp.
79 lines (67 loc) • 2.22 kB
text/typescript
import { Platform } from '@angular/cdk/platform';
import {
AfterViewInit,
ChangeDetectionStrategy,
Component,
ElementRef,
EventEmitter,
Input,
OnDestroy,
Output,
ViewEncapsulation,
} from '@angular/core';
import { Types } from '@antv/g2';
import { OhayoConfigService, InputNumber, NumberInput } from '@ohayo/util';
import { fromEvent, Subscription } from 'rxjs';
import { debounceTime } from 'rxjs/operators';
export class G2CustomComponent implements AfterViewInit, OnDestroy {
static ngAcceptInputType_delay: NumberInput;
static ngAcceptInputType_height: NumberInput;
static ngAcceptInputType_resizeTime: NumberInput;
private resize$: Subscription | null = null;
// #region fields
delay = 0;
height: number;
resizeTime = 0;
theme: string | Types.LooseObject;
readonly render = new EventEmitter<ElementRef>();
// tslint:disable-next-line:no-output-native
readonly resize = new EventEmitter<ElementRef>();
readonly destroy = new EventEmitter<ElementRef>();
// #endregion
constructor(private el: ElementRef, configSrv: OhayoConfigService, private platform: Platform) {
configSrv.attachKey(this, 'chart', 'theme');
}
private renderChart(): void {
this.el.nativeElement.innerHTML = '';
this.render.emit(this.el);
this.installResizeEvent();
}
private installResizeEvent(): void {
if (this.resizeTime <= 0 || this.resize$) return;
this.resize$ = fromEvent(window, 'resize')
.pipe(debounceTime(Math.min(200, this.resizeTime)))
.subscribe(() => this.resize.emit(this.el));
}
ngAfterViewInit(): void {
if (!this.platform.isBrowser) {
return;
}
setTimeout(() => this.renderChart(), this.delay);
}
ngOnDestroy(): void {
this.destroy.emit(this.el);
if (this.resize$) this.resize$.unsubscribe();
}
}