Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.2.0.oc] OC-22314 Date value should be masked when printed #235

Open
wants to merge 1 commit into
base: 7.2.0.oc
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 46 additions & 14 deletions packages/enketo-core/src/widget/text-print/text-print.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,57 @@ class TextPrintWidget extends Widget {
}

_addWidget() {
if (!this.widget) {
const className = 'print-input-text';
const printElement = document.createElement('div');
printElement.classList.add(className, 'widget');

this.element.after(printElement);
this.element.classList.add('print-hide');

this.widget = this.element.parentElement.querySelector(
`.${className}`
);
this.widget.innerHTML = this.element.value.replace(/\n/g, '<br>');
if (this.widget) {
return;
}

const previousElement = this.element.previousElementSibling;
const isDateWidget = previousElement?.classList.contains('date');

// If previous element is a date widget, change its value to masked value
if (isDateWidget) {
const dateInputElement = previousElement.querySelector('input');
if (dateInputElement) {
const elementValue = this.element.value || '';
dateInputElement.dataset.actualValue = elementValue;
dateInputElement.value = elementValue ? 'MaskedXXXXXXX' : '';
}
return;
}

// Create print-only element with value from original input
const className = 'print-input-text';
const printElement = document.createElement('div');
printElement.classList.add(className, 'widget');

printElement.innerHTML = this.element.value.replace(/\n/g, '<br>');
this.element.after(printElement);
this.element.classList.add('print-hide');

this.widget = printElement;
}

_removeWidget() {
this.element.classList.remove('print-hide');

const previousElement = this.element.previousElementSibling;
const isDateWidget = previousElement?.classList.contains('date');

// If previous element is a date widget, change its value to actual value
if (isDateWidget) {
const dateInputElement = previousElement.querySelector('input');
const actualValue = dateInputElement?.dataset.actualValue;
if (actualValue) {
dateInputElement.value = actualValue;
// Remove the data attribute
delete dateInputElement.dataset.actualValue;
}
return;
}

// Remove the print-only element
if (this.widget) {
this.element.classList.remove('print-hide');
this.element.parentElement.removeChild(this.widget);
this.widget.remove();
this.widget = null;
}
}
Expand Down
99 changes: 57 additions & 42 deletions packages/enketo-express/public/js/src/module/gui.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,59 +553,74 @@ function printOcForm() {
const textPrints = document.querySelectorAll(
'.question:not(.or-appearance-autocomplete):not(.or-appearance-url) > input[type=text]:not(.ignore):not([data-for]), .question:not(.or-appearance-autocomplete):not(.or-appearance-url) > textarea:not(.ignore):not([data-for])'
);
let historyAdded;
let historyAdded = false;

return new Promise((resolve) => {
if (formTheme === 'grid' || (!formTheme && printHelper.isGrid())) {
return prompt(texts, options, gridInputs).then((format) => {
if (!format) {
return;
}
if (format.queries === 'yes') {
historyAdded = true;
dns.forEach((dn) => {
dn.dispatchEvent(events.Printify());
});
}
textPrints.forEach((textPrint) => {
textPrint.dispatchEvent(events.Printify());
return new Promise((resolve, reject) => {
const cleanup = () => {
textPrints.forEach((textPrint) => {
textPrint.dispatchEvent(events.DePrintify());
});
if (historyAdded) {
return new Promise((resolveCleanup) => {
setTimeout(() => {
dns.forEach((dn) => {
dn.dispatchEvent(events.DePrintify());
});
resolveCleanup();
}, 1000);
});
}
return Promise.resolve();
};

return printGrid(format).then(resolve);
});
}
return prompt(texts, options, regularInputs).then((format) => {
const handlePrint = (format) => {
if (!format) {
return;
return cleanup().then(resolve);
}

if (format.queries === 'yes') {
historyAdded = true;
dns.forEach((dn) => {
dn.dispatchEvent(events.Printify());
});
}

textPrints.forEach((textPrint) => {
textPrint.dispatchEvent(events.Printify());
});
setTimeout(window.print, 100);
resolve();
});
}).then(() => {
textPrints.forEach((textPrint) => {
textPrint.dispatchEvent(events.DePrintify());
});
if (!historyAdded) {
return;
}

return new Promise((resolve) => {
setTimeout(() => {
dns.forEach((dn) => {
dn.dispatchEvent(events.DePrintify());
if (formTheme === 'grid' || (!formTheme && printHelper.isGrid())) {
return printGrid(format)
.then(() => cleanup())
.then(resolve)
.catch((error) => {
cleanup().then(() => reject(error));
});
}

return new Promise((resolvePrint) => {
setTimeout(() => {
window.print();
resolvePrint();
}, 100);
})
.then(() => cleanup())
.then(resolve)
.catch((error) => {
cleanup().then(() => reject(error));
});
resolve();
}, 1000);
});
};

const promptInputs =
formTheme === 'grid' || (!formTheme && printHelper.isGrid())
? gridInputs
: regularInputs;

prompt(texts, options, promptInputs)
.then(handlePrint)
.catch((error) => {
cleanup().then(() => reject(error));
});
});
}

Expand Down Expand Up @@ -648,19 +663,19 @@ function printGrid(format, delay = 800) {
setTimeout(() => {
printHelper
.fixGrid(format)
.then(window.print)
.then(() => window.print())
.catch(console.error)
.then(() => {
if (swapped) {
return new Promise((resolve) => {
return new Promise((innerResolve) => {
setTimeout(() => {
printHelper.styleReset();
resolve();
innerResolve();
}, 500);
});
}
});
resolve();
})
.finally(() => resolve());
}, delay)
);
}
Expand Down
Loading