paperless-ngx/src-ui/src/app/components/app-frame/app-frame.component.ts

160 lines
4.7 KiB
TypeScript
Raw Normal View History

import { Component } from '@angular/core'
import { FormControl } from '@angular/forms'
import { ActivatedRoute, Router, Params } from '@angular/router'
import { from, Observable, Subscription, BehaviorSubject } from 'rxjs'
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'
import { Meta } from '@angular/platform-browser'
import { DocumentListViewService } from 'src/app/services/document-list-view.service'
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'
import { QueryParamsService } from 'src/app/services/query-params.service'
2020-10-27 01:10:18 +01:00
@Component({
selector: 'app-app-frame',
templateUrl: './app-frame.component.html',
styleUrls: ['./app-frame.component.scss'],
2020-10-27 01:10:18 +01:00
})
2022-02-16 01:06:22 -08:00
export class AppFrameComponent {
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,
public savedViewService: SavedViewService,
private list: DocumentListViewService,
2022-04-01 01:53:59 -07:00
private meta: Meta,
private remoteVersionService: RemoteVersionService,
private queryParamsService: QueryParamsService
2022-04-01 01:53:59 -07:00
) {
this.remoteVersionService
.checkForUpdates()
.subscribe((appRemoteVersion: AppRemoteVersion) => {
this.appRemoteVersion = appRemoteVersion
})
}
2020-10-27 01:10:18 +01: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
isMenuCollapsed: boolean = true
2020-11-22 17:48:54 +01:00
closeMenu() {
this.isMenuCollapsed = true
}
2020-10-27 01:10:18 +01:00
searchField = new FormControl('')
get openDocuments(): PaperlessDocument[] {
return this.openDocumentsService.getOpenDocuments()
}
2020-10-27 01:10:18 +01:00
2020-10-27 17:05:14 +01:00
searchAutoComplete = (text$: Observable<string>) =>
text$.pipe(
debounceTime(200),
distinctUntilChanged(),
map((term) => {
2020-10-27 17:05:14 +01:00
if (term.lastIndexOf(' ') != -1) {
return term.substring(term.lastIndexOf(' ') + 1)
} else {
return term
}
}),
switchMap((term) =>
2020-10-27 17:05:14 +01:00
term.length < 2 ? from([[]]) : this.searchService.autocomplete(term)
)
)
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)
currentSearch += event.item + ' '
2020-10-27 17:05:14 +01:00
} else {
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-05 08:36:18 -07:00
this.queryParamsService.navigateWithFilterRules([
2022-03-27 00:09:09 -07:00
{
rule_type: FILTER_FULLTEXT_QUERY,
value: (this.searchField.value as string).trim(),
},
])
2020-10-27 01:10:18 +01:00
}
closeDocument(d: PaperlessDocument) {
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
}
})
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
this.openDocumentsService
.closeAll()
.pipe(first())
.subscribe((confirmed) => {
if (confirmed) {
this.closeMenu()
2021-01-26 22:27:50 -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
}
})
2020-11-12 11:11:57 +01:00
}
get displayName() {
// TODO: taken from dashboard component, is this the best way to pass around username?
let tagFullName = this.meta.getTag('name=full_name')
let tagUsername = this.meta.getTag('name=username')
if (tagFullName && tagFullName.content) {
return tagFullName.content
} else if (tagUsername && tagUsername.content) {
return tagUsername.content
} else {
return null
}
}
2020-10-27 01:10:18 +01:00
}