ng-ytl-zorro-antd
Version:
An enterprise-class UI components based on Ant Design and Angular
114 lines (102 loc) • 4.42 kB
text/typescript
import {
forwardRef,
Component,
Input,
OnInit,
ViewEncapsulation,
} from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms';
export class NzProgressComponent implements ControlValueAccessor, OnInit {
_statusColorMap = {
normal : '#108ee9',
exception: '#ff5500',
success : '#87d068',
};
_pathString = '';
_pathStyle = {};
_circleStyle = {};
_percent = 0;
_hasFormat = false;
_rawStatus = 'normal';
// ngModel Access
onChange: (value: number) => void = () => null;
onTouched: () => void = () => null;
nzType = 'line';
nzStrokeWidth: number = this.nzType === 'line' ? 10 : 6;
nzWidth = 132;
nzShowInfo = true;
nzStatus = 'normal';
set _setFormat(value: (input: number) => string) {
this._format = value;
this._hasFormat = true;
}
_format = (percent: number) => percent + '%';
updateCircleStatus(): void {
const circleSize = this.nzWidth || 132;
this._circleStyle = {
'width.px' : circleSize,
'height.px' : circleSize,
'font-size.px': circleSize * 0.16 + 6,
};
const radius = 50 - this.nzStrokeWidth / 2;
const len = Math.PI * 2 * radius;
this._pathString = `M 50,50 m 0,-${radius}\n a ${radius},${radius} 0 1 1 0,${radius * 2}\n a ${radius},${radius} 0 1 1 0,-${radius * 2}`;
this._pathStyle = {
'stroke-dasharray' : `${len}px ${len}px`,
'stroke-dashoffset': (100 - this._percent) / 100 * len + 'px',
'transition' : 'stroke-dashoffset 0.3s ease 0s, stroke 0.3s ease'
};
}
writeValue(value: number): void {
this._percent = value == null ? 0 : +value;
if (this._percent === 100) {
this.nzStatus = 'success';
} else {
this.nzStatus = this._rawStatus;
}
if (this.nzType === 'circle') {
this.updateCircleStatus();
}
}
registerOnChange(fn: (_: number) => void): void {
this.onChange = fn;
}
registerOnTouched(fn: () => void): void {
this.onTouched = fn;
}
ngOnInit(): void {
this._rawStatus = this.nzStatus;
}
}