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

96 lines
2.5 KiB
TypeScript
Raw Normal View History

2020-10-27 01:10:18 +01:00
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
2020-12-17 21:36:21 +01:00
import { PaperlessDocument } from 'src/app/data/paperless-document';
import { PaperlessDocumentType } from 'src/app/data/paperless-document-type';
2020-11-02 12:25:02 +01:00
import { SearchHit } from 'src/app/data/search-result';
2020-12-17 21:36:21 +01:00
import { DocumentService } from 'src/app/services/rest/document.service';
2020-11-02 12:25:02 +01:00
import { SearchService } from 'src/app/services/rest/search.service';
2020-10-27 01:10:18 +01:00
@Component({
selector: 'app-search',
templateUrl: './search.component.html',
styleUrls: ['./search.component.scss']
2020-10-27 01:10:18 +01:00
})
export class SearchComponent implements OnInit {
2020-11-02 12:25:02 +01:00
results: SearchHit[] = []
2020-10-27 01:10:18 +01:00
query: string = ""
2020-12-17 21:36:21 +01:00
more_like: number
more_like_doc: PaperlessDocument
2020-11-02 01:35:09 +01:00
searching = false
2020-11-02 12:25:02 +01:00
currentPage = 1
pageCount = 1
resultCount
correctedQuery: string = null
errorMessage: string
2020-12-17 21:36:21 +01:00
get maxScore() {
return this.results?.length > 0 ? this.results[0].score : 100
}
constructor(private searchService: SearchService, private route: ActivatedRoute, private router: Router, private documentService: DocumentService) { }
2020-10-27 01:10:18 +01:00
ngOnInit(): void {
this.route.queryParamMap.subscribe(paramMap => {
2020-12-17 23:41:55 +01:00
window.scrollTo(0, 0)
2020-10-27 01:10:18 +01:00
this.query = paramMap.get('query')
2020-12-17 21:36:21 +01:00
this.more_like = paramMap.has('more_like') ? +paramMap.get('more_like') : null
if (this.more_like) {
this.documentService.get(this.more_like).subscribe(r => {
this.more_like_doc = r
})
} else {
this.more_like_doc = null
}
2020-11-02 01:35:09 +01:00
this.searching = true
2020-11-02 12:25:02 +01:00
this.currentPage = 1
this.loadPage()
2020-10-27 01:10:18 +01:00
})
}
searchCorrectedQuery() {
2020-12-17 21:36:21 +01:00
this.router.navigate(["search"], {queryParams: {query: this.correctedQuery, more_like: this.more_like}})
2020-10-27 01:10:18 +01:00
}
2020-11-02 12:25:02 +01:00
loadPage(append: boolean = false) {
this.errorMessage = null
this.correctedQuery = null
2020-12-17 21:36:21 +01:00
this.searchService.search(this.query, this.currentPage, this.more_like).subscribe(result => {
2020-11-02 12:25:02 +01:00
if (append) {
this.results.push(...result.results)
} else {
this.results = result.results
}
this.pageCount = result.page_count
this.searching = false
this.resultCount = result.count
this.correctedQuery = result.corrected_query
}, error => {
this.searching = false
this.resultCount = 1
2020-11-30 16:19:32 +01:00
this.pageCount = 1
this.results = []
this.errorMessage = error.error
2020-11-02 12:25:02 +01:00
})
}
onScroll() {
if (this.currentPage < this.pageCount) {
this.currentPage += 1
this.loadPage(true)
}
}
2020-10-27 01:10:18 +01:00
}