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

91 lines
2.7 KiB
TypeScript
Raw Normal View History

2020-10-27 01:10:18 +01:00
import { Component, OnDestroy, OnInit } from '@angular/core';
import { FormControl } from '@angular/forms';
2020-11-12 11:11:57 +01:00
import { ActivatedRoute, Router } from '@angular/router';
import { from, Observable, Subscription } from 'rxjs';
2020-10-27 17:05:14 +01:00
import { debounceTime, distinctUntilChanged, map, switchMap } from 'rxjs/operators';
2020-10-27 01:10:18 +01:00
import { PaperlessDocument } from 'src/app/data/paperless-document';
import { OpenDocumentsService } from 'src/app/services/open-documents.service';
2020-10-27 17:05:14 +01:00
import { SearchService } from 'src/app/services/rest/search.service';
2020-10-30 22:46:43 +01:00
import { SavedViewConfigService } from 'src/app/services/saved-view-config.service';
2020-11-12 11:11:57 +01:00
import { DocumentDetailComponent } from '../document-detail/document-detail.component';
2020-10-27 17:05:14 +01:00
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
})
export class AppFrameComponent implements OnInit, OnDestroy {
2020-10-30 22:46:43 +01:00
constructor (
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 viewConfigService: SavedViewConfigService
) {
2020-10-27 01:10:18 +01:00
}
isMenuCollapsed: boolean = true
2020-10-27 01:10:18 +01:00
searchField = new FormControl('')
openDocuments: PaperlessDocument[] = []
openDocumentsSubscription: Subscription
2020-10-27 17:05:14 +01:00
searchAutoComplete = (text$: Observable<string>) =>
text$.pipe(
debounceTime(200),
distinctUntilChanged(),
map(term => {
if (term.lastIndexOf(' ') != -1) {
return term.substring(term.lastIndexOf(' ') + 1)
} else {
return term
}
}),
switchMap(term =>
term.length < 2 ? from([[]]) : this.searchService.autocomplete(term)
)
)
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 + " "
} else {
currentSearch = event.item + " "
}
this.searchField.patchValue(currentSearch)
}
2020-10-27 01:10:18 +01:00
search() {
this.router.navigate(['search'], {queryParams: {query: this.searchField.value}})
}
2020-11-12 11:11:57 +01:00
closeAll() {
this.openDocumentsService.closeAll()
// 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([""])
}
}
2020-10-27 01:10:18 +01:00
ngOnInit() {
2020-11-04 01:42:23 +01:00
this.openDocuments = this.openDocumentsService.getOpenDocuments()
2020-10-27 01:10:18 +01:00
}
ngOnDestroy() {
this.openDocumentsSubscription.unsubscribe()
}
}