| <template> |
| <div class="progress-bar-container bg-light hide-on-app-closed"> |
| <div class="progress" style="height: 20px; border-radius: 0"> |
| <div class="progress-bar progress-bar-striped progress-bar-animated" role="progressbar" |
| :style="{ width: progressPercent + '%' }" :aria-valuenow="progressPercent" aria-valuemin="0" |
| aria-valuemax="100"> |
| |
| <span v-if="displayProgress > 0"> |
| {{ displayMessage }} ({{ progressPercent }}%) |
| </span> |
| </div> |
| </div> |
| </div> |
| </template> |
| |
| <script lang="ts"> |
| import { Options, Vue } from "vue-class-component"; |
| import { Prop, Watch } from "vue-property-decorator"; |
| |
| const INACTIVITY_TIMEOUT_MS = 2 * 60 * 1000; |
| const COMPLETION_PAUSE_MS = 250; |
| const MIN_DISPLAY_PROGRESS = 0.05; |
| |
| |
| |
| |
| |
| |
| @Options({}) |
| export default class ProgressBar extends Vue { |
| |
| @Prop({ type: Number, default: 0 }) |
| progress!: number; |
| |
| |
| @Prop({ type: Boolean, default: false }) |
| active!: boolean; |
| |
| displayProgress = 0; |
| displayMessage = ""; |
| completionPauseActive = false; |
| completionPauseTimeout: number | null = null; |
| inactivityTimeout: number | null = null; |
| |
| |
| |
| |
| |
| |
| get progressPercent(): number { |
| const percent = Math.round(this.displayProgress * 100); |
| return Math.max(0, Math.min(100, percent)); |
| } |
| |
| |
| private resetInternalState() { |
| |
| this.displayProgress = 0; |
| this.displayMessage = ""; |
| this.completionPauseActive = false; |
| this.clearCompletionPauseTimeout(); |
| this.clearInactivityTimeout(); |
| } |
| |
| |
| private clearInactivityTimeout() { |
| if (this.inactivityTimeout) { |
| clearTimeout(this.inactivityTimeout); |
| this.inactivityTimeout = null; |
| } |
| } |
| |
| |
| private startInactivityTimeout() { |
| this.clearInactivityTimeout(); |
| this.inactivityTimeout = window.setTimeout(() => { |
| console.warn("Progress bar timed out due to inactivity."); |
| this.resetInternalState(); |
| this.inactivityTimeout = null; |
| }, INACTIVITY_TIMEOUT_MS); |
| } |
| |
| |
| private clearCompletionPauseTimeout() { |
| if (this.completionPauseTimeout) { |
| clearTimeout(this.completionPauseTimeout); |
| this.completionPauseTimeout = null; |
| } |
| } |
| |
| |
| private startCompletionPause() { |
| this.clearInactivityTimeout(); |
| this.cancelCompletionPause(); |
| |
| this.displayProgress = 1; |
| this.displayMessage = "Finishing..."; |
| |
| this.completionPauseActive = true; |
| |
| this.completionPauseTimeout = window.setTimeout(() => { |
| this.completionPauseActive = false; |
| this.completionPauseTimeout = null; |
| |
| if (!this.active) { |
| this.displayProgress = 0; |
| this.displayMessage = ""; |
| } else { |
| |
| this.evaluateState(); |
| } |
| }, COMPLETION_PAUSE_MS); |
| } |
| |
| |
| private cancelCompletionPause() { |
| this.clearCompletionPauseTimeout(); |
| this.completionPauseActive = false; |
| } |
| |
| |
| private evaluateState() { |
| |
| if (this.completionPauseActive) { |
| return; |
| } |
| |
| if (this.active) { |
| |
| const potentialNewProgress = Math.max(MIN_DISPLAY_PROGRESS, this.progress); |
| |
| if (potentialNewProgress > this.displayProgress || this.displayProgress === 0) { |
| this.displayProgress = potentialNewProgress; |
| } |
| |
| this.displayMessage = (this.progressPercent > 15) ? "Job running" : ""; |
| |
| |
| this.startInactivityTimeout(); |
| this.cancelCompletionPause(); |
| |
| } else { |
| |
| this.clearInactivityTimeout(); |
| |
| |
| if (this.displayProgress > 0) { |
| |
| this.startCompletionPause(); |
| } else { |
| |
| this.resetInternalState(); |
| } |
| } |
| } |
| |
| |
| |
| @Watch("active") |
| onActiveChanged() { |
| this.evaluateState(); |
| } |
| |
| |
| @Watch("progress") |
| onProgressChanged() { |
| this.evaluateState(); |
| } |
| |
| |
| beforeUnmount() { |
| this.clearInactivityTimeout(); |
| this.clearCompletionPauseTimeout(); |
| } |
| |
| |
| mounted() { |
| this.evaluateState(); |
| } |
| } |
| </script> |
| |
| <style scoped lang="scss"> |
| .progress-bar-container { |
| width: 100%; |
| |
| // padding: 5px 0; |
| border-top: 1px solid #dee2e6; |
| |
| z-index: 50; |
| |
| flex-shrink: 0; |
| |
| } |
| |
| .progress { |
| background-color: #e9ecef; |
| |
| overflow: hidden; |
| |
| height: 20px; |
| |
| border-radius: 0; |
| |
| } |
| |
| .progress-bar { |
| |
| background-color: #0d6efd; |
| |
| color: white; |
| text-align: center; |
| white-space: nowrap; |
| font-size: 0.85rem; |
| line-height: 20px; |
| |
| transition: width 0.3s ease-in-out; |
| |
| display: flex; |
| justify-content: center; |
| align-items: center; |
| } |
| |
| |
| .progress-bar span { |
| display: inline-block; |
| |
| } |
| </style> |