Merge pull request #15570 from calixteman/15568

[JS] Take into account all the required fields for some computations
This commit is contained in:
calixteman 2022-10-13 21:57:35 +02:00 committed by GitHub
commit 09d077dc25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 154 additions and 10 deletions

View File

@ -500,14 +500,18 @@ class AForm {
const event = globalThis.event; const event = globalThis.event;
const values = []; const values = [];
cFields = this.AFMakeArrayFromList(cFields);
for (const cField of cFields) { for (const cField of cFields) {
const field = this._document.getField(cField); const field = this._document.getField(cField);
if (!field) { if (!field) {
continue; continue;
} }
const number = this.AFMakeNumber(field.value); for (const child of field.getArray()) {
if (number !== null) { const number = this.AFMakeNumber(child.value);
values.push(number); if (number !== null) {
values.push(number);
}
} }
} }

View File

@ -889,6 +889,24 @@ class Doc extends PDFObject {
return children; return children;
} }
_getTerminalChildren(fieldName) {
// Get all the descendants which have a value.
const children = [];
const len = fieldName.length;
for (const [name, field] of this._fields.entries()) {
if (name.startsWith(fieldName)) {
const finalPart = name.slice(len);
if (
field.obj._hasValue &&
(finalPart === "" || finalPart.startsWith("."))
) {
children.push(field.wrapped);
}
}
}
return children;
}
getIcon() { getIcon() {
/* Not implemented */ /* Not implemented */
} }

View File

@ -76,6 +76,7 @@ class Field extends PDFObject {
this._fillColor = data.fillColor || ["T"]; this._fillColor = data.fillColor || ["T"];
this._isChoice = Array.isArray(data.items); this._isChoice = Array.isArray(data.items);
this._items = data.items || []; this._items = data.items || [];
this._hasValue = data.hasOwnProperty("value");
this._page = data.page || 0; this._page = data.page || 0;
this._strokeColor = data.strokeColor || ["G", 0]; this._strokeColor = data.strokeColor || ["G", 0];
this._textColor = data.textColor || ["G", 0]; this._textColor = data.textColor || ["G", 0];
@ -393,15 +394,32 @@ class Field extends PDFObject {
} }
getArray() { getArray() {
// Gets the array of terminal child fields (that is, fields that can have
// a value for this Field object, the parent field).
if (this._kidIds) { if (this._kidIds) {
return this._kidIds.map(id => this._appObjects[id].wrapped); const array = [];
const fillArrayWithKids = kidIds => {
for (const id of kidIds) {
const obj = this._appObjects[id];
if (!obj) {
continue;
}
if (obj.obj._hasValue) {
array.push(obj.wrapped);
}
if (obj.obj._kidIds) {
fillArrayWithKids(obj.obj._kidIds);
}
}
};
fillArrayWithKids(this._kidIds);
return array;
} }
if (this._children === null) { if (this._children === null) {
this._children = this._document.obj this._children = this._document.obj._getTerminalChildren(this._fieldPath);
._getChildren(this._fieldPath)
.map(child => child.wrapped);
} }
return this._children; return this._children;
} }

View File

@ -200,7 +200,7 @@ describe("Scripting", function () {
value = await myeval( value = await myeval(
`this.getField("A.B.C.D").getArray().map((x) => x.value)` `this.getField("A.B.C.D").getArray().map((x) => x.value)`
); );
expect(value).toEqual([5, 7]); expect(value).toEqual([4, 5, 6, 7]);
}); });
}); });
@ -1037,7 +1037,7 @@ describe("Scripting", function () {
describe("AFSimple_Calculate", function () { describe("AFSimple_Calculate", function () {
it("should compute the sum of several fields", async () => { it("should compute the sum of several fields", async () => {
const refIds = [0, 1, 2, 3].map(_ => getId()); const refIds = [0, 1, 2, 3, 4].map(_ => getId());
const data = { const data = {
objects: { objects: {
field1: [ field1: [
@ -1076,9 +1076,21 @@ describe("Scripting", function () {
type: "text", type: "text",
}, },
], ],
field5: [
{
id: refIds[4],
value: "",
actions: {
Calculate: [
`AFSimple_Calculate("SUM", "field1, field2, field3, unknown");`,
],
},
type: "text",
},
],
}, },
appInfo: { language: "en-US", platform: "Linux x86_64" }, appInfo: { language: "en-US", platform: "Linux x86_64" },
calculationOrder: [refIds[3]], calculationOrder: [refIds[3], refIds[4]],
dispatchEventName: "_dispatchMe", dispatchEventName: "_dispatchMe",
}; };
@ -1124,6 +1136,98 @@ describe("Scripting", function () {
value: 6, value: 6,
formattedValue: null, formattedValue: null,
}); });
expect(send_queue.has(refIds[4])).toEqual(true);
expect(send_queue.get(refIds[4])).toEqual({
id: refIds[4],
siblings: null,
value: 6,
formattedValue: null,
});
});
it("should compute the sum of several fields in fields tree", async () => {
const refIds = [0, 1, 2, 3, 4, 5].map(_ => getId());
const data = {
objects: {
field1: [
{
id: refIds[0],
kidIds: [refIds[1], refIds[2]],
},
],
"field1.field2": [
{
id: refIds[1],
kidIds: [refIds[3]],
},
],
"field1.field3": [
{
id: refIds[2],
value: "",
actions: {},
type: "text",
},
],
"field1.field2.field4": [
{
id: refIds[3],
kidIds: [refIds[4]],
},
],
"field1.field2.field4.field5": [
{
id: refIds[4],
value: "",
actions: {},
type: "text",
},
],
field6: [
{
id: refIds[5],
value: "",
actions: {
Calculate: [`AFSimple_Calculate("SUM", "field1");`],
},
type: "text",
},
],
},
appInfo: { language: "en-US", platform: "Linux x86_64" },
calculationOrder: [refIds[5]],
dispatchEventName: "_dispatchMe",
};
sandbox.createSandbox(data);
await sandbox.dispatchEventInSandbox({
id: refIds[2],
value: "123",
name: "Keystroke",
willCommit: true,
});
expect(send_queue.has(refIds[5])).toEqual(true);
expect(send_queue.get(refIds[5])).toEqual({
id: refIds[5],
siblings: null,
value: 123,
formattedValue: null,
});
await sandbox.dispatchEventInSandbox({
id: refIds[4],
value: "456",
name: "Keystroke",
willCommit: true,
});
expect(send_queue.has(refIds[5])).toEqual(true);
expect(send_queue.get(refIds[5])).toEqual({
id: refIds[5],
siblings: null,
value: 579,
formattedValue: null,
});
}); });
}); });