diff --git a/client/src/app/core/services/config.service.spec.ts b/client/src/app/core/services/config.service.spec.ts new file mode 100644 index 000000000..90426ee20 --- /dev/null +++ b/client/src/app/core/services/config.service.spec.ts @@ -0,0 +1,15 @@ +import { TestBed, inject } from '@angular/core/testing'; + +import { ConfigService } from './config.service'; + +describe('ConfigService', () => { + beforeEach(() => { + TestBed.configureTestingModule({ + providers: [ConfigService] + }); + }); + + it('should be created', inject([ConfigService], (service: ConfigService) => { + expect(service).toBeTruthy(); + })); +}); diff --git a/client/src/app/core/services/config.service.ts b/client/src/app/core/services/config.service.ts new file mode 100644 index 000000000..eba1fc629 --- /dev/null +++ b/client/src/app/core/services/config.service.ts @@ -0,0 +1,75 @@ +import { Injectable } from '@angular/core'; + +import { OpenSlidesComponent } from 'app/openslides.component'; +import { Observable, BehaviorSubject } from 'rxjs'; +import { Config } from '../../shared/models/core/config'; + +/** + * Handler for config variables. + * + * @example + * ```ts + * this.configService.get('general_event_name').subscribe(value => { + * console.log(value); + * }); + * ``` + * + * @example + * ```ts + * const value = this.configService.instant('general_event_name'); + * ``` + */ +@Injectable({ + providedIn: 'root' +}) +export class ConfigService extends OpenSlidesComponent { + /** + * Stores a subject per key. Values are published, if the DataStore gets an update. + */ + private configSubjects: { [key: string]: BehaviorSubject } = {}; + + /** + * Listen for changes of config variables. + */ + public constructor() { + super(); + + this.DS.getObservable().subscribe(data => { + // on changes notify the observers for specific keys. + if (data instanceof Config && this.configSubjects[data.key]) { + this.configSubjects[data.key].next(data.value); + } + }); + } + + /** + * Get the constant named by key from the DataStore. If the DataStore isn't up to date or + * not filled via autoupdates the results may be wrong/empty. Use this with caution. + * + * Usefull for synchronos code, e.g. during generation of PDFs, when the DataStore is filled. + * + * @param key The config value to get from. + */ + public instant(key: string): any { + const values = this.DS.filter('core/config', value => value.key === key); + if (values.length > 1) { + throw new Error('More keys found then expected'); + } else if (values.length === 1) { + return values[0].value; + } else { + return; + } + } + + /** + * Get an observer for the config value given by the key. + * + * @param key The config value to get from. + */ + public get(key: string): Observable { + if (!this.configSubjects[key]) { + this.configSubjects[key] = new BehaviorSubject(this.instant(key)); + } + return this.configSubjects[key].asObservable(); + } +} diff --git a/client/src/app/core/services/data-store.service.ts b/client/src/app/core/services/data-store.service.ts index 767ba7bb4..764447dad 100644 --- a/client/src/app/core/services/data-store.service.ts +++ b/client/src/app/core/services/data-store.service.ts @@ -227,7 +227,7 @@ export class DataStoreService { public filter( collectionType: ModelConstructor | string, callback: (model: T) => boolean - ): BaseModel[] { + ): T[] { return this.getAll(collectionType).filter(callback); } diff --git a/client/src/app/site/legal-notice/legal-notice.component.html b/client/src/app/site/legal-notice/legal-notice.component.html index f3ee69cc0..8978d1629 100644 --- a/client/src/app/site/legal-notice/legal-notice.component.html +++ b/client/src/app/site/legal-notice/legal-notice.component.html @@ -4,10 +4,7 @@
- +
OpenSlides 3.0 PRE ALPHA (Lizenz: MIT) diff --git a/client/src/app/site/legal-notice/legal-notice.component.ts b/client/src/app/site/legal-notice/legal-notice.component.ts index b866f1f45..e811c81a8 100644 --- a/client/src/app/site/legal-notice/legal-notice.component.ts +++ b/client/src/app/site/legal-notice/legal-notice.component.ts @@ -1,4 +1,6 @@ import { Component, OnInit } from '@angular/core'; +import { ConfigService } from '../../core/services/config.service'; +import { TranslateService } from '@ngx-translate/core'; @Component({ selector: 'os-legal-notice', @@ -6,7 +8,15 @@ import { Component, OnInit } from '@angular/core'; styleUrls: ['./legal-notice.component.scss'] }) export class LegalNoticeComponent implements OnInit { - public constructor() {} + public legalNotice: string; - public ngOnInit() {} + public constructor(private configService: ConfigService, private translate: TranslateService) {} + + public ngOnInit() { + this.configService.get('general_event_legal_notice').subscribe(value => { + if (value) { + this.legalNotice = this.translate.instant(value); + } + }); + } }