From 2f41d1300927505d3a2969792499b8c64e32ae8f Mon Sep 17 00:00:00 2001 From: Pete Matsyburka Date: Fri, 20 Dec 2024 13:31:24 +0200 Subject: [PATCH] add order fields --- app/javascript/submission_form/form.vue | 37 +++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/app/javascript/submission_form/form.vue b/app/javascript/submission_form/form.vue index 4ac1b56e..4468e494 100644 --- a/app/javascript/submission_form/form.vue +++ b/app/javascript/submission_form/form.vue @@ -643,6 +643,11 @@ export default { required: false, default: '-80px' }, + orderAsOnPage: { + type: Boolean, + required: false, + default: false + }, requireSigningReason: { type: Boolean, required: false, @@ -962,6 +967,38 @@ export default { return acc }, []) + if (this.orderAsOnPage) { + const fieldAreasIndex = {} + const attachmentUuids = Object.keys(this.attachmentConditionsIndex) + + const sortArea = (aArea, bArea) => { + if (aArea.attachment_uuid === bArea.attachment_uuid) { + if (aArea.page === bArea.page) { + if (Math.abs(aArea.y - bArea.y) < 0.01) { + if (aArea.x === bArea.x) { + return 0 + } else { + return aArea.x - bArea.x + } + } else { + return aArea.y - bArea.y + } + } else { + return aArea.page - bArea.page + } + } else { + return attachmentUuids.indexOf(aArea.attachment_uuid) - attachmentUuids.indexOf(bArea.attachment_uuid) + } + } + + sortedFields.sort((aField, bField) => { + const aArea = (fieldAreasIndex[aField.uuid] ||= [...(aField.areas || [])].sort(sortArea)[0]) + const bArea = (fieldAreasIndex[bField.uuid] ||= [...(bField.areas || [])].sort(sortArea)[0]) + + return sortArea(aArea, bArea) + }) + } + if (verificationFields.length) { sortedFields.push(verificationFields.pop()) }