2020-11-18 11:07:56 +01:00
|
|
|
// (C) Copyright 2015 Moodle Pty Ltd.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2022-02-03 17:08:53 +01:00
|
|
|
import { Component, Input, OnChanges, SimpleChange, ChangeDetectionStrategy, ElementRef, OnInit } from '@angular/core';
|
2021-06-07 17:17:35 +02:00
|
|
|
import { SafeStyle } from '@angular/platform-browser';
|
|
|
|
import { DomSanitizer, Translate } from '@singletons';
|
2020-11-18 11:07:56 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Component to show a progress bar and its value.
|
|
|
|
*
|
|
|
|
* Example usage:
|
|
|
|
* <core-progress-bar [progress]="percentage"></core-progress-bar>
|
|
|
|
*/
|
|
|
|
@Component({
|
|
|
|
selector: 'core-progress-bar',
|
|
|
|
templateUrl: 'core-progress-bar.html',
|
|
|
|
styleUrls: ['progress-bar.scss'],
|
|
|
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
|
|
})
|
2022-02-03 17:08:53 +01:00
|
|
|
export class CoreProgressBarComponent implements OnInit, OnChanges {
|
2020-11-18 11:07:56 +01:00
|
|
|
|
2021-12-23 16:02:42 +01:00
|
|
|
@Input() progress!: number | string; // Percentage from 0 to 100. Negative number will show an indeterminate progress bar.
|
2020-11-18 11:07:56 +01:00
|
|
|
@Input() text?: string; // Percentage in text to be shown at the right. If not defined, progress will be used.
|
2021-05-14 15:56:17 +02:00
|
|
|
@Input() a11yText?: string; // Accessibility text to read before the percentage.
|
|
|
|
@Input() ariaDescribedBy?: string; // ID of the element that described the progress, if any.
|
2022-02-03 17:08:53 +01:00
|
|
|
@Input() color = '';
|
2021-05-14 15:56:17 +02:00
|
|
|
|
2020-11-18 11:07:56 +01:00
|
|
|
width?: SafeStyle;
|
2021-05-14 15:56:17 +02:00
|
|
|
progressBarValueText?: string;
|
|
|
|
|
2020-11-18 11:07:56 +01:00
|
|
|
protected textSupplied = false;
|
2022-02-03 17:08:53 +01:00
|
|
|
protected element: HTMLElement;
|
|
|
|
|
|
|
|
constructor(elementRef: ElementRef) {
|
|
|
|
this.element = elementRef.nativeElement;
|
|
|
|
}
|
2020-11-18 11:07:56 +01:00
|
|
|
|
|
|
|
/**
|
2022-02-03 17:08:53 +01:00
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ngOnInit(): void {
|
|
|
|
if (this.color) {
|
|
|
|
this.element.classList.add('ion-color', 'ion-color-' + this.color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
2020-11-18 11:07:56 +01:00
|
|
|
*/
|
|
|
|
ngOnChanges(changes: { [name: string]: SimpleChange }): void {
|
2022-02-03 17:08:53 +01:00
|
|
|
if (changes.color) {
|
|
|
|
this.element.classList.remove('ion-color', 'ion-color-' + changes.color.previousValue);
|
|
|
|
if (changes.color.currentValue) {
|
|
|
|
this.element.classList.add('ion-color', 'ion-color-' + changes.color.currentValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-16 10:46:40 +01:00
|
|
|
if (changes.text && changes.text.currentValue !== undefined) {
|
2020-11-18 11:07:56 +01:00
|
|
|
// User provided a custom text, don't use default.
|
|
|
|
this.textSupplied = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (changes.progress) {
|
|
|
|
// Progress has changed.
|
|
|
|
if (typeof this.progress == 'string') {
|
|
|
|
this.progress = parseInt(this.progress, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.progress < 0 || isNaN(this.progress)) {
|
|
|
|
this.progress = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.progress != -1) {
|
|
|
|
// Remove decimals.
|
|
|
|
this.progress = Math.floor(this.progress);
|
|
|
|
|
|
|
|
if (!this.textSupplied) {
|
|
|
|
this.text = String(this.progress);
|
|
|
|
}
|
|
|
|
|
2021-06-07 17:17:35 +02:00
|
|
|
this.width = DomSanitizer.bypassSecurityTrustStyle(this.progress + '%');
|
2020-11-18 11:07:56 +01:00
|
|
|
}
|
|
|
|
}
|
2021-05-14 15:56:17 +02:00
|
|
|
|
|
|
|
if (changes.text || changes.progress || changes.a11yText) {
|
|
|
|
this.progressBarValueText = (this.a11yText ? Translate.instant(this.a11yText) + ' ' : '') +
|
|
|
|
Translate.instant('core.percentagenumber', { $a: this.text });
|
|
|
|
}
|
2020-11-18 11:07:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|