coreui-angular-ex
Version:
CoreUI Components Library for Angular
113 lines (97 loc) • 2.81 kB
text/typescript
import {
booleanAttribute,
Component,
ElementRef,
HostBinding,
Input,
numberAttribute,
OnChanges,
OnInit,
Renderer2,
SimpleChanges
} from '@angular/core';
import { Colors } from '../coreui.types';
export class ProgressBarComponent implements OnInit, OnChanges {
/**
* Use to animate the stripes right to left via CSS3 animations.
* @type boolean
*/
animated: string | boolean = false;
/**
* Sets the color context of the component to one of CoreUI’s themed colors.
* @values 'primary', 'secondary', 'success', 'danger', 'warning', 'info', 'dark', 'light'
*/
color?: Colors;
// TODO: check if this is necessary.
precision: string | number = 0;
/**
* The percent to progress the ProgressBar.
* @type number
*/
value: string | number = 0;
/**
* Set the progress bar variant to optional striped.
* @values 'striped'
*/
variant?: 'striped';
/**
* Set default html role attribute.
* @type string
*/
role = 'progressbar';
private state = {
percent: 0,
min: 0,
max: 100
};
constructor(
private renderer: Renderer2,
private hostElement: ElementRef
) { }
get min(): number {
return this.state.min;
}
set min(value: number) {
this.state.min = isNaN(value) ? 0 : value;
}
get max(): number {
return this.state.max;
}
set max(value: number) {
this.state.max = isNaN(value) || value <= 0 || value === this.min ? 100 : value;
}
get hostClasses(): any {
return {
'progress-bar': true,
'progress-bar-animated': this.animated,
[`progress-bar-${this.variant}`]: !!this.variant,
[`bg-${this.color}`]: !!this.color
};
}
ngOnInit(): void {
this.setValues();
}
setPercent(): void {
this.state.percent = +((<number>this.value / (this.max - this.min)) * 100).toFixed(<number>this.precision);
}
setValues(): void {
this.setPercent();
const host: HTMLElement = this.hostElement.nativeElement;
this.renderer.setStyle(host, 'width', `${this.state.percent}%`);
this.renderer.setAttribute(host, 'aria-valuenow', String(this.value));
this.renderer.setAttribute(host, 'aria-valuemin', String(this.min));
this.renderer.setAttribute(host, 'aria-valuemax', String(this.max));
}
public ngOnChanges(changes: SimpleChanges): void {
this.setValues();
}
}