|
| 1 | +import { Component, EventEmitter, Input, OnDestroy, OnInit, Output } from '@angular/core'; |
| 2 | +import { Subject, timer } from 'rxjs'; |
| 3 | +import { takeUntil } from 'rxjs/operators'; |
| 4 | +export type StartDate = Date | string | number; |
| 5 | +@Component({ |
| 6 | + selector: 'ng-circle-timer', |
| 7 | + template: ` |
| 8 | + <div class="base-timer"> |
| 9 | + <svg class="base-timer-svg" |
| 10 | + viewBox="0 0 100 100" |
| 11 | + xmlns="http://www.w3.org/2000/svg"> |
| 12 | + <g class="base-timer-circle"> |
| 13 | + <circle [class.base-timer-completed]=completed [class.base-timer-stroke]=!completed |
| 14 | + cx="50" |
| 15 | + cy="50" |
| 16 | + r="45" /> |
| 17 | + <path [attr.stroke-dasharray]="dasharray" |
| 18 | + [style.stroke]="color" |
| 19 | + id="remaining-time-stroke" |
| 20 | + d=" M 50, 50 |
| 21 | + m -45, 0 |
| 22 | + a 45,45 0 1,0 90,0 |
| 23 | + a 45,45 0 1,0 -90,0"> |
| 24 | + </path> |
| 25 | + </g> |
| 26 | + </svg> |
| 27 | + <span class="time"> |
| 28 | + <span>{{formattedTimeLeft}}</span> |
| 29 | + </span> |
| 30 | + </div> |
| 31 | + `, |
| 32 | + styles: [ |
| 33 | + ] |
| 34 | +}) |
| 35 | +export class NgCircleTimerComponent implements OnInit, OnDestroy { |
| 36 | + @Input() startDate?: string; // only to set initial state (ngOnInit) |
| 37 | + @Input() duration = 0; // milliseconds |
| 38 | + @Input() color = '#1cbbf8'; |
| 39 | + |
| 40 | + @Output() onComplete = new EventEmitter<boolean>(); |
| 41 | + |
| 42 | + destroy$ = new Subject<void>(); |
| 43 | + |
| 44 | + timeLeft = 0; |
| 45 | + ticking = false; |
| 46 | + completed = false; |
| 47 | + formattedTimeLeft = ''; |
| 48 | + |
| 49 | + readonly fullDasharray = 283; |
| 50 | + dasharray = `${this.fullDasharray} ${this.fullDasharray}`; |
| 51 | + |
| 52 | + constructor() {} |
| 53 | + |
| 54 | + ngOnInit() { |
| 55 | + this.init(this.startDate); |
| 56 | + } |
| 57 | + |
| 58 | + ngOnDestroy() { |
| 59 | + this.destroy$.next(); |
| 60 | + this.destroy$.complete(); |
| 61 | + } |
| 62 | + |
| 63 | + init(startDate?: StartDate): void { |
| 64 | + this.setTimeLeft(startDate); |
| 65 | + this.formatTimeLeft(); |
| 66 | + this.setDasharray(); |
| 67 | + |
| 68 | + if (this.timeLeft === 0) { |
| 69 | + this.completed = true; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + setTimeLeft(startDate?: StartDate): number { |
| 74 | + this.ticking = false; |
| 75 | + this.completed = false; |
| 76 | + this.destroy$.next(); |
| 77 | + |
| 78 | + if (!startDate) { |
| 79 | + this.timeLeft = this.duration; |
| 80 | + return 0; |
| 81 | + } |
| 82 | + |
| 83 | + const startTime = new Date(<string>startDate).getTime(); |
| 84 | + const endTime = startTime + this.duration; |
| 85 | + const timeLeftRaw = endTime - Date.now(); |
| 86 | + if (timeLeftRaw <= 0) { |
| 87 | + this.timeLeft = 0; |
| 88 | + return 0; |
| 89 | + } |
| 90 | + |
| 91 | + const timeLeftSeconds = timeLeftRaw / 1000; |
| 92 | + |
| 93 | + this.timeLeft = Math.floor(timeLeftSeconds) * 1000; |
| 94 | + return timeLeftSeconds % 1; |
| 95 | + } |
| 96 | + |
| 97 | + start(startDate?: StartDate, delayMs = 0, replaying = false): void { |
| 98 | + if (this.ticking) { |
| 99 | + console.log('Cannot start: timer already running.'); |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + const decimalPortion = this.setTimeLeft(startDate); |
| 104 | + const startDelayMs = delayMs + decimalPortion * 1000; |
| 105 | + this.formatTimeLeft(); |
| 106 | + this.setDasharray(); |
| 107 | + |
| 108 | + if (this.timeLeft === 0) { |
| 109 | + this.completed = true; |
| 110 | + console.log('Cannot start: timer already completed.'); |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + timer(startDelayMs, 1000) |
| 115 | + .pipe(takeUntil(this.destroy$)) |
| 116 | + .subscribe(() => { |
| 117 | + this.ticking = true; |
| 118 | + this.timeLeft -= 1000; |
| 119 | + if (this.timeLeft <= 0) { |
| 120 | + this.timeLeft = 0; |
| 121 | + this.ticking = false; |
| 122 | + this.completed = true; |
| 123 | + this.onComplete.emit(replaying); |
| 124 | + this.destroy$.next(); |
| 125 | + } |
| 126 | + |
| 127 | + this.formatTimeLeft(); |
| 128 | + this.setDasharray(); |
| 129 | + }); |
| 130 | + } |
| 131 | + |
| 132 | + replay(startDate?: StartDate): void { |
| 133 | + this.start(startDate, 1200, true); |
| 134 | + } |
| 135 | + |
| 136 | + pause(): void {} |
| 137 | + |
| 138 | + continue(): void {} |
| 139 | + |
| 140 | + complete(): void { |
| 141 | + this.timeLeft = 0; |
| 142 | + this.ticking = false; |
| 143 | + this.completed = true; |
| 144 | + this.destroy$.next(); |
| 145 | + |
| 146 | + this.formatTimeLeft(); |
| 147 | + this.setDasharray(); |
| 148 | + } |
| 149 | + |
| 150 | + isTicking(): boolean { |
| 151 | + return this.ticking; |
| 152 | + } |
| 153 | + |
| 154 | + isCompleted(): boolean { |
| 155 | + return this.completed; |
| 156 | + } |
| 157 | + |
| 158 | + formatTimeLeft(): void { |
| 159 | + if (this.timeLeft <= 0) { |
| 160 | + this.timeLeft = 0; |
| 161 | + } |
| 162 | + |
| 163 | + const daysLeft = Math.floor(this.timeLeft / (1000 * 60 * 60 * 24)); |
| 164 | + const hoursLeft = Math.floor((this.timeLeft % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); |
| 165 | + const minutesLeft = Math.floor((this.timeLeft % (1000 * 60 * 60)) / (1000 * 60)); |
| 166 | + const secondsLeft = Math.floor((this.timeLeft % (1000 * 60)) / 1000); |
| 167 | + |
| 168 | + const formattedDays = daysLeft < 10 ? `0${daysLeft}` : `${daysLeft}`; |
| 169 | + const formattedHours = hoursLeft < 10 ? `0${hoursLeft}` : `${hoursLeft}`; |
| 170 | + const formattedMinutes = minutesLeft < 10 ? `0${minutesLeft}` : `${minutesLeft}`; |
| 171 | + const formattedSeconds = secondsLeft < 10 ? `0${secondsLeft}` : `${secondsLeft}`; |
| 172 | + |
| 173 | + this.formattedTimeLeft = `${formattedMinutes}:${formattedSeconds}`; |
| 174 | + |
| 175 | + if (formattedHours !== '00' || formattedDays !== '00') { |
| 176 | + this.formattedTimeLeft = `${formattedHours}:` + this.formattedTimeLeft; |
| 177 | + } |
| 178 | + |
| 179 | + if (formattedDays !== '00') { |
| 180 | + this.formattedTimeLeft = `${formattedDays}:` + this.formattedTimeLeft; |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + setDasharray(): void { |
| 185 | + const rawFraction = this.timeLeft / this.duration; |
| 186 | + const fraction = rawFraction - (1 / this.duration) * (1 - rawFraction); |
| 187 | + const remaining = Math.round(fraction * this.fullDasharray); |
| 188 | + |
| 189 | + this.dasharray = `${remaining} ${this.fullDasharray}`; |
| 190 | + } |
| 191 | +} |
0 commit comments