paperless-ngx/src-ui/src/app/components/document-list/document-list.component.ts

180 lines
5.8 KiB
TypeScript
Raw Normal View History

import { Component, OnDestroy, OnInit, QueryList, ViewChild, ViewChildren } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
2020-10-30 22:46:43 +01:00
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { Subscription } from 'rxjs';
2021-04-04 00:03:51 +02:00
import { FilterRule } from 'src/app/data/filter-rule';
2021-04-03 21:07:36 +02:00
import { FILTER_FULLTEXT_MORELIKE } from 'src/app/data/filter-rule-type';
2020-12-22 02:59:09 +01:00
import { PaperlessDocument } from 'src/app/data/paperless-document';
import { PaperlessSavedView } from 'src/app/data/paperless-saved-view';
2021-01-04 17:08:52 +01:00
import { SortableDirective, SortEvent } from 'src/app/directives/sortable.directive';
import { ConsumerStatusService } from 'src/app/services/consumer-status.service';
2020-10-30 23:43:19 +01:00
import { DocumentListViewService } from 'src/app/services/document-list-view.service';
import { DOCUMENT_SORT_FIELDS } from 'src/app/services/rest/document.service';
import { SavedViewService } from 'src/app/services/rest/saved-view.service';
import { ToastService } from 'src/app/services/toast.service';
import { FilterEditorComponent } from './filter-editor/filter-editor.component';
2020-10-30 22:46:43 +01:00
import { SaveViewConfigDialogComponent } from './save-view-config-dialog/save-view-config-dialog.component';
2020-10-27 01:10:18 +01:00
@Component({
selector: 'app-document-list',
templateUrl: './document-list.component.html',
styleUrls: ['./document-list.component.scss']
2020-10-27 01:10:18 +01:00
})
export class DocumentListComponent implements OnInit, OnDestroy {
2020-10-27 01:10:18 +01:00
constructor(
public list: DocumentListViewService,
public savedViewService: SavedViewService,
2020-10-30 22:46:43 +01:00
public route: ActivatedRoute,
private router: Router,
private toastService: ToastService,
private modalService: NgbModal,
private consumerStatusService: ConsumerStatusService
) { }
2020-10-27 01:10:18 +01:00
@ViewChild("filterEditor")
private filterEditor: FilterEditorComponent
2020-10-27 01:10:18 +01:00
2021-01-04 17:08:52 +01:00
@ViewChildren(SortableDirective) headers: QueryList<SortableDirective>;
2020-10-27 01:10:18 +01:00
displayMode = 'smallCards' // largeCards, smallCards, details
2021-04-04 00:03:51 +02:00
unmodifiedFilterRules: FilterRule[] = []
2021-01-06 07:57:33 -08:00
private consumptionFinishedSubscription: Subscription
2021-01-06 10:57:13 -08:00
get isFiltered() {
return this.list.filterRules?.length > 0
}
getTitle() {
return this.list.activeSavedViewTitle || $localize`Documents`
}
2020-10-27 01:10:18 +01:00
getSortFields() {
2020-10-30 23:43:19 +01:00
return DOCUMENT_SORT_FIELDS
2020-10-27 01:10:18 +01:00
}
2021-01-04 17:08:52 +01:00
onSort(event: SortEvent) {
this.list.setSort(event.column, event.reverse)
}
2020-12-14 23:14:19 -08:00
get isBulkEditing(): boolean {
return this.list.selected.size > 0
}
2020-10-27 01:10:18 +01:00
saveDisplayMode() {
localStorage.setItem('document-list:displayMode', this.displayMode)
}
ngOnInit(): void {
if (localStorage.getItem('document-list:displayMode') != null) {
this.displayMode = localStorage.getItem('document-list:displayMode')
}
this.consumptionFinishedSubscription = this.consumerStatusService.onDocumentConsumptionFinished().subscribe(() => {
this.list.reload()
})
2020-10-30 22:46:43 +01:00
this.route.paramMap.subscribe(params => {
if (params.has('id')) {
this.savedViewService.getCached(+params.get('id')).subscribe(view => {
if (!view) {
this.router.navigate(["404"])
return
}
this.list.activateSavedView(view)
this.list.reload()
2021-04-04 00:03:51 +02:00
this.unmodifiedFilterRules = view.filter_rules
})
2020-10-30 22:46:43 +01:00
} else {
this.list.activateSavedView(null)
this.list.reload()
2021-04-04 00:03:51 +02:00
this.unmodifiedFilterRules = []
2020-10-30 22:46:43 +01:00
}
})
2020-10-27 01:10:18 +01:00
}
ngOnDestroy() {
if (this.consumptionFinishedSubscription) {
this.consumptionFinishedSubscription.unsubscribe()
}
}
loadViewConfig(view: PaperlessSavedView) {
this.list.loadSavedView(view)
2020-12-14 19:39:16 +01:00
this.list.reload()
2020-10-30 22:46:43 +01:00
}
saveViewConfig() {
if (this.list.activeSavedViewId != null) {
let savedView: PaperlessSavedView = {
id: this.list.activeSavedViewId,
filter_rules: this.list.filterRules,
sort_field: this.list.sortField,
sort_reverse: this.list.sortReverse
}
this.savedViewService.patch(savedView).subscribe(result => {
this.toastService.showInfo($localize`View "${this.list.activeSavedViewTitle}" saved successfully.`)
2021-04-04 01:25:54 +02:00
this.unmodifiedFilterRules = this.list.filterRules
})
}
}
saveViewConfigAs() {
2020-10-30 22:46:43 +01:00
let modal = this.modalService.open(SaveViewConfigDialogComponent, {backdrop: 'static'})
modal.componentInstance.defaultName = this.filterEditor.generateFilterName()
2020-10-30 22:46:43 +01:00
modal.componentInstance.saveClicked.subscribe(formValue => {
2021-01-03 21:44:53 +01:00
modal.componentInstance.buttonsEnabled = false
let savedView: PaperlessSavedView = {
name: formValue.name,
show_on_dashboard: formValue.showOnDashboard,
show_in_sidebar: formValue.showInSideBar,
filter_rules: this.list.filterRules,
sort_reverse: this.list.sortReverse,
sort_field: this.list.sortField
2020-12-14 21:14:33 +01:00
}
2020-12-14 21:14:33 +01:00
this.savedViewService.create(savedView).subscribe(() => {
modal.close()
2020-12-28 21:52:09 +01:00
this.toastService.showInfo($localize`View "${savedView.name}" created successfully.`)
2021-01-03 21:44:53 +01:00
}, error => {
modal.componentInstance.error = error.error
modal.componentInstance.buttonsEnabled = true
2020-10-30 22:46:43 +01:00
})
})
}
2021-01-14 14:10:23 -08:00
toggleSelected(document: PaperlessDocument, event: MouseEvent): void {
if (!event.shiftKey) this.list.toggleSelected(document)
else this.list.selectRangeTo(document)
}
clickTag(tagID: number) {
this.list.selectNone()
setTimeout(() => {
this.filterEditor.toggleTag(tagID)
})
}
clickCorrespondent(correspondentID: number) {
this.list.selectNone()
setTimeout(() => {
this.filterEditor.toggleCorrespondent(correspondentID)
})
}
clickDocumentType(documentTypeID: number) {
this.list.selectNone()
setTimeout(() => {
this.filterEditor.toggleDocumentType(documentTypeID)
})
}
clickMoreLike(documentID: number) {
2021-04-03 21:07:36 +02:00
this.list.quickFilter([{rule_type: FILTER_FULLTEXT_MORELIKE, value: documentID.toString()}])
}
2020-12-22 02:59:09 +01:00
trackByDocumentId(index, item: PaperlessDocument) {
return item.id
}
2020-10-27 01:10:18 +01:00
}