Skip to content

Commit

Permalink
XFA - Add a storage to save fields values
Browse files Browse the repository at this point in the history
  - this is required to be able to print (or save) a document. Some pages can be unloaded (because pdf.js is lazy) and this storage will help to save their data in order to resuse them when printing or just when displaying a page again.
  • Loading branch information
calixteman committed May 24, 2021
1 parent 4ccdaa7 commit b544088
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 30 deletions.
8 changes: 8 additions & 0 deletions src/core/xfa/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -909,11 +909,14 @@ class CheckButton extends XFAObject {
style.height = size;
}

const fieldId = this[$getParent]()[$getParent]()[$uid];
const input = {
name: "input",
attributes: {
class: "xfaCheckbox",
fieldId,
type: "radio",
id: `${fieldId}-radio`,
},
};

Expand Down Expand Up @@ -991,6 +994,7 @@ class ChoiceList extends XFAObject {

const selectAttributes = {
class: "xfaSelect",
fieldId: this[$getParent]()[$getParent]()[$uid],
style,
};

Expand Down Expand Up @@ -1217,6 +1221,7 @@ class DateTimeEdit extends XFAObject {
name: "input",
attributes: {
type: "text",
fieldId: this[$getParent]()[$getParent]()[$uid],
class: "xfaTextfield",
style,
},
Expand Down Expand Up @@ -2977,6 +2982,7 @@ class NumericEdit extends XFAObject {
name: "input",
attributes: {
type: "text",
fieldId: this[$getParent]()[$getParent]()[$uid],
class: "xfaTextfield",
style,
},
Expand Down Expand Up @@ -4515,6 +4521,7 @@ class TextEdit extends XFAObject {
html = {
name: "textarea",
attributes: {
fieldId: this[$getParent]()[$getParent]()[$uid],
class: "xfaTextfield",
style,
},
Expand All @@ -4524,6 +4531,7 @@ class TextEdit extends XFAObject {
name: "input",
attributes: {
type: "text",
fieldId: this[$getParent]()[$getParent]()[$uid],
class: "xfaTextfield",
style,
},
Expand Down
68 changes: 63 additions & 5 deletions src/display/xfa_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,60 @@
*/

class XfaLayer {
static setAttributes(html, attrs) {
for (const [key, value] of Object.entries(attrs)) {
if (value === null || value === undefined) {
static setupStorage(html, fieldId, element, storage) {
const storedData = storage.getValue(fieldId, { value: null });
switch (element.name) {
case "textarea":
html.textContent = storedData.value !== null ? storedData.value : "";
html.addEventListener("input", event => {
storage.setValue(fieldId, { value: event.target.value });
});
break;
case "input":
if (storedData.value !== null) {
html.setAttribute("value", storedData.value);
}
if (element.attributes.type === "radio") {
html.addEventListener("change", event => {
const { target } = event;
for (const radio of document.getElementsByName(target.name)) {
if (radio !== target) {
const id = radio.id;
storage.setValue(id.split("-")[0], { value: false });
}
}
storage.setValue(fieldId, { value: target.checked });
});
} else {
html.addEventListener("input", event => {
storage.setValue(fieldId, { value: event.target.value });
});
}
break;
case "select":
if (storedData.value !== null) {
for (const option of element.children) {
if (option.attributes.value === storedData.value) {
option.attributes.selected = true;
}
}
}
html.addEventListener("input", event => {
const options = event.target.options;
const value =
options.selectedIndex === -1
? null
: options[options.selectedIndex].value;
storage.setValue(fieldId, { value });
});
break;
}
}

static setAttributes(html, element, storage) {
const { attributes } = element;
for (const [key, value] of Object.entries(attributes)) {
if (value === null || value === undefined || key === "fieldId") {
continue;
}

Expand All @@ -30,13 +81,20 @@ class XfaLayer {
Object.assign(html.style, value);
}
}

// Set the value after the others to be sure overwrite
// any other values.
if ("fieldId" in attributes && storage) {
XfaLayer.setupStorage(html, attributes.fieldId, element, storage);
}
}

static render(parameters) {
const storage = parameters.annotationStorage;
const root = parameters.xfa;
const rootHtml = document.createElement(root.name);
if (root.attributes) {
XfaLayer.setAttributes(rootHtml, root.attributes);
XfaLayer.setAttributes(rootHtml, root);
}
const stack = [[root, -1, rootHtml]];

Expand Down Expand Up @@ -69,7 +127,7 @@ class XfaLayer {
const childHtml = document.createElement(name);
html.appendChild(childHtml);
if (child.attributes) {
XfaLayer.setAttributes(childHtml, child.attributes);
XfaLayer.setAttributes(childHtml, child, storage);
}

if (child.children && child.children.length > 0) {
Expand Down
6 changes: 5 additions & 1 deletion web/base_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1317,12 +1317,16 @@ class BaseViewer {
/**
* @param {HTMLDivElement} pageDiv
* @param {PDFPage} pdfPage
* @param {AnnotationStorage} [annotationStorage] - Storage for annotation
* data in forms.
* @returns {XfaLayerBuilder}
*/
createXfaLayerBuilder(pageDiv, pdfPage) {
createXfaLayerBuilder(pageDiv, pdfPage, annotationStorage = null) {
return new XfaLayerBuilder({
pageDiv,
pdfPage,
annotationStorage:
annotationStorage || this.pdfDocument?.annotationStorage,
});
}

Expand Down
3 changes: 2 additions & 1 deletion web/pdf_page_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,8 @@ class PDFPageView {
if (!this.xfaLayer) {
this.xfaLayer = this.xfaLayerFactory.createXfaLayerBuilder(
div,
pdfPage
pdfPage,
/* annotationStorage = */ null
);
}
this._renderXfaLayer();
Expand Down
55 changes: 32 additions & 23 deletions web/xfa_layer_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ import { XfaLayer } from "pdfjs-lib";
* @typedef {Object} XfaLayerBuilderOptions
* @property {HTMLDivElement} pageDiv
* @property {PDFPage} pdfPage
* @property {AnnotationStorage} [annotationStorage]
*/

class XfaLayerBuilder {
/**
* @param {XfaLayerBuilderOptions} options
*/
constructor({ pageDiv, pdfPage }) {
constructor({ pageDiv, pdfPage, annotationStorage }) {
this.pageDiv = pageDiv;
this.pdfPage = pdfPage;
this.annotationStorage = annotationStorage;

this.div = null;
this._cancelled = false;
Expand All @@ -40,29 +42,34 @@ class XfaLayerBuilder {
* annotations is complete.
*/
render(viewport, intent = "display") {
return this.pdfPage.getXfa().then(xfa => {
if (this._cancelled) {
return;
}
return this.pdfPage
.getXfa()
.then(xfa => {
if (this._cancelled) {
return;
}
const parameters = {
viewport: viewport.clone({ dontFlip: true }),
div: this.div,
xfa,
page: this.pdfPage,
annotationStorage: this.annotationStorage,
};

const parameters = {
viewport: viewport.clone({ dontFlip: true }),
div: this.div,
xfa,
page: this.pdfPage,
};
if (this.div) {
XfaLayer.update(parameters);
} else {
// Create an xfa layer div and render the form
this.div = document.createElement("div");
this.pageDiv.appendChild(this.div);
parameters.div = this.div;

if (this.div) {
XfaLayer.update(parameters);
} else {
// Create an xfa layer div and render the form
this.div = document.createElement("div");
this.pageDiv.appendChild(this.div);
parameters.div = this.div;

XfaLayer.render(parameters);
}
});
XfaLayer.render(parameters);
}
})
.catch(error => {
console.error(error);
});
}

cancel() {
Expand All @@ -84,11 +91,13 @@ class DefaultXfaLayerFactory {
/**
* @param {HTMLDivElement} pageDiv
* @param {PDFPage} pdfPage
* @param {AnnotationStorage} [annotationStorage]
*/
createXfaLayerBuilder(pageDiv, pdfPage) {
createXfaLayerBuilder(pageDiv, pdfPage, annotationStorage = null) {
return new XfaLayerBuilder({
pageDiv,
pdfPage,
annotationStorage,
});
}
}
Expand Down

0 comments on commit b544088

Please sign in to comment.