2022-09-30 14:03:59 -07:00
|
|
|
import { Component, HostListener, OnInit } from '@angular/core'
|
2022-03-11 10:53:32 -08:00
|
|
|
import { FormControl } from '@angular/forms'
|
2022-08-06 19:30:39 -07:00
|
|
|
import { ActivatedRoute, Router } from '@angular/router'
|
2022-05-23 00:32:39 -07:00
|
|
|
import { from, Observable } from 'rxjs'
|
2022-03-11 10:53:32 -08:00
|
|
|
import {
|
|
|
|
|
debounceTime,
|
|
|
|
|
distinctUntilChanged,
|
|
|
|
|
map,
|
|
|
|
|
switchMap,
|
|
|
|
|
first,
|
|
|
|
|
} from 'rxjs/operators'
|
|
|
|
|
import { PaperlessDocument } from 'src/app/data/paperless-document'
|
|
|
|
|
import { OpenDocumentsService } from 'src/app/services/open-documents.service'
|
|
|
|
|
import { SavedViewService } from 'src/app/services/rest/saved-view.service'
|
|
|
|
|
import { SearchService } from 'src/app/services/rest/search.service'
|
|
|
|
|
import { environment } from 'src/environments/environment'
|
|
|
|
|
import { DocumentDetailComponent } from '../document-detail/document-detail.component'
|
2022-05-26 21:21:29 -07:00
|
|
|
import { DocumentListViewService } from 'src/app/services/document-list-view.service'
|
2022-03-11 10:53:32 -08:00
|
|
|
import { FILTER_FULLTEXT_QUERY } from 'src/app/data/filter-rule-type'
|
2022-04-01 01:53:59 -07:00
|
|
|
import {
|
|
|
|
|
RemoteVersionService,
|
|
|
|
|
AppRemoteVersion,
|
|
|
|
|
} from 'src/app/services/rest/remote-version.service'
|
2022-05-09 11:08:28 -07:00
|
|
|
import { SettingsService } from 'src/app/services/settings.service'
|
2022-05-26 23:09:24 -07:00
|
|
|
import { TasksService } from 'src/app/services/tasks.service'
|
2022-08-06 19:30:39 -07:00
|
|
|
import { ComponentCanDeactivate } from 'src/app/guards/dirty-doc.guard'
|
2022-09-12 12:54:28 -07:00
|
|
|
import { SETTINGS_KEYS } from 'src/app/data/paperless-uisettings'
|
|
|
|
|
import { ToastService } from 'src/app/services/toast.service'
|
2022-11-12 04:03:35 -08:00
|
|
|
import { ComponentWithPermissions } from '../with-permissions/with-permissions.component'
|
2020-12-31 16:23:08 -08:00
|
|
|
|
2020-10-27 01:10:18 +01:00
|
|
|
@Component({
|
|
|
|
|
selector: 'app-app-frame',
|
|
|
|
|
templateUrl: './app-frame.component.html',
|
2022-03-11 10:53:32 -08:00
|
|
|
styleUrls: ['./app-frame.component.scss'],
|
2020-10-27 01:10:18 +01:00
|
|
|
})
|
2022-11-12 04:03:35 -08:00
|
|
|
export class AppFrameComponent
|
|
|
|
|
extends ComponentWithPermissions
|
|
|
|
|
implements OnInit, ComponentCanDeactivate
|
|
|
|
|
{
|
2022-03-11 10:53:32 -08:00
|
|
|
constructor(
|
2020-10-30 22:46:43 +01:00
|
|
|
public router: Router,
|
2020-11-12 11:11:57 +01:00
|
|
|
private activatedRoute: ActivatedRoute,
|
2020-10-30 22:46:43 +01:00
|
|
|
private openDocumentsService: OpenDocumentsService,
|
|
|
|
|
private searchService: SearchService,
|
2020-12-31 16:23:08 -08:00
|
|
|
public savedViewService: SavedViewService,
|
2022-05-05 00:23:06 -07:00
|
|
|
private remoteVersionService: RemoteVersionService,
|
2022-05-20 15:16:17 -07:00
|
|
|
private list: DocumentListViewService,
|
2022-05-26 23:09:24 -07:00
|
|
|
public settingsService: SettingsService,
|
2022-09-12 12:54:28 -07:00
|
|
|
public tasksService: TasksService,
|
2022-10-09 23:58:41 -07:00
|
|
|
private readonly toastService: ToastService
|
2022-11-12 04:03:35 -08:00
|
|
|
) {
|
|
|
|
|
super()
|
|
|
|
|
}
|
2022-09-30 14:03:59 -07:00
|
|
|
|
|
|
|
|
ngOnInit(): void {
|
|
|
|
|
if (this.settingsService.get(SETTINGS_KEYS.UPDATE_CHECKING_ENABLED)) {
|
2022-09-30 12:30:23 -07:00
|
|
|
this.checkForUpdates()
|
|
|
|
|
}
|
2022-09-30 14:03:59 -07:00
|
|
|
this.tasksService.reload()
|
2022-04-01 01:53:59 -07:00
|
|
|
}
|
2022-10-09 22:49:08 -07:00
|
|
|
|
2020-12-16 14:41:57 +01:00
|
|
|
versionString = `${environment.appTitle} ${environment.version}`
|
2022-04-01 01:53:59 -07:00
|
|
|
appRemoteVersion
|
2020-12-16 14:41:57 +01:00
|
|
|
|
2020-11-22 11:35:04 +01:00
|
|
|
isMenuCollapsed: boolean = true
|
|
|
|
|
|
2022-09-27 11:00:02 -07:00
|
|
|
slimSidebarAnimating: boolean = false
|
|
|
|
|
|
|
|
|
|
toggleSlimSidebar(): void {
|
|
|
|
|
this.slimSidebarAnimating = true
|
|
|
|
|
this.slimSidebarEnabled = !this.slimSidebarEnabled
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
this.slimSidebarAnimating = false
|
|
|
|
|
}, 200) // slightly longer than css animation for slim sidebar
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-12 12:54:28 -07:00
|
|
|
get slimSidebarEnabled(): boolean {
|
|
|
|
|
return this.settingsService.get(SETTINGS_KEYS.SLIM_SIDEBAR)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set slimSidebarEnabled(enabled: boolean) {
|
|
|
|
|
this.settingsService.set(SETTINGS_KEYS.SLIM_SIDEBAR, enabled)
|
|
|
|
|
this.settingsService
|
|
|
|
|
.storeSettings()
|
|
|
|
|
.pipe(first())
|
|
|
|
|
.subscribe({
|
|
|
|
|
error: (error) => {
|
|
|
|
|
this.toastService.showError(
|
|
|
|
|
$localize`An error occurred while saving settings.`
|
|
|
|
|
)
|
|
|
|
|
console.log(error)
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-22 17:48:54 +01:00
|
|
|
closeMenu() {
|
|
|
|
|
this.isMenuCollapsed = true
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-27 01:10:18 +01:00
|
|
|
searchField = new FormControl('')
|
|
|
|
|
|
2022-10-29 09:44:26 -07:00
|
|
|
get searchFieldEmpty(): boolean {
|
|
|
|
|
return this.searchField.value.trim().length == 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resetSearchField() {
|
|
|
|
|
this.searchField.reset('')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
searchFieldKeyup(event: KeyboardEvent) {
|
|
|
|
|
if (event.key == 'Escape') {
|
|
|
|
|
this.resetSearchField()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-25 07:29:51 -08:00
|
|
|
get openDocuments(): PaperlessDocument[] {
|
|
|
|
|
return this.openDocumentsService.getOpenDocuments()
|
|
|
|
|
}
|
2020-10-27 01:10:18 +01:00
|
|
|
|
2022-08-06 19:30:39 -07:00
|
|
|
@HostListener('window:beforeunload')
|
|
|
|
|
canDeactivate(): Observable<boolean> | boolean {
|
|
|
|
|
return !this.openDocumentsService.hasDirty()
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-27 17:05:14 +01:00
|
|
|
searchAutoComplete = (text$: Observable<string>) =>
|
|
|
|
|
text$.pipe(
|
|
|
|
|
debounceTime(200),
|
|
|
|
|
distinctUntilChanged(),
|
2022-03-11 10:53:32 -08:00
|
|
|
map((term) => {
|
2020-10-27 17:05:14 +01:00
|
|
|
if (term.lastIndexOf(' ') != -1) {
|
|
|
|
|
return term.substring(term.lastIndexOf(' ') + 1)
|
|
|
|
|
} else {
|
|
|
|
|
return term
|
|
|
|
|
}
|
|
|
|
|
}),
|
2022-03-11 10:53:32 -08:00
|
|
|
switchMap((term) =>
|
2020-10-27 17:05:14 +01:00
|
|
|
term.length < 2 ? from([[]]) : this.searchService.autocomplete(term)
|
|
|
|
|
)
|
|
|
|
|
)
|
2020-12-31 16:23:08 -08:00
|
|
|
|
2020-10-27 17:05:14 +01:00
|
|
|
itemSelected(event) {
|
|
|
|
|
event.preventDefault()
|
|
|
|
|
let currentSearch: string = this.searchField.value
|
|
|
|
|
let lastSpaceIndex = currentSearch.lastIndexOf(' ')
|
|
|
|
|
if (lastSpaceIndex != -1) {
|
|
|
|
|
currentSearch = currentSearch.substring(0, lastSpaceIndex + 1)
|
2022-03-11 10:53:32 -08:00
|
|
|
currentSearch += event.item + ' '
|
2020-10-27 17:05:14 +01:00
|
|
|
} else {
|
2022-03-11 10:53:32 -08:00
|
|
|
currentSearch = event.item + ' '
|
2020-10-27 17:05:14 +01:00
|
|
|
}
|
|
|
|
|
this.searchField.patchValue(currentSearch)
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-27 01:10:18 +01:00
|
|
|
search() {
|
2020-11-22 17:48:54 +01:00
|
|
|
this.closeMenu()
|
2022-05-20 15:16:17 -07:00
|
|
|
this.list.quickFilter([
|
2022-03-27 00:09:09 -07:00
|
|
|
{
|
|
|
|
|
rule_type: FILTER_FULLTEXT_QUERY,
|
|
|
|
|
value: (this.searchField.value as string).trim(),
|
|
|
|
|
},
|
2022-03-11 10:53:32 -08:00
|
|
|
])
|
2020-10-27 01:10:18 +01:00
|
|
|
}
|
|
|
|
|
|
2021-02-25 07:43:02 -08:00
|
|
|
closeDocument(d: PaperlessDocument) {
|
2022-03-11 10:53:32 -08:00
|
|
|
this.openDocumentsService
|
|
|
|
|
.closeDocument(d)
|
|
|
|
|
.pipe(first())
|
|
|
|
|
.subscribe((confirmed) => {
|
|
|
|
|
if (confirmed) {
|
|
|
|
|
this.closeMenu()
|
|
|
|
|
let route = this.activatedRoute.snapshot
|
|
|
|
|
while (route.firstChild) {
|
|
|
|
|
route = route.firstChild
|
|
|
|
|
}
|
|
|
|
|
if (
|
|
|
|
|
route.component == DocumentDetailComponent &&
|
|
|
|
|
route.params['id'] == d.id
|
|
|
|
|
) {
|
|
|
|
|
this.router.navigate([''])
|
|
|
|
|
}
|
2022-02-16 01:06:22 -08:00
|
|
|
}
|
2022-03-11 10:53:32 -08:00
|
|
|
})
|
2020-10-27 01:10:18 +01:00
|
|
|
}
|
|
|
|
|
|
2020-11-12 11:11:57 +01:00
|
|
|
closeAll() {
|
2021-01-26 22:27:50 -08:00
|
|
|
// user may need to confirm losing unsaved changes
|
2022-03-11 10:53:32 -08:00
|
|
|
this.openDocumentsService
|
|
|
|
|
.closeAll()
|
|
|
|
|
.pipe(first())
|
|
|
|
|
.subscribe((confirmed) => {
|
|
|
|
|
if (confirmed) {
|
|
|
|
|
this.closeMenu()
|
2021-01-26 22:27:50 -08:00
|
|
|
|
2022-03-11 10:53:32 -08:00
|
|
|
// TODO: is there a better way to do this?
|
|
|
|
|
let route = this.activatedRoute
|
|
|
|
|
while (route.firstChild) {
|
|
|
|
|
route = route.firstChild
|
|
|
|
|
}
|
|
|
|
|
if (route.component === DocumentDetailComponent) {
|
|
|
|
|
this.router.navigate([''])
|
|
|
|
|
}
|
2021-01-26 22:27:50 -08:00
|
|
|
}
|
2022-03-11 10:53:32 -08:00
|
|
|
})
|
2020-11-12 11:11:57 +01:00
|
|
|
}
|
2022-09-30 12:30:23 -07:00
|
|
|
|
|
|
|
|
private checkForUpdates() {
|
|
|
|
|
this.remoteVersionService
|
|
|
|
|
.checkForUpdates()
|
|
|
|
|
.subscribe((appRemoteVersion: AppRemoteVersion) => {
|
|
|
|
|
this.appRemoteVersion = appRemoteVersion
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setUpdateChecking(enable: boolean) {
|
|
|
|
|
this.settingsService.set(SETTINGS_KEYS.UPDATE_CHECKING_ENABLED, enable)
|
|
|
|
|
this.settingsService
|
|
|
|
|
.storeSettings()
|
|
|
|
|
.pipe(first())
|
|
|
|
|
.subscribe({
|
|
|
|
|
error: (error) => {
|
|
|
|
|
this.toastService.showError(
|
|
|
|
|
$localize`An error occurred while saving update checking settings.`
|
|
|
|
|
)
|
|
|
|
|
console.log(error)
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
if (enable) {
|
|
|
|
|
this.checkForUpdates()
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-10-27 01:10:18 +01:00
|
|
|
}
|