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

107 lines
3.3 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 {
2020-10-27 01:10:18 +01:00
constructor(
private service: AbstractPaperlessService<T>,
2020-10-27 01:10:18 +01:00
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) {
return $localize`Automatic`
2020-10-28 18:04:50 +01:00
} else if (o.match && o.match.length > 0) {
2021-01-01 23:08:02 +01:00
return `${MATCHING_ALGORITHMS.find(a => a.id == o.matching_algorithm).shortName}: ${o.match}`
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() {
// TODO: this is a hack
this.service.list(this.page, null, this.sortField, this.sortDirection == 'des').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-12-28 22:54:49 +01:00
getDeleteMessage(object: T) {
return $localize`Do you really want to delete this element?`
2020-10-27 17:33:57 +01:00
}
2020-10-27 01:10:18 +01:00
openDeleteDialog(object: T) {
var activeModal = this.modalService.open(ConfirmDialogComponent, {backdrop: 'static'})
activeModal.componentInstance.title = $localize`Confirm delete`
2020-12-28 22:54:49 +01:00
activeModal.componentInstance.messageBold = this.getDeleteMessage(object)
activeModal.componentInstance.message = $localize`Associated documents will not be deleted.`
activeModal.componentInstance.btnClass = "btn-danger"
activeModal.componentInstance.btnCaption = $localize`Delete`
2020-12-16 19:35:21 +01:00
activeModal.componentInstance.confirmClicked.subscribe(() => {
2020-10-27 01:10:18 +01:00
this.service.delete(object).subscribe(_ => {
activeModal.close()
this.reloadData()
})
}
)
}
}