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

217 lines
7.9 KiB
TypeScript
Raw Normal View History

2021-01-05 22:11:42 +01:00
import { Component, OnInit, ViewChild } from '@angular/core';
2020-10-27 01:10:18 +01:00
import { FormControl, FormGroup } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { PaperlessCorrespondent } from 'src/app/data/paperless-correspondent';
import { PaperlessDocument } from 'src/app/data/paperless-document';
import { PaperlessDocumentMetadata } from 'src/app/data/paperless-document-metadata';
2020-10-27 01:10:18 +01:00
import { PaperlessDocumentType } from 'src/app/data/paperless-document-type';
2020-12-16 16:44:54 +01:00
import { DocumentTitlePipe } from 'src/app/pipes/document-title.pipe';
2020-10-27 01:10:18 +01:00
import { DocumentListViewService } from 'src/app/services/document-list-view.service';
import { OpenDocumentsService } from 'src/app/services/open-documents.service';
import { CorrespondentService } from 'src/app/services/rest/correspondent.service';
import { DocumentTypeService } from 'src/app/services/rest/document-type.service';
import { DocumentService } from 'src/app/services/rest/document.service';
import { ConfirmDialogComponent } from '../common/confirm-dialog/confirm-dialog.component';
2020-10-27 01:10:18 +01:00
import { CorrespondentEditDialogComponent } from '../manage/correspondent-list/correspondent-edit-dialog/correspondent-edit-dialog.component';
import { DocumentTypeEditDialogComponent } from '../manage/document-type-list/document-type-edit-dialog/document-type-edit-dialog.component';
2020-12-18 14:31:09 -08:00
import { PDFDocumentProxy } from 'ng2-pdf-viewer';
2021-01-03 21:44:53 +01:00
import { ToastService } from 'src/app/services/toast.service';
2021-01-05 22:11:42 +01:00
import { TextComponent } from '../common/input/text/text.component';
2020-10-30 22:46:43 +01:00
2020-10-27 01:10:18 +01:00
@Component({
selector: 'app-document-detail',
templateUrl: './document-detail.component.html',
styleUrls: ['./document-detail.component.scss']
2020-10-27 01:10:18 +01:00
})
export class DocumentDetailComponent implements OnInit {
2021-01-05 22:11:42 +01:00
@ViewChild("inputTitle")
titleInput: TextComponent
expandOriginalMetadata = false
expandArchivedMetadata = false
error: any
networkActive = false
2020-12-08 15:28:09 +01:00
2020-10-27 01:10:18 +01:00
documentId: number
document: PaperlessDocument
metadata: PaperlessDocumentMetadata
2020-10-27 01:10:18 +01:00
title: string
previewUrl: string
downloadUrl: string
downloadOriginalUrl: string
2020-10-27 01:10:18 +01:00
correspondents: PaperlessCorrespondent[]
documentTypes: PaperlessDocumentType[]
documentForm: FormGroup = new FormGroup({
title: new FormControl(''),
content: new FormControl(''),
2020-11-04 13:10:23 +01:00
created: new FormControl(),
correspondent: new FormControl(),
document_type: new FormControl(),
2020-10-27 01:10:18 +01:00
archive_serial_number: new FormControl(),
tags: new FormControl([])
2020-10-27 01:10:18 +01:00
})
2020-12-18 14:47:06 -08:00
previewCurrentPage: number = 1
2020-12-19 01:15:40 +01:00
previewNumPages: number = 1
2020-12-18 14:31:09 -08:00
2020-10-27 01:10:18 +01:00
constructor(
2020-12-18 14:31:09 -08:00
private documentsService: DocumentService,
2020-10-27 01:10:18 +01:00
private route: ActivatedRoute,
private correspondentService: CorrespondentService,
private documentTypeService: DocumentTypeService,
private router: Router,
private modalService: NgbModal,
private openDocumentService: OpenDocumentsService,
2020-12-16 16:44:54 +01:00
private documentListViewService: DocumentListViewService,
2021-01-03 21:44:53 +01:00
private documentTitlePipe: DocumentTitlePipe,
private toastService: ToastService) { }
2020-10-27 01:10:18 +01:00
getContentType() {
return this.metadata?.has_archive_version ? 'application/pdf' : this.metadata?.original_mime_type
}
2020-10-27 01:10:18 +01:00
ngOnInit(): void {
2020-11-04 13:10:23 +01:00
this.documentForm.valueChanges.subscribe(wow => {
Object.assign(this.document, this.documentForm.value)
})
this.correspondentService.listAll().subscribe(result => this.correspondents = result.results)
this.documentTypeService.listAll().subscribe(result => this.documentTypes = result.results)
2020-10-27 01:10:18 +01:00
this.route.paramMap.subscribe(paramMap => {
this.documentId = +paramMap.get('id')
this.previewUrl = this.documentsService.getPreviewUrl(this.documentId)
this.downloadUrl = this.documentsService.getDownloadUrl(this.documentId)
this.downloadOriginalUrl = this.documentsService.getDownloadUrl(this.documentId, true)
2020-11-04 13:10:23 +01:00
if (this.openDocumentService.getOpenDocument(this.documentId)) {
this.updateComponent(this.openDocumentService.getOpenDocument(this.documentId))
} else {
this.documentsService.get(this.documentId).subscribe(doc => {
this.openDocumentService.openDocument(doc)
this.updateComponent(doc)
}, error => {this.router.navigate(['404'])})
}
2020-10-27 01:10:18 +01:00
})
}
2020-11-04 13:10:23 +01:00
updateComponent(doc: PaperlessDocument) {
this.document = doc
this.documentsService.getMetadata(doc.id).subscribe(result => {
this.metadata = result
})
2020-12-16 16:44:54 +01:00
this.title = this.documentTitlePipe.transform(doc.title)
2020-11-04 13:10:23 +01:00
this.documentForm.patchValue(doc)
}
2020-10-27 01:10:18 +01:00
createDocumentType() {
var modal = this.modalService.open(DocumentTypeEditDialogComponent, {backdrop: 'static'})
modal.componentInstance.dialogMode = 'create'
modal.componentInstance.success.subscribe(newDocumentType => {
2020-11-12 09:23:57 +01:00
this.documentTypeService.listAll().subscribe(documentTypes => {
2020-10-27 01:10:18 +01:00
this.documentTypes = documentTypes.results
this.documentForm.get('document_type').setValue(newDocumentType.id)
2020-10-27 01:10:18 +01:00
})
})
}
createCorrespondent() {
var modal = this.modalService.open(CorrespondentEditDialogComponent, {backdrop: 'static'})
modal.componentInstance.dialogMode = 'create'
modal.componentInstance.success.subscribe(newCorrespondent => {
2020-11-12 09:23:57 +01:00
this.correspondentService.listAll().subscribe(correspondents => {
2020-10-27 01:10:18 +01:00
this.correspondents = correspondents.results
this.documentForm.get('correspondent').setValue(newCorrespondent.id)
2020-10-27 01:10:18 +01:00
})
})
}
discard() {
this.documentsService.get(this.documentId).subscribe(doc => {
Object.assign(this.document, doc)
this.title = doc.title
this.documentForm.patchValue(doc)
}, error => {this.router.navigate(['404'])})
2020-10-27 01:10:18 +01:00
}
2020-12-18 14:31:09 -08:00
save() {
this.networkActive = true
2020-11-04 13:10:23 +01:00
this.documentsService.update(this.document).subscribe(result => {
2020-10-27 01:10:18 +01:00
this.close()
this.networkActive = false
this.error = null
}, error => {
this.networkActive = false
this.error = error.error
2020-10-27 01:10:18 +01:00
})
}
saveEditNext() {
this.networkActive = true
2020-11-04 13:10:23 +01:00
this.documentsService.update(this.document).subscribe(result => {
this.error = null
2020-10-27 01:10:18 +01:00
this.documentListViewService.getNext(this.document.id).subscribe(nextDocId => {
this.networkActive = false
2020-10-27 01:10:18 +01:00
if (nextDocId) {
this.openDocumentService.closeDocument(this.document)
this.router.navigate(['documents', nextDocId])
2021-01-05 22:11:42 +01:00
this.titleInput.focus()
2020-10-27 01:10:18 +01:00
}
}, error => {
this.networkActive = false
2020-10-27 01:10:18 +01:00
})
}, error => {
this.networkActive = false
this.error = error.error
2020-10-27 01:10:18 +01:00
})
}
close() {
this.openDocumentService.closeDocument(this.document)
if (this.documentListViewService.savedViewId) {
this.router.navigate(['view', this.documentListViewService.savedViewId])
} else {
this.router.navigate(['documents'])
}
2020-10-27 01:10:18 +01:00
}
delete() {
let modal = this.modalService.open(ConfirmDialogComponent, {backdrop: 'static'})
modal.componentInstance.title = $localize`Confirm delete`
2020-12-30 16:29:22 +01:00
modal.componentInstance.messageBold = $localize`Do you really want to delete document "${this.document.title}"?`
modal.componentInstance.message = $localize`The files for this document will be deleted permanently. This operation cannot be undone.`
modal.componentInstance.btnClass = "btn-danger"
modal.componentInstance.btnCaption = $localize`Delete document`
modal.componentInstance.confirmClicked.subscribe(() => {
2021-01-03 21:44:53 +01:00
modal.componentInstance.buttonsEnabled = false
2020-10-27 01:10:18 +01:00
this.documentsService.delete(this.document).subscribe(() => {
2020-12-18 14:31:09 -08:00
modal.close()
2020-10-27 01:10:18 +01:00
this.close()
2021-01-03 21:44:53 +01:00
}, error => {
this.toastService.showError($localize`Error deleting document: ${JSON.stringify(error)}`)
modal.componentInstance.buttonsEnabled = true
2020-10-27 01:10:18 +01:00
})
})
}
2020-12-17 21:36:21 +01:00
moreLike() {
this.router.navigate(["search"], {queryParams: {more_like:this.document.id}})
}
2020-10-27 01:10:18 +01:00
hasNext() {
return this.documentListViewService.hasNext(this.documentId)
}
2020-12-18 14:31:09 -08:00
pdfPreviewLoaded(pdf: PDFDocumentProxy) {
this.previewNumPages = pdf.numPages
}
2020-10-27 01:10:18 +01:00
}