2017-07-15 23:29:40 +08:00
|
|
|
import {Input, OnChanges, OnDestroy, OnInit, Output, ViewChild} from "@angular/core";
|
2017-07-14 05:39:09 +08:00
|
|
|
import {AuthenticationService} from "../../model/network/authentication.service";
|
|
|
|
import {UserRoles} from "../../../../common/entities/UserDTO";
|
|
|
|
import {Utils} from "../../../../common/Utils";
|
2017-07-15 18:47:11 +08:00
|
|
|
import {ErrorDTO} from "../../../../common/entities/Error";
|
2017-07-14 05:39:09 +08:00
|
|
|
import {NotificationService} from "../../model/notification.service";
|
|
|
|
import {NavigationService} from "../../model/navigation.service";
|
2017-07-15 20:27:12 +08:00
|
|
|
import {AbstractSettingsService} from "./abstract.settings.service";
|
2017-07-15 23:29:40 +08:00
|
|
|
import {IPrivateConfig} from "../../../../common/config/private/IPrivateConfig";
|
2017-07-14 05:39:09 +08:00
|
|
|
|
|
|
|
|
2017-07-28 06:04:19 +08:00
|
|
|
export abstract class SettingsComponent<T, S extends AbstractSettingsService<T>=AbstractSettingsService<T>>
|
|
|
|
implements OnInit, OnDestroy, OnChanges {
|
2017-07-15 23:29:40 +08:00
|
|
|
|
|
|
|
@Input()
|
|
|
|
public simplifiedMode: boolean = true;
|
|
|
|
|
|
|
|
@ViewChild('settingsForm')
|
|
|
|
form: HTMLFormElement;
|
|
|
|
|
|
|
|
@Output('hasAvailableSettings')
|
|
|
|
hasAvailableSettings: boolean = true;
|
2017-07-14 05:39:09 +08:00
|
|
|
|
|
|
|
public inProgress = false;
|
|
|
|
public error: string = null;
|
|
|
|
public changed: boolean = false;
|
2017-07-28 05:10:16 +08:00
|
|
|
private _subscription = null;
|
|
|
|
private _settingsSubscription = null;
|
2017-07-14 05:39:09 +08:00
|
|
|
|
2017-07-15 23:29:40 +08:00
|
|
|
public settings: T = <any>{};
|
|
|
|
public original: T = <any>{};
|
|
|
|
|
2017-07-14 05:39:09 +08:00
|
|
|
constructor(private name,
|
|
|
|
private _authService: AuthenticationService,
|
|
|
|
private _navigation: NavigationService,
|
2017-07-28 06:04:19 +08:00
|
|
|
public _settingsService: S,
|
2017-07-15 23:29:40 +08:00
|
|
|
protected notification: NotificationService,
|
2017-07-26 03:09:37 +08:00
|
|
|
private sliceFN?: (s: IPrivateConfig) => T) {
|
|
|
|
if (this.sliceFN) {
|
2017-07-28 05:10:16 +08:00
|
|
|
this._settingsSubscription = this._settingsService.Settings.subscribe(this.onNewSettings);
|
2017-07-26 03:09:37 +08:00
|
|
|
this.onNewSettings(this._settingsService._settingsService.settings.value);
|
|
|
|
}
|
2017-07-14 05:39:09 +08:00
|
|
|
}
|
|
|
|
|
2017-07-15 23:29:40 +08:00
|
|
|
onNewSettings = (s) => {
|
|
|
|
this.settings = Utils.clone(this.sliceFN(s));
|
|
|
|
this.original = Utils.clone(this.settings);
|
|
|
|
this.ngOnChanges();
|
|
|
|
};
|
|
|
|
|
2017-07-14 05:39:09 +08:00
|
|
|
ngOnInit() {
|
|
|
|
if (!this._authService.isAuthenticated() ||
|
|
|
|
this._authService.user.value.role < UserRoles.Admin) {
|
|
|
|
this._navigation.toLogin();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.getSettings();
|
|
|
|
|
2017-07-28 05:10:16 +08:00
|
|
|
this._subscription = this.form.valueChanges.subscribe((data) => {
|
2017-07-15 23:29:40 +08:00
|
|
|
this.changed = !Utils.equalsFilter(this.settings, this.original);
|
2017-07-14 05:39:09 +08:00
|
|
|
});
|
2017-07-15 23:29:40 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
ngOnChanges(): void {
|
|
|
|
this.hasAvailableSettings = (this._settingsService.isSupported() || !this.simplifiedMode);
|
2017-07-14 05:39:09 +08:00
|
|
|
}
|
|
|
|
|
2017-07-15 23:29:40 +08:00
|
|
|
|
2017-07-14 05:39:09 +08:00
|
|
|
ngOnDestroy() {
|
2017-07-28 05:10:16 +08:00
|
|
|
if (this._subscription != null) {
|
|
|
|
this._subscription.unsubscribe();
|
2017-07-14 05:39:09 +08:00
|
|
|
}
|
2017-07-28 05:10:16 +08:00
|
|
|
if (this._settingsSubscription != null) {
|
|
|
|
this._settingsSubscription.unsubscribe();
|
2017-07-20 04:40:27 +08:00
|
|
|
}
|
2017-07-14 05:39:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private async getSettings() {
|
2017-07-15 20:27:12 +08:00
|
|
|
await this._settingsService.getSettings();
|
2017-07-14 05:39:09 +08:00
|
|
|
this.changed = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public reset() {
|
|
|
|
this.getSettings();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-07-15 18:47:11 +08:00
|
|
|
public async save() {
|
2017-07-14 05:39:09 +08:00
|
|
|
this.inProgress = true;
|
2017-07-15 18:47:11 +08:00
|
|
|
this.error = "";
|
2017-07-14 05:39:09 +08:00
|
|
|
try {
|
2017-07-15 23:29:40 +08:00
|
|
|
await this._settingsService.updateSettings(this.settings);
|
2017-07-15 18:47:11 +08:00
|
|
|
await this.getSettings();
|
|
|
|
this.notification.success(this.name + ' settings saved', "Success");
|
2017-07-15 22:31:43 +08:00
|
|
|
this.inProgress = false;
|
|
|
|
return true;
|
2017-07-14 05:39:09 +08:00
|
|
|
} catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
if (err.message) {
|
2017-07-15 18:47:11 +08:00
|
|
|
this.error = (<ErrorDTO>err).message;
|
2017-07-14 05:39:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-15 22:31:43 +08:00
|
|
|
this.inProgress = false;
|
|
|
|
return false;
|
2017-07-14 05:39:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|