3 * Copyright (C) 2017-2018 "IoT.bzh"
4 * Author Sebastien Douheret <sebastien@iot.bzh>
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 import { Injectable } from '@angular/core';
20 import { CookieService } from 'ngx-cookie';
21 import { NbThemeService } from '@nebular/theme';
23 import { Observable } from 'rxjs/Observable';
24 import { BehaviorSubject } from 'rxjs/BehaviorSubject';
26 export interface IConfig {
29 grafanaDashboardUrl: string;
33 export class ConfigService {
35 public Conf$: Observable<IConfig>;
37 private confSubject: BehaviorSubject<IConfig>;
38 private confStore: IConfig;
40 private confDefault: IConfig = {
43 grafanaDashboardUrl: 'http://localhost:3000',
47 private cookie: CookieService,
48 private themeService: NbThemeService,
50 this.confSubject = <BehaviorSubject<IConfig>>new BehaviorSubject(this.confStore);
51 this.Conf$ = this.confSubject.asObservable();
53 // Save selected theme in cookie
54 this.themeService.onThemeChange().subscribe(tm => {
55 if (typeof this.confStore === 'undefined') {
58 if (tm.name !== this.confStore.theme) {
59 this.confStore.theme = tm.name;
60 this.save(this.confStore);
64 // Load initial config and apply it
66 this.themeService.changeTheme(this.confStore.theme);
71 // Try to retrieve previous config from cookie
72 const cookConf = this.cookie.getObject('xds-config');
73 if (cookConf != null) {
74 this.confStore = <IConfig>cookConf;
75 this.confSubject.next(Object.assign({}, this.confStore));
82 // Save config into cookie
83 save(cfg: IConfig | null) {
84 this.confStore = (cfg !== null) ? cfg : this.confDefault;
87 this.confSubject.next(Object.assign({}, this.confStore));
89 this.cookie.putObject('xds-config', this.confStore);