]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
1 | import { Directive, Host, HostListener, Input, OnInit, Optional } from '@angular/core'; |
2 | ||
3 | import { NgbNav, NgbNavChangeEvent } from '@ng-bootstrap/ng-bootstrap'; | |
4 | ||
5 | @Directive({ | |
6 | selector: '[cdStatefulTab]' | |
7 | }) | |
8 | export class StatefulTabDirective implements OnInit { | |
9 | @Input() | |
10 | cdStatefulTab: string; | |
aee94f69 TL |
11 | @Input() |
12 | cdStatefulTabDefault = ''; | |
f67539c2 TL |
13 | |
14 | private localStorage = window.localStorage; | |
15 | ||
16 | constructor(@Optional() @Host() private nav: NgbNav) {} | |
17 | ||
18 | ngOnInit() { | |
19 | // Is an activate tab identifier stored in the local storage? | |
aee94f69 TL |
20 | const activeId = |
21 | this.cdStatefulTabDefault || this.localStorage.getItem(`tabset_${this.cdStatefulTab}`); | |
f67539c2 TL |
22 | if (activeId) { |
23 | this.nav.select(activeId); | |
24 | } | |
25 | } | |
26 | ||
27 | @HostListener('navChange', ['$event']) | |
28 | onNavChange(event: NgbNavChangeEvent) { | |
29 | // Store the current active tab identifier in the local storage. | |
30 | if (this.cdStatefulTab && event.nextId) { | |
31 | this.localStorage.setItem(`tabset_${this.cdStatefulTab}`, event.nextId); | |
32 | } | |
33 | } | |
34 | } |