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

95 lines
3.7 KiB
TypeScript
Raw Normal View History

2020-12-27 23:05:19 -08:00
import { Component, OnInit, 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 { GENERAL_SETTINGS } from 'src/app/data/storage-keys';
import { DocumentListViewService } from 'src/app/services/document-list-view.service';
import { SavedViewService } from 'src/app/services/rest/saved-view.service';
2020-12-28 21:52:09 +01:00
import { ToastService } from 'src/app/services/toast.service';
2020-12-27 23:05:19 -08:00
import { AppViewService } from 'src/app/services/app-view.service';
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
})
2020-12-15 02:35:04 +01:00
export class SettingsComponent implements OnInit {
savedViewGroup = new FormGroup({})
2020-10-27 01:10:18 +01:00
2020-11-04 19:28:08 +01:00
settingsForm = new FormGroup({
2020-12-15 02:35:04 +01:00
'documentListItemPerPage': new FormControl(+localStorage.getItem(GENERAL_SETTINGS.DOCUMENT_LIST_SIZE) || GENERAL_SETTINGS.DOCUMENT_LIST_SIZE_DEFAULT),
2020-12-27 23:05:19 -08:00
'darkModeUseSystem': new FormControl(
localStorage.getItem(GENERAL_SETTINGS.DARK_MODE_USE_SYSTEM) == undefined ? GENERAL_SETTINGS.DARK_MODE_USE_SYSTEM_DEFAULT : JSON.parse(localStorage.getItem(GENERAL_SETTINGS.DARK_MODE_USE_SYSTEM))
),
'darkModeEnabled': new FormControl(
localStorage.getItem(GENERAL_SETTINGS.DARK_MODE_ENABLED) == undefined ? GENERAL_SETTINGS.DARK_MODE_ENABLED_DEFAULT : JSON.parse(localStorage.getItem(GENERAL_SETTINGS.DARK_MODE_ENABLED))
),
2020-12-15 02:35:04 +01:00
'savedViews': this.savedViewGroup
2020-11-04 19:28:08 +01:00
})
2020-12-27 23:05:19 -08:00
savedViews: PaperlessSavedView[]
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 appViewService: AppViewService
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
for (let view of this.savedViews) {
this.savedViewGroup.addControl(view.id.toString(), new FormGroup({
"id": new FormControl(view.id),
"name": new FormControl(view.name),
"show_on_dashboard": new FormControl(view.show_on_dashboard),
"show_in_sidebar": new FormControl(view.show_in_sidebar)
}))
}
})
}
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)
2020-12-28 21:52:09 +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
}
2020-12-27 23:05:19 -08:00
toggleDarkModeSetting() {
if (this.settingsForm.value.darkModeUseSystem) {
(this.settingsForm.controls.darkModeEnabled as FormControl).disable()
} else {
(this.settingsForm.controls.darkModeEnabled as FormControl).enable()
}
}
private saveLocalSettings() {
localStorage.setItem(GENERAL_SETTINGS.DOCUMENT_LIST_SIZE, this.settingsForm.value.documentListItemPerPage)
2020-12-27 23:05:19 -08:00
localStorage.setItem(GENERAL_SETTINGS.DARK_MODE_USE_SYSTEM, this.settingsForm.value.darkModeUseSystem)
localStorage.setItem(GENERAL_SETTINGS.DARK_MODE_ENABLED, (this.settingsForm.value.darkModeEnabled == true).toString())
this.documentListViewService.updatePageSize()
2020-12-27 23:05:19 -08:00
this.appViewService.updateDarkModeSettings()
2020-12-28 21:52:09 +01:00
this.toastService.showInfo($localize`Settings saved successfully.`)
}
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
}
2020-10-27 01:10:18 +01:00
}