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