summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/shared/directives/stateful-tab.directive.ts
blob: cf6f27e9592d473073ef838b974f2233ba46908b (plain)
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
import { Directive, Host, HostListener, Input, OnInit, Optional } from '@angular/core';

import { NgbNav, NgbNavChangeEvent } from '@ng-bootstrap/ng-bootstrap';

@Directive({
  selector: '[cdStatefulTab]'
})
export class StatefulTabDirective implements OnInit {
  @Input()
  cdStatefulTab: string;

  private localStorage = window.localStorage;

  constructor(@Optional() @Host() private nav: NgbNav) {}

  ngOnInit() {
    // Is an activate tab identifier stored in the local storage?
    const activeId = this.localStorage.getItem(`tabset_${this.cdStatefulTab}`);
    if (activeId) {
      this.nav.select(activeId);
    }
  }

  @HostListener('navChange', ['$event'])
  onNavChange(event: NgbNavChangeEvent) {
    // Store the current active tab identifier in the local storage.
    if (this.cdStatefulTab && event.nextId) {
      this.localStorage.setItem(`tabset_${this.cdStatefulTab}`, event.nextId);
    }
  }
}