paperless-ngx/src-ui/src/app/components/manage/settings/settings.component.ts

188 lines
8.4 KiB
TypeScript
Raw Normal View History

import { Component, Inject, LOCALE_ID, OnInit, OnDestroy, Renderer2 } from '@angular/core';
2020-11-04 19:28:08 +01:00
import { FormControl, FormGroup } from '@angular/forms';
import { PaperlessSavedView } from 'src/app/data/paperless-saved-view';
2020-11-04 19:28:08 +01:00
import { DocumentListViewService } from 'src/app/services/document-list-view.service';
import { SavedViewService } from 'src/app/services/rest/saved-view.service';
import { LanguageOption, SettingsService, SETTINGS_KEYS } from 'src/app/services/settings.service';
2020-12-28 21:52:09 +01:00
import { ToastService } from 'src/app/services/toast.service';
2021-01-25 19:23:03 -08:00
import { dirtyCheck, DirtyComponent } from '@ngneat/dirty-check-forms';
import { Observable, Subscription, BehaviorSubject } from 'rxjs';
2020-10-27 01:10:18 +01:00
@Component({
selector: 'app-settings',
templateUrl: './settings.component.html',
styleUrls: ['./settings.component.scss']
2020-10-27 01:10:18 +01:00
})
2021-01-25 19:23:03 -08:00
export class SettingsComponent implements OnInit, OnDestroy, DirtyComponent {
2020-12-15 02:35:04 +01:00
savedViewGroup = new FormGroup({})
2020-10-27 01:10:18 +01:00
2020-11-04 19:28:08 +01:00
settingsForm = new FormGroup({
2021-01-25 19:23:03 -08:00
'bulkEditConfirmationDialogs': new FormControl(null),
'bulkEditApplyOnClose': new FormControl(null),
'documentListItemPerPage': new FormControl(null),
'darkModeUseSystem': new FormControl(null),
'darkModeEnabled': new FormControl(null),
'darkModeInvertThumbs': new FormControl(null),
'themeColor': new FormControl(null),
2021-01-25 19:23:03 -08:00
'useNativePdfViewer': new FormControl(null),
2021-01-15 12:50:34 +01:00
'savedViews': this.savedViewGroup,
2021-01-25 19:23:03 -08:00
'displayLanguage': new FormControl(null),
'dateLocale': new FormControl(null),
'dateFormat': new FormControl(null),
'notificationsConsumerNewDocument': new FormControl(null),
'notificationsConsumerSuccess': new FormControl(null),
'notificationsConsumerFailed': new FormControl(null),
'notificationsConsumerSuppressOnDashboard': new FormControl(null),
2020-11-04 19:28:08 +01:00
})
2020-12-27 23:05:19 -08:00
savedViews: PaperlessSavedView[]
2021-01-25 22:44:26 -08:00
store: BehaviorSubject<any>
storeSub: Subscription
isDirty$: Observable<boolean>
isDirty: Boolean = false
2021-01-25 19:23:03 -08:00
2021-01-17 12:14:19 +01:00
get computedDateLocale(): string {
2021-02-17 12:15:22 +01:00
return this.settingsForm.value.dateLocale || this.settingsForm.value.displayLanguage || this.currentLocale
2021-01-16 20:49:12 -08:00
}
2020-10-30 22:46:43 +01:00
constructor(
public savedViewService: SavedViewService,
2020-12-14 21:14:33 +01:00
private documentListViewService: DocumentListViewService,
2020-12-27 23:05:19 -08:00
private toastService: ToastService,
private settings: SettingsService,
@Inject(LOCALE_ID) public currentLocale: string
2020-10-30 22:46:43 +01:00
) { }
2020-12-15 02:35:04 +01:00
ngOnInit() {
this.savedViewService.listAll().subscribe(r => {
this.savedViews = r.results
2021-01-25 19:23:03 -08:00
let storeData = {
'bulkEditConfirmationDialogs': this.settings.get(SETTINGS_KEYS.BULK_EDIT_CONFIRMATION_DIALOGS),
'bulkEditApplyOnClose': this.settings.get(SETTINGS_KEYS.BULK_EDIT_APPLY_ON_CLOSE),
'documentListItemPerPage': this.settings.get(SETTINGS_KEYS.DOCUMENT_LIST_SIZE),
'darkModeUseSystem': this.settings.get(SETTINGS_KEYS.DARK_MODE_USE_SYSTEM),
'darkModeEnabled': this.settings.get(SETTINGS_KEYS.DARK_MODE_ENABLED),
'darkModeInvertThumbs': this.settings.get(SETTINGS_KEYS.DARK_MODE_THUMB_INVERTED),
'themeColor': this.settings.get(SETTINGS_KEYS.THEME_COLOR),
2021-01-25 19:23:03 -08:00
'useNativePdfViewer': this.settings.get(SETTINGS_KEYS.USE_NATIVE_PDF_VIEWER),
'savedViews': {},
'displayLanguage': this.settings.getLanguage(),
'dateLocale': this.settings.get(SETTINGS_KEYS.DATE_LOCALE),
'dateFormat': this.settings.get(SETTINGS_KEYS.DATE_FORMAT),
'notificationsConsumerNewDocument': this.settings.get(SETTINGS_KEYS.NOTIFICATIONS_CONSUMER_NEW_DOCUMENT),
'notificationsConsumerSuccess': this.settings.get(SETTINGS_KEYS.NOTIFICATIONS_CONSUMER_SUCCESS),
'notificationsConsumerFailed': this.settings.get(SETTINGS_KEYS.NOTIFICATIONS_CONSUMER_FAILED),
'notificationsConsumerSuppressOnDashboard': this.settings.get(SETTINGS_KEYS.NOTIFICATIONS_CONSUMER_SUPPRESS_ON_DASHBOARD),
2021-01-25 19:23:03 -08:00
}
2020-12-15 02:35:04 +01:00
for (let view of this.savedViews) {
2021-01-25 19:23:03 -08:00
storeData.savedViews[view.id.toString()] = {
"id": view.id,
"name": view.name,
"show_on_dashboard": view.show_on_dashboard,
"show_in_sidebar": view.show_in_sidebar
}
2020-12-15 02:35:04 +01:00
this.savedViewGroup.addControl(view.id.toString(), new FormGroup({
2021-01-25 19:23:03 -08:00
"id": new FormControl(null),
"name": new FormControl(null),
"show_on_dashboard": new FormControl(null),
"show_in_sidebar": new FormControl(null)
2020-12-15 02:35:04 +01:00
}))
}
2021-01-25 19:23:03 -08:00
this.store = new BehaviorSubject(storeData)
this.storeSub = this.store.asObservable().subscribe(state => {
this.settingsForm.patchValue(state, { emitEvent: false })
2021-01-25 22:44:26 -08:00
})
2021-01-25 19:23:03 -08:00
// Initialize dirtyCheck
2021-01-25 22:44:26 -08:00
this.isDirty$ = dirtyCheck(this.settingsForm, this.store.asObservable())
2022-03-09 15:40:04 -08:00
// Record dirty in case we need to 'undo' appearance settings if not saved on close
this.isDirty$.subscribe(dirty => {
this.isDirty = dirty
})
// "Live" visual changes prior to save
2022-03-09 15:40:04 -08:00
this.settingsForm.valueChanges.subscribe(() => {
this.settings.updateAppearanceSettings(this.settingsForm.get('darkModeUseSystem').value, this.settingsForm.get('darkModeEnabled').value, this.settingsForm.get('themeColor').value)
})
2020-12-15 02:35:04 +01:00
})
}
2021-01-25 19:23:03 -08:00
ngOnDestroy() {
if (this.isDirty) this.settings.updateAppearanceSettings() // in case user changed appearance but didnt save
2021-01-25 19:23:03 -08:00
this.storeSub && this.storeSub.unsubscribe();
}
deleteSavedView(savedView: PaperlessSavedView) {
2020-12-14 21:14:33 +01:00
this.savedViewService.delete(savedView).subscribe(() => {
2020-12-15 02:35:04 +01:00
this.savedViewGroup.removeControl(savedView.id.toString())
this.savedViews.splice(this.savedViews.indexOf(savedView), 1)
2021-01-02 14:18:20 +01:00
this.toastService.showInfo($localize`Saved view "${savedView.name}" deleted.`)
2020-12-14 21:14:33 +01:00
})
2020-10-30 22:46:43 +01:00
}
private saveLocalSettings() {
2020-12-29 17:09:07 +01:00
this.settings.set(SETTINGS_KEYS.BULK_EDIT_APPLY_ON_CLOSE, this.settingsForm.value.bulkEditApplyOnClose)
this.settings.set(SETTINGS_KEYS.BULK_EDIT_CONFIRMATION_DIALOGS, this.settingsForm.value.bulkEditConfirmationDialogs)
this.settings.set(SETTINGS_KEYS.DOCUMENT_LIST_SIZE, this.settingsForm.value.documentListItemPerPage)
this.settings.set(SETTINGS_KEYS.DARK_MODE_USE_SYSTEM, this.settingsForm.value.darkModeUseSystem)
this.settings.set(SETTINGS_KEYS.DARK_MODE_ENABLED, (this.settingsForm.value.darkModeEnabled == true).toString())
this.settings.set(SETTINGS_KEYS.DARK_MODE_THUMB_INVERTED, (this.settingsForm.value.darkModeInvertThumbs == true).toString())
this.settings.set(SETTINGS_KEYS.THEME_COLOR, (this.settingsForm.value.themeColor).toString())
this.settings.set(SETTINGS_KEYS.USE_NATIVE_PDF_VIEWER, this.settingsForm.value.useNativePdfViewer)
this.settings.set(SETTINGS_KEYS.DATE_LOCALE, this.settingsForm.value.dateLocale)
this.settings.set(SETTINGS_KEYS.DATE_FORMAT, this.settingsForm.value.dateFormat)
2021-01-27 17:28:11 +01:00
this.settings.set(SETTINGS_KEYS.NOTIFICATIONS_CONSUMER_NEW_DOCUMENT, this.settingsForm.value.notificationsConsumerNewDocument)
this.settings.set(SETTINGS_KEYS.NOTIFICATIONS_CONSUMER_SUCCESS, this.settingsForm.value.notificationsConsumerSuccess)
this.settings.set(SETTINGS_KEYS.NOTIFICATIONS_CONSUMER_FAILED, this.settingsForm.value.notificationsConsumerFailed)
this.settings.set(SETTINGS_KEYS.NOTIFICATIONS_CONSUMER_SUPPRESS_ON_DASHBOARD, this.settingsForm.value.notificationsConsumerSuppressOnDashboard)
2021-01-15 12:50:34 +01:00
this.settings.setLanguage(this.settingsForm.value.displayLanguage)
this.store.next(this.settingsForm.value)
this.documentListViewService.updatePageSize()
this.settings.updateAppearanceSettings()
2020-12-28 21:52:09 +01:00
this.toastService.showInfo($localize`Settings saved successfully.`)
}
get displayLanguageOptions(): LanguageOption[] {
return [
{code: "", name: $localize`Use system language`}
].concat(this.settings.getLanguageOptions())
}
get dateLocaleOptions(): LanguageOption[] {
2021-02-17 12:15:22 +01:00
return [
{code: "", name: $localize`Use date format of display language`}
].concat(this.settings.getDateLocaleOptions())
}
get today() {
return new Date()
2021-01-15 12:50:34 +01:00
}
2020-11-04 19:28:08 +01:00
saveSettings() {
2020-12-15 02:35:04 +01:00
let x = []
for (let id in this.savedViewGroup.value) {
x.push(this.savedViewGroup.value[id])
}
if (x.length > 0) {
this.savedViewService.patchMany(x).subscribe(s => {
this.saveLocalSettings()
}, error => {
2020-12-28 21:52:09 +01:00
this.toastService.showError($localize`Error while storing settings on server: ${JSON.stringify(error.error)}`)
})
} else {
this.saveLocalSettings()
}
2020-12-15 02:35:04 +01:00
2020-11-04 19:28:08 +01:00
}
2022-03-09 15:43:06 -08:00
clearThemeColor() {
this.settingsForm.get('themeColor').patchValue('');
}
2020-10-27 01:10:18 +01:00
}