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

193 lines
7 KiB
TypeScript
Raw Normal View History

import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
2020-10-27 01:10:18 +01:00
import { FormControl, FormGroup } from '@angular/forms';
2020-12-07 22:15:56 +01:00
import { Title } from '@angular/platform-browser';
2020-10-27 01:10:18 +01:00
import { ActivatedRoute, Router } from '@angular/router';
import { NgbModal, NgbNav } from '@ng-bootstrap/ng-bootstrap';
2020-10-27 01:10:18 +01:00
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';
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';
2020-12-07 22:15:56 +01:00
import { environment } from 'src/environments/environment';
2020-10-27 01:10:18 +01:00
import { DeleteDialogComponent } from '../common/delete-dialog/delete-dialog.component';
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-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 {
2020-12-08 15:28:09 +01:00
public expandOriginalMetadata = false;
public expandArchivedMetadata = false;
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
})
@ViewChild('nav') nav: NgbNav
@ViewChild('pdfPreview') set pdfPreview(element): void {
// this gets called when compontent added or removed from DOM
if (element && element.nativeElement.offsetParent !== null) { // its visible
setTimeout(()=> this.nav?.select(1));
}
}
2020-10-27 01:10:18 +01:00
constructor(
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-07 22:15:56 +01:00
private documentListViewService: DocumentListViewService,
private titleService: Title) { }
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
2020-12-07 22:15:56 +01:00
this.titleService.setTitle(`${doc.title} - ${environment.appTitle}`)
this.documentsService.getMetadata(doc.id).subscribe(result => {
this.metadata = result
})
2020-11-04 13:10:23 +01:00
this.title = doc.title
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
}
save() {
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()
})
}
saveEditNext() {
2020-11-04 13:10:23 +01:00
this.documentsService.update(this.document).subscribe(result => {
2020-10-27 01:10:18 +01:00
this.documentListViewService.getNext(this.document.id).subscribe(nextDocId => {
if (nextDocId) {
this.openDocumentService.closeDocument(this.document)
this.router.navigate(['documents', nextDocId])
}
})
})
}
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(DeleteDialogComponent, {backdrop: 'static'})
modal.componentInstance.message = `Do you really want to delete document '${this.document.title}'?`
modal.componentInstance.message2 = `The files for this document will be deleted permanently. This operation cannot be undone.`
modal.componentInstance.deleteClicked.subscribe(() => {
this.documentsService.delete(this.document).subscribe(() => {
modal.close()
2020-10-27 01:10:18 +01:00
this.close()
})
})
}
hasNext() {
return this.documentListViewService.hasNext(this.documentId)
}
previewCreated() {
console.log('Preview Created');
}
mobilePreviewCreated() {
console.log('Mobile Preview Created');
}
2020-10-27 01:10:18 +01:00
}