1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/frontend/app/settings/_abstract/abstract.settings.component.ts

156 lines
4.4 KiB
TypeScript
Raw Normal View History

2018-03-31 03:30:30 +08:00
import {Input, OnChanges, OnDestroy, OnInit, Output, ViewChild} from '@angular/core';
import {AuthenticationService} from '../../model/network/authentication.service';
import {UserRoles} from '../../../../common/entities/UserDTO';
import {Utils} from '../../../../common/Utils';
import {ErrorDTO} from '../../../../common/entities/Error';
import {NotificationService} from '../../model/notification.service';
import {NavigationService} from '../../model/navigation.service';
import {AbstractSettingsService} from './abstract.settings.service';
import {IPrivateConfig} from '../../../../common/config/private/IPrivateConfig';
import {I18n} from '@ngx-translate/i18n-polyfill';
2017-07-14 05:39:09 +08:00
2018-10-23 06:33:17 +08:00
export abstract class SettingsComponent<T, S extends AbstractSettingsService<T> = AbstractSettingsService<T>>
2017-07-28 06:04:19 +08:00
implements OnInit, OnDestroy, OnChanges {
2017-07-15 23:29:40 +08:00
@Input()
2018-05-04 06:23:48 +08:00
public simplifiedMode = true;
2017-07-15 23:29:40 +08:00
@ViewChild('settingsForm')
form: HTMLFormElement;
@Output()
2018-05-04 06:23:48 +08:00
hasAvailableSettings = true;
2017-07-14 05:39:09 +08:00
public inProgress = false;
public error: string = null;
2018-05-04 06:23:48 +08:00
public changed = false;
2017-07-28 05:10:16 +08:00
private _subscription = null;
2018-05-04 07:17:08 +08:00
private readonly _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>{};
2018-03-30 08:30:23 +08:00
text = {
2018-03-31 03:30:30 +08:00
Enabled: 'Enabled',
Disabled: 'Disabled',
Low: 'Low',
High: 'High'
2018-03-30 08:30:23 +08:00
};
protected constructor(private name,
private _authService: AuthenticationService,
private _navigation: NavigationService,
public _settingsService: S,
protected notification: NotificationService,
public i18n: I18n,
private sliceFN?: (s: IPrivateConfig) => T) {
2017-07-26 03:09:37 +08:00
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);
}
2018-03-31 03:30:30 +08:00
this.text.Enabled = i18n('Enabled');
this.text.Disabled = i18n('Disabled');
this.text.Low = i18n('Low');
this.text.High = i18n('High');
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();
};
settingsSame(newSettings: T, original: T): boolean {
if (typeof original !== 'object' || original == null) {
return newSettings === original;
}
if (!newSettings) {
return false;
}
const keys = Object.keys(newSettings);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
if (typeof original[key] === 'undefined') {
throw new Error('unknown settings: ' + key);
}
if (typeof original[key] === 'object') {
if (this.settingsSame(newSettings[key], original[key]) === false) {
return false;
}
} else if (newSettings[key] !== original[key]) {
return false;
}
}
return true;
}
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();
// TODO: fix after this issue is fixed: https://github.com/angular/angular/issues/24818
this._subscription = this.form.valueChanges.subscribe(() => {
setTimeout(() => {
this.changed = !this.settingsSame(this.settings, this.original);
}, 0);
2017-07-14 05:39:09 +08:00
});
2017-07-15 23:29:40 +08:00
}
ngOnChanges(): void {
2018-10-23 06:33:17 +08:00
this.hasAvailableSettings = ((this._settingsService.isSupported() &&
this._settingsService.showInSimplifiedMode())
|| !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;
2018-03-31 03:30:30 +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();
2018-11-19 03:26:29 +08:00
this.notification.success(this.name + ' ' + this.i18n('settings saved'), this.i18n('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
}
}