|
| 1 | +import { Component } from '@angular/core'; |
| 2 | +import { NgbActiveModal, NgbModal } from '@ng-bootstrap/ng-bootstrap'; |
| 3 | +import { CategoryForm, FieldDocument, Document, RelationsManager, Relation, Resource, |
| 4 | + Datastore } from 'idai-field-core'; |
| 5 | +import { Menus } from '../../../../services/menus'; |
| 6 | +import { MenuContext } from '../../../../services/menu-context'; |
| 7 | +import { DoceditComponent } from '../../../docedit/docedit.component'; |
| 8 | +import { Messages } from '../../../messages/messages'; |
| 9 | +import { M } from '../../../messages/m'; |
| 10 | + |
| 11 | + |
| 12 | +@Component({ |
| 13 | + templateUrl: './workflow-editor-modal.html', |
| 14 | + host: { |
| 15 | + '(window:keydown)': 'onKeyDown($event)' |
| 16 | + }, |
| 17 | + standalone: false |
| 18 | +}) |
| 19 | +/** |
| 20 | + * @author Thomas Kleinke |
| 21 | + */ |
| 22 | +export class WorkflowEditorModalComponent { |
| 23 | + |
| 24 | + public document: FieldDocument; |
| 25 | + public workflowSteps: Array<Document>; |
| 26 | + |
| 27 | + |
| 28 | + constructor(private activeModal: NgbActiveModal, |
| 29 | + private menus: Menus, |
| 30 | + private modalService: NgbModal, |
| 31 | + private relationsManager: RelationsManager, |
| 32 | + private datastore: Datastore, |
| 33 | + private messages: Messages) {} |
| 34 | + |
| 35 | + |
| 36 | + public cancel = () => this.activeModal.close(); |
| 37 | + |
| 38 | + |
| 39 | + public async onKeyDown(event: KeyboardEvent) { |
| 40 | + |
| 41 | + if (event.key === 'Escape' && this.menus.getContext() === MenuContext.WORKFLOW_EDITOR) { |
| 42 | + this.cancel(); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + |
| 47 | + public async initialize() { |
| 48 | + |
| 49 | + await this.updateWorkflowSteps(); |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + public async createWorkflowStep(category: CategoryForm) { |
| 54 | + |
| 55 | + const newWorkflowStep: Document = await this.openEditorModal( |
| 56 | + WorkflowEditorModalComponent.buildWorkflowStepDocument(category) |
| 57 | + ); |
| 58 | + if (!newWorkflowStep) return; |
| 59 | + |
| 60 | + await this.addRelation(newWorkflowStep); |
| 61 | + await this.updateWorkflowSteps(); |
| 62 | + } |
| 63 | + |
| 64 | + |
| 65 | + /** |
| 66 | + * @returns edited document if changes have been saved, undefined if the modal has been canceled |
| 67 | + */ |
| 68 | + private async openEditorModal(document: Document): Promise<Document|undefined> { |
| 69 | + |
| 70 | + this.menus.setContext(MenuContext.DOCEDIT); |
| 71 | + |
| 72 | + const doceditRef = this.modalService.open(DoceditComponent, |
| 73 | + { size: 'lg', backdrop: 'static', keyboard: false, animation: false }); |
| 74 | + doceditRef.componentInstance.setDocument(document); |
| 75 | + |
| 76 | + try { |
| 77 | + return (await doceditRef.result).document; |
| 78 | + } catch(_) { |
| 79 | + // Modal has been canceled |
| 80 | + return undefined; |
| 81 | + } finally { |
| 82 | + this.menus.setContext(MenuContext.WORKFLOW_EDITOR); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + private async addRelation(workflowStep: Document) { |
| 88 | + |
| 89 | + const oldVersion: Document = Document.clone(workflowStep); |
| 90 | + |
| 91 | + const resource: Resource = this.document.resource; |
| 92 | + if (!resource.relations) resource.relations = {}; |
| 93 | + if (!resource.relations[Relation.HAS_WORKFLOW_STEP]) resource.relations[Relation.HAS_WORKFLOW_STEP] = []; |
| 94 | + resource.relations[Relation.HAS_WORKFLOW_STEP].push(workflowStep.resource.id); |
| 95 | + |
| 96 | + try { |
| 97 | + await this.relationsManager.update(this.document, oldVersion); |
| 98 | + } catch (err) { |
| 99 | + console.error(err); |
| 100 | + this.messages.add([M.DOCEDIT_ERROR_SAVE]); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + |
| 105 | + private async updateWorkflowSteps() { |
| 106 | + |
| 107 | + const targetIds: string[] = this.document.resource.relations?.[Relation.HAS_WORKFLOW_STEP] ?? []; |
| 108 | + this.workflowSteps = await this.datastore.getMultiple(targetIds); |
| 109 | + } |
| 110 | + |
| 111 | + |
| 112 | + private static buildWorkflowStepDocument(category: CategoryForm): Document { |
| 113 | + |
| 114 | + return <Document> { |
| 115 | + resource: { |
| 116 | + relations: {}, |
| 117 | + category: category.name |
| 118 | + } |
| 119 | + }; |
| 120 | + } |
| 121 | +} |
0 commit comments