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

106 lines
3.2 KiB
TypeScript
Raw Normal View History

2020-11-08 16:58:06 +01:00
import { Directive, OnInit, QueryList, ViewChildren } from '@angular/core';
2020-10-27 01:10:18 +01:00
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
2020-10-30 22:46:43 +01:00
import { MatchingModel, MATCHING_ALGORITHMS, MATCH_AUTO } from 'src/app/data/matching-model';
2020-10-27 01:10:18 +01:00
import { ObjectWithId } from 'src/app/data/object-with-id';
2020-11-08 16:58:06 +01:00
import { SortableDirective, SortEvent } from 'src/app/directives/sortable.directive';
2020-10-27 01:10:18 +01:00
import { AbstractPaperlessService } from 'src/app/services/rest/abstract-paperless-service';
import { ConfirmDialogComponent } from '../../common/confirm-dialog/confirm-dialog.component';
2020-10-27 01:10:18 +01:00
@Directive()
export abstract class GenericListComponent<T extends ObjectWithId> implements OnInit {
constructor(
private service: AbstractPaperlessService<T>,
private modalService: NgbModal,
private editDialogComponent: any) {
}
2020-11-08 16:58:06 +01:00
@ViewChildren(SortableDirective) headers: QueryList<SortableDirective>;
2020-10-27 01:10:18 +01:00
public data: T[] = []
public page = 1
public collectionSize = 0
2020-11-08 16:58:06 +01:00
public sortField: string
public sortDirection: string
2020-10-28 18:04:50 +01:00
getMatching(o: MatchingModel) {
2020-10-30 22:46:43 +01:00
if (o.matching_algorithm == MATCH_AUTO) {
2020-10-28 18:04:50 +01:00
return "Automatic"
} else if (o.match && o.match.length > 0) {
2020-10-30 22:46:43 +01:00
return `${o.match} (${MATCHING_ALGORITHMS.find(a => a.id == o.matching_algorithm).name})`
2020-10-28 18:04:50 +01:00
} else {
return "-"
}
}
2020-11-08 16:58:06 +01:00
onSort(event: SortEvent) {
if (event.direction && event.direction.length > 0) {
this.sortField = event.column
this.sortDirection = event.direction
} else {
this.sortField = null
this.sortDirection = null
}
this.headers.forEach(header => {
if (header.sortable !== this.sortField) {
header.direction = '';
}
});
this.reloadData()
}
2020-10-27 01:10:18 +01:00
ngOnInit(): void {
this.reloadData()
}
reloadData() {
2020-11-08 16:58:06 +01:00
this.service.list(this.page, null, this.sortField, this.sortDirection).subscribe(c => {
2020-10-27 01:10:18 +01:00
this.data = c.results
this.collectionSize = c.count
});
}
openCreateDialog() {
var activeModal = this.modalService.open(this.editDialogComponent, {backdrop: 'static'})
activeModal.componentInstance.dialogMode = 'create'
activeModal.componentInstance.success.subscribe(o => {
this.reloadData()
})
}
openEditDialog(object: T) {
var activeModal = this.modalService.open(this.editDialogComponent, {backdrop: 'static'})
activeModal.componentInstance.object = object
activeModal.componentInstance.dialogMode = 'edit'
activeModal.componentInstance.success.subscribe(o => {
this.reloadData()
})
}
2020-10-27 17:33:57 +01:00
getObjectName(object: T) {
return object.toString()
}
2020-10-27 01:10:18 +01:00
openDeleteDialog(object: T) {
var activeModal = this.modalService.open(ConfirmDialogComponent, {backdrop: 'static'})
activeModal.componentInstance.title = "Confirm delete"
activeModal.componentInstance.messageBold = `Do you really want to delete ${this.getObjectName(object)}?`
activeModal.componentInstance.message = "Associated documents will not be deleted."
activeModal.componentInstance.btnClass = "btn-danger"
activeModal.componentInstance.btnCaption = "Delete"
activeModal.componentInstance.confirmPressed.subscribe(() => {
2020-10-27 01:10:18 +01:00
this.service.delete(object).subscribe(_ => {
activeModal.close()
this.reloadData()
})
}
)
}
}