Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { Injectable } from '@angular/core';
import { delay, of, Subscription } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class AnchorService {
private activeAnchors: { [id: string]: Subscription } = {};
toggleAnchor(anchorId: string) {
if (this.activeAnchors[anchorId]) {
this.removeAnchor(anchorId);
} else {
this.addAnchor(anchorId);
}
}
private addAnchor(anchorId: string): void {
this.activeAnchors[anchorId] = of(true)
.pipe(
delay(30000))
.subscribe(() => {
this.removeAnchor(anchorId);
});
AnchorService.toggleAnchorRendering(anchorId, true);
}
private removeAnchor(anchorId: string): void {
this.activeAnchors[anchorId].unsubscribe();
delete this.activeAnchors[anchorId];
AnchorService.toggleAnchorRendering(anchorId, false);
}
private static toggleAnchorRendering(anchorId: string, shouldScroll: boolean): void {
const anchors = document.querySelectorAll(`aspect-anchor[data-anchor-id="${anchorId}"]`);
anchors.forEach(anchor => anchor.classList.toggle('active-anchor'));
if (shouldScroll) anchors.item(0).scrollIntoView({ behavior: 'smooth', block: 'start' });
}
reset(): void {
Object.keys(this.activeAnchors).forEach(anchorId => this.removeAnchor(anchorId));
}
}