|
| 1 | +import { Injectable } from '@angular/core'; |
| 2 | +import { BehaviorSubject } from 'rxjs'; |
| 3 | +import { environment } from '../../../environments/environment'; |
| 4 | + |
| 5 | +@Injectable({ |
| 6 | + providedIn: 'root', |
| 7 | +}) |
| 8 | +export class LiveRegionService { |
| 9 | + |
| 10 | + /** |
| 11 | + * The duration after which the messages disappear in milliseconds |
| 12 | + * @protected |
| 13 | + */ |
| 14 | + protected messageTimeOutDurationMs: number = environment.liveRegion.messageTimeOutDurationMs; |
| 15 | + |
| 16 | + /** |
| 17 | + * Array containing the messages that should be shown in the live region |
| 18 | + * @protected |
| 19 | + */ |
| 20 | + protected messages: string[] = []; |
| 21 | + |
| 22 | + /** |
| 23 | + * BehaviorSubject emitting the array with messages every time the array updates |
| 24 | + * @protected |
| 25 | + */ |
| 26 | + protected messages$: BehaviorSubject<string[]> = new BehaviorSubject([]); |
| 27 | + |
| 28 | + /** |
| 29 | + * Whether the live region should be visible |
| 30 | + * @protected |
| 31 | + */ |
| 32 | + protected liveRegionIsVisible: boolean = environment.liveRegion.isVisible; |
| 33 | + |
| 34 | + /** |
| 35 | + * Returns a copy of the array with the current live region messages |
| 36 | + */ |
| 37 | + getMessages() { |
| 38 | + return [...this.messages]; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Returns the BehaviorSubject emitting the array with messages every time the array updates |
| 43 | + */ |
| 44 | + getMessages$() { |
| 45 | + return this.messages$; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Adds a message to the live-region messages array |
| 50 | + * @param message |
| 51 | + */ |
| 52 | + addMessage(message: string) { |
| 53 | + this.messages.push(message); |
| 54 | + this.emitCurrentMessages(); |
| 55 | + |
| 56 | + // Clear the message once the timeOut has passed |
| 57 | + setTimeout(() => this.pop(), this.messageTimeOutDurationMs); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Clears the live-region messages array |
| 62 | + */ |
| 63 | + clear() { |
| 64 | + this.messages = []; |
| 65 | + this.emitCurrentMessages(); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Removes the longest living message from the array. |
| 70 | + * @protected |
| 71 | + */ |
| 72 | + protected pop() { |
| 73 | + if (this.messages.length > 0) { |
| 74 | + this.messages.shift(); |
| 75 | + this.emitCurrentMessages(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Makes the messages$ BehaviorSubject emit the current messages array |
| 81 | + * @protected |
| 82 | + */ |
| 83 | + protected emitCurrentMessages() { |
| 84 | + this.messages$.next(this.getMessages()); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Returns a boolean specifying whether the live region should be visible. |
| 89 | + * Returns 'true' if the region should be visible and false otherwise. |
| 90 | + */ |
| 91 | + getLiveRegionVisibility(): boolean { |
| 92 | + return this.liveRegionIsVisible; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Sets the visibility of the live region. |
| 97 | + * Setting this to true will make the live region visible which is useful for debugging purposes. |
| 98 | + * @param isVisible |
| 99 | + */ |
| 100 | + setLiveRegionVisibility(isVisible: boolean) { |
| 101 | + this.liveRegionIsVisible = isVisible; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Gets the current message timeOut duration in milliseconds |
| 106 | + */ |
| 107 | + getMessageTimeOutMs(): number { |
| 108 | + return this.messageTimeOutDurationMs; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Sets the message timeOut duration |
| 113 | + * @param timeOutMs the message timeOut duration in milliseconds |
| 114 | + */ |
| 115 | + setMessageTimeOutMs(timeOutMs: number) { |
| 116 | + this.messageTimeOutDurationMs = timeOutMs; |
| 117 | + } |
| 118 | +} |
0 commit comments