Migration to AGL gerrit (update go import)
[src/xds/xds-agent.git] / webapp / src / app / @core-xds / services / config.service.ts
1 /**
2 * @license
3 * Copyright (C) 2017-2018 "IoT.bzh"
4 * Author Sebastien Douheret <sebastien@iot.bzh>
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 *   http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 import { Injectable } from '@angular/core';
20 import { CookieService } from 'ngx-cookie';
21 import { Observable } from 'rxjs/Observable';
22 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
23
24 import { AlertService, IAlert } from '../services/alert.service';
25
26 export interface IConfig {
27     language: string;
28     projectsRootDir: string;
29 }
30
31 @Injectable()
32 export class ConfigService {
33
34     public Conf$: Observable<IConfig>;
35
36     private confSubject: BehaviorSubject<IConfig>;
37     private confStore: IConfig;
38
39     constructor(
40         private cookie: CookieService,
41         private alert: AlertService,
42     ) {
43         this.load();
44         this.confSubject = <BehaviorSubject<IConfig>>new BehaviorSubject(this.confStore);
45         this.Conf$ = this.confSubject.asObservable();
46     }
47
48     // Load config
49     load() {
50         // Try to retrieve previous config from cookie
51         const cookConf = this.cookie.getObject('xds-config');
52         if (cookConf != null) {
53             this.confStore = <IConfig>cookConf;
54         } else {
55             // Set default config
56             this.confStore = {
57                 language: 'ENG',
58                 projectsRootDir: '',
59                 // projects: []
60             };
61         }
62     }
63
64     // Save config into cookie
65     save() {
66         // Notify subscribers
67         this.confSubject.next(Object.assign({}, this.confStore));
68
69         // Don't save projects in cookies (too big!)
70         const cfg = Object.assign({}, this.confStore);
71         this.cookie.putObject('xds-config', cfg);
72     }
73
74     set projectsRootDir(p: string) {
75         this.confStore.projectsRootDir = p;
76         this.save();
77     }
78
79 }