New dashboard look & feel
[src/xds/xds-agent.git] / webapp / src / app / @core-xds / services / config.service.ts
1 import { Injectable } from '@angular/core';
2 import { CookieService } from 'ngx-cookie';
3 import { Observable } from 'rxjs/Observable';
4 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
5
6 import { AlertService, IAlert } from '../services/alert.service';
7
8 export interface IConfig {
9     language: string;
10     projectsRootDir: string;
11 }
12
13 @Injectable()
14 export class ConfigService {
15
16     public Conf$: Observable<IConfig>;
17
18     private confSubject: BehaviorSubject<IConfig>;
19     private confStore: IConfig;
20
21     constructor(
22         private cookie: CookieService,
23         private alert: AlertService,
24     ) {
25         this.load();
26         this.confSubject = <BehaviorSubject<IConfig>>new BehaviorSubject(this.confStore);
27         this.Conf$ = this.confSubject.asObservable();
28     }
29
30     // Load config
31     load() {
32         // Try to retrieve previous config from cookie
33         const cookConf = this.cookie.getObject('xds-config');
34         if (cookConf != null) {
35             this.confStore = <IConfig>cookConf;
36         } else {
37             // Set default config
38             this.confStore = {
39                 language: 'ENG',
40                 projectsRootDir: '',
41                 // projects: []
42             };
43         }
44     }
45
46     // Save config into cookie
47     save() {
48         // Notify subscribers
49         this.confSubject.next(Object.assign({}, this.confStore));
50
51         // Don't save projects in cookies (too big!)
52         const cfg = Object.assign({}, this.confStore);
53         this.cookie.putObject('xds-config', cfg);
54     }
55
56     set projectsRootDir(p: string) {
57         this.confStore.projectsRootDir = p;
58         this.save();
59     }
60
61 }