|
| 1 | +import { |
| 2 | + Component, |
| 3 | + HostBinding, |
| 4 | + Input, |
| 5 | + TemplateRef |
| 6 | +} from "@angular/core"; |
| 7 | + |
| 8 | +@Component({ |
| 9 | + selector: "ibm-progress-bar", |
| 10 | + template: ` |
| 11 | + <div |
| 12 | + *ngIf="label" |
| 13 | + class="bx--progress-bar__label" |
| 14 | + [id]="id"> |
| 15 | + <span class="bx--progress-bar__label-text"> |
| 16 | + <ng-container *ngIf="!isTemplate(label)">{{label}}</ng-container> |
| 17 | + <ng-template *ngIf="isTemplate(label)" [ngTemplateOutlet]="label"></ng-template> |
| 18 | + </span> |
| 19 | + <svg |
| 20 | + *ngIf="isFinished" |
| 21 | + fill="currentColor" |
| 22 | + ibmIcon="checkmark--filled" |
| 23 | + class="bx--progress-bar__status-icon"> |
| 24 | + </svg> |
| 25 | + <svg |
| 26 | + *ngIf="isError" |
| 27 | + fill="currentColor" |
| 28 | + ibmIcon="error--filled" |
| 29 | + class="bx--progress-bar__status-icon"> |
| 30 | + </svg> |
| 31 | + </div> |
| 32 | + <div |
| 33 | + class="bx--progress-bar__track" |
| 34 | + role="progressbar" |
| 35 | + [attr.aria-invalid]="isError" |
| 36 | + [attr.labelledby]="id" |
| 37 | + [attr.describedby]="helperText ? helperId: null" |
| 38 | + [attr.aria-valuemin]="!indeterminate ? 0 : null" |
| 39 | + [attr.aria-valuemax]="!indeterminate ? max : null" |
| 40 | + [attr.aria-valuenow]="!indeterminate ? value : null"> |
| 41 | + <div |
| 42 | + class="bx--progress-bar__bar" |
| 43 | + [ngStyle]="{ |
| 44 | + 'transform': !isFinished && !isError ? percentage : null |
| 45 | + }"> |
| 46 | + </div> |
| 47 | + </div> |
| 48 | + <div |
| 49 | + [id]="helperId" |
| 50 | + *ngIf="helperText" |
| 51 | + class="bx--progress-bar__helper-text"> |
| 52 | + <ng-container *ngIf="!isTemplate(helperText)">{{helperText}}</ng-container> |
| 53 | + <ng-template *ngIf="isTemplate(helperText)" [ngTemplateOutlet]="helperText"></ng-template> |
| 54 | + </div> |
| 55 | + ` |
| 56 | +}) |
| 57 | +export class ProgressBar { |
| 58 | + /** |
| 59 | + * Current value |
| 60 | + */ |
| 61 | + @Input() set value(num: number | undefined) { |
| 62 | + this._value = num; |
| 63 | + // Validate number |
| 64 | + if (num > this.max) { |
| 65 | + this._value = this.max; |
| 66 | + } |
| 67 | + if (num < 0) { |
| 68 | + this._value = 0; |
| 69 | + } |
| 70 | + // Set values based on current state |
| 71 | + if (this.isError) { |
| 72 | + this._value = 0; |
| 73 | + } else if (this.isFinished) { |
| 74 | + this._value = this.max; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + get value() { |
| 79 | + return this._value; |
| 80 | + } |
| 81 | + |
| 82 | + get percentage() { |
| 83 | + return `scaleX(${this.value / this.max})`; |
| 84 | + } |
| 85 | + // Size |
| 86 | + @HostBinding("class.bx--progress-bar--big") get bigBar() { |
| 87 | + return this.size === "big"; |
| 88 | + } |
| 89 | + @HostBinding("class.bx--progress-bar--small") get smallBar() { |
| 90 | + return this.size === "small"; |
| 91 | + } |
| 92 | + // Type |
| 93 | + @HostBinding("class.bx--progress-bar--default") get defaultType() { |
| 94 | + return this.type === "default"; |
| 95 | + } |
| 96 | + @HostBinding("class.bx--progress-bar--indented") get indentedType() { |
| 97 | + return this.type === "indented"; |
| 98 | + } |
| 99 | + @HostBinding("class.bx--progress-bar--inline") get inlineType() { |
| 100 | + return this.type === "inline"; |
| 101 | + } |
| 102 | + // Status |
| 103 | + @HostBinding("class.bx--progress-bar--finished") get isFinished() { |
| 104 | + return this.status === "finished"; |
| 105 | + } |
| 106 | + @HostBinding("class.bx--progress-bar--error") get isError() { |
| 107 | + return this.status === "error"; |
| 108 | + } |
| 109 | + @HostBinding("class.bx--progress-bar--indeterminate") get indeterminate() { |
| 110 | + return this.value === undefined && !this.isFinished && !this.isError; |
| 111 | + } |
| 112 | + static progressBarCounter = 0; |
| 113 | + |
| 114 | + @Input() id = `progress-bar-${ProgressBar.progressBarCounter++}`; |
| 115 | + helperId = `progress-bar-helper-${ProgressBar.progressBarCounter}`; |
| 116 | + /** |
| 117 | + * Description of the progress bar |
| 118 | + */ |
| 119 | + @Input() label: string | TemplateRef<any>; |
| 120 | + /** |
| 121 | + * Current progress textual representation |
| 122 | + */ |
| 123 | + @Input() helperText: string | TemplateRef<any>; |
| 124 | + /** |
| 125 | + * Maximum value |
| 126 | + */ |
| 127 | + @Input() max = 100; |
| 128 | + /** |
| 129 | + * Alignment variant of the progress bar, default is `default` |
| 130 | + */ |
| 131 | + @Input() type: "default" | "inline" | "indented" = "default"; |
| 132 | + /** |
| 133 | + * Current status of the progress bar, default is `active` |
| 134 | + */ |
| 135 | + @Input() status: "active" | "finished" | "error" = "active"; |
| 136 | + /** |
| 137 | + * Size of the progress bar, default is `big` |
| 138 | + */ |
| 139 | + @Input() size: "small" | "big" = "big"; |
| 140 | + |
| 141 | + @HostBinding("class.bx--progress-bar") defaultClass = true; |
| 142 | + private _value = undefined; |
| 143 | + |
| 144 | + isTemplate(value) { |
| 145 | + return value instanceof TemplateRef; |
| 146 | + } |
| 147 | +} |
0 commit comments