]> git.ipfire.org Git - thirdparty/paperless-ngx.git/blob
9a253f488bc9cdacb24d49ca6f06fc188194c518
[thirdparty/paperless-ngx.git] /
1 import { Component, EventEmitter, Output } from '@angular/core'
2 import { FormControl, FormGroup } from '@angular/forms'
3 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'
4 import { first } from 'rxjs'
5 import { CustomField, CustomFieldDataType } from 'src/app/data/custom-field'
6 import { DocumentService } from 'src/app/services/rest/document.service'
7
8 @Component({
9 selector: 'pngx-custom-fields-bulk-edit-dialog',
10 templateUrl: './custom-fields-bulk-edit-dialog.component.html',
11 styleUrl: './custom-fields-bulk-edit-dialog.component.scss',
12 })
13 export class CustomFieldsBulkEditDialogComponent {
14 CustomFieldDataType = CustomFieldDataType
15
16 @Output()
17 succeeded = new EventEmitter()
18
19 @Output()
20 failed = new EventEmitter()
21
22 public networkActive = false
23
24 public customFields: CustomField[] = []
25
26 private _fieldsToAdd: CustomField[] = [] // static object for change detection
27 public get fieldsToAdd() {
28 return this._fieldsToAdd
29 }
30
31 private _fieldsToAddIds: number[] = []
32 public get fieldsToAddIds() {
33 return this._fieldsToAddIds
34 }
35 public set fieldsToAddIds(ids: number[]) {
36 this._fieldsToAddIds = ids
37 this._fieldsToAdd = this.customFields.filter((field) =>
38 this._fieldsToAddIds.includes(field.id)
39 )
40 this.initForm()
41 }
42
43 public fieldsToRemoveIds: number[] = []
44
45 public form: FormGroup = new FormGroup({})
46
47 public documents: number[] = []
48
49 constructor(
50 private activeModal: NgbActiveModal,
51 private documentService: DocumentService
52 ) {}
53
54 initForm() {
55 Object.keys(this.form.controls).forEach((key) => {
56 if (!this._fieldsToAddIds.includes(parseInt(key))) {
57 this.form.removeControl(key)
58 }
59 })
60 this._fieldsToAddIds.forEach((field_id) => {
61 this.form.addControl(field_id.toString(), new FormControl(null))
62 })
63 }
64
65 public save() {
66 this.documentService
67 .bulkEdit(this.documents, 'modify_custom_fields', {
68 add_custom_fields: this.form.value,
69 remove_custom_fields: this.fieldsToRemoveIds,
70 })
71 .pipe(first())
72 .subscribe({
73 next: () => {
74 this.activeModal.close()
75 this.succeeded.emit()
76 },
77 error: (error) => {
78 this.failed.emit(error)
79 },
80 })
81 }
82
83 public cancel() {
84 this.activeModal.close()
85 }
86
87 public removeField(fieldId: number) {
88 this.fieldsToAddIds = this._fieldsToAddIds.filter((id) => id !== fieldId)
89 }
90 }