ngx-eagle
Version:
UI component infrastructure and Design components for mobile and desktop Angular web applications.
159 lines (148 loc) • 4.48 kB
text/typescript
import {
AfterViewInit,
ChangeDetectionStrategy,
ChangeDetectorRef,
Component,
ElementRef,
Input,
OnChanges,
Renderer2,
SimpleChanges,
TemplateRef,
ViewChild,
} from '@angular/core';
import { NgxSize, NgxType } from './typings';
import { NgIf, NgTemplateOutlet } from '@angular/common';
export class ProgressComponent implements AfterViewInit, OnChanges {
ngxColor: string = '#1890FF';
ngxPercent: number = 0;
ngxSize: NgxSize = 'default';
ngxTimer: number = 0.5;
ngxType: NgxType = 'line';
template!: TemplateRef<void>;
lineProgressRef!: ElementRef;
circleProgressRef!: ElementRef;
constructor(
private cdr: ChangeDetectorRef,
private renderer: Renderer2,
private elementRef: ElementRef
) {}
ngAfterViewInit(): void {
if (this.ngxType === 'line') {
this.updateLineProgress();
} else {
this.updateCircleProgress();
}
this.cdr.markForCheck();
}
ngOnChanges(changes: SimpleChanges): void {
if (changes['ngxPercent']) {
if (this.ngxType === 'line') {
this.updateLineProgress();
} else {
this.updateCircleProgress();
}
}
}
private updateLineProgress(): void {
setTimeout(() => {
if (this.lineProgressRef) {
this.renderer.setStyle(
this.lineProgressRef.nativeElement,
'width',
`${this.ngxPercent}%`
);
this.renderer.setStyle(
this.lineProgressRef.nativeElement,
'background-color',
this.ngxColor
);
this.renderer.setStyle(
this.lineProgressRef.nativeElement,
'transition',
`width ${this.ngxTimer}s ease-in-out`
);
}
});
}
private updateCircleProgress(): void {
this.renderer.setStyle(this.elementRef.nativeElement, 'width', '100px');
const progress = Math.min(100, Math.max(0, this.ngxPercent));
const offset = 283 - (283 * progress) / 100;
if (this.circleProgressRef) {
this.renderer.setStyle(
this.circleProgressRef.nativeElement,
'stroke-dasharray',
'283'
);
this.renderer.setStyle(
this.circleProgressRef.nativeElement,
'stroke-dashoffset',
'283'
);
this.renderer.setStyle(
this.circleProgressRef.nativeElement,
'stroke',
this.ngxColor
);
setTimeout(() => {
this.renderer.setStyle(
this.circleProgressRef.nativeElement,
'stroke-dashoffset',
offset.toString()
);
this.renderer.setStyle(
this.circleProgressRef.nativeElement,
'transition',
`stroke-dashoffset ${this.ngxTimer}s ease-in-out`
);
});
}
}
}