Refactor: Consolidate image field logic into PDFIframe.prototype.refreshSignItemsForPage to avoid overriding enableCustom methods and improve patching strategy.
This commit is contained in:
parent
c02e629a88
commit
424e68c57c
@ -4,7 +4,7 @@ import { patch } from "@web/core/utils/patch";
|
||||
import { PDFIframe } from "@sign/components/sign_request/PDF_iframe";
|
||||
import { SignablePDFIframe } from "@sign/components/sign_request/signable_PDF_iframe";
|
||||
|
||||
console.log("Sign Image Field: Initializing diagnostic patches...");
|
||||
console.log("Sign Image Field: Re-evaluating patch strategy without overriding enableCustom for base classes");
|
||||
|
||||
/**
|
||||
* Diagnostic Bridge Restoration
|
||||
@ -16,7 +16,11 @@ const restoreBridge = (instance, caller) => {
|
||||
const iframe = iwin.frameElement;
|
||||
|
||||
if (iframe && !iframe.odoo_iframe_instance) {
|
||||
// Only log once per frame to reduce noise
|
||||
if (!iframe._odoo_bridge_logged) {
|
||||
console.log(`[Diagnostic] Restoring bridge on iframe element from ${caller}`);
|
||||
iframe._odoo_bridge_logged = true;
|
||||
}
|
||||
iframe.odoo_iframe_instance = instance;
|
||||
}
|
||||
|
||||
@ -92,68 +96,30 @@ window.diagnosticForceShowFields = () => {
|
||||
hiddenItems.forEach(el => el.classList.remove('d-none'));
|
||||
};
|
||||
|
||||
const hookCheckSignItemsCompletion = (instance) => {
|
||||
console.log("[Diagnostic] checkSignItemsCompletion hooked. Total signItems mapped:");
|
||||
let totalItems = 0;
|
||||
for (const page in instance.signItems) {
|
||||
const items = instance.signItems[page];
|
||||
totalItems += Object.keys(items).length;
|
||||
console.log(` Page ${page}: ${Object.keys(items).length} items`);
|
||||
}
|
||||
console.log(`[Diagnostic] Total items managed by PDFIframe: ${totalItems}`);
|
||||
};
|
||||
const processedItems = new WeakSet();
|
||||
|
||||
// 1. Patch PDFIframe (ROOT)
|
||||
// Safest Patching Strategy: Only patch `PDFIframe.prototype.refreshSignItemsForPage`
|
||||
// This avoids ANY issues with `enableCustom` halting mid-execution.
|
||||
patch(PDFIframe.prototype, {
|
||||
enableCustom(signItem) {
|
||||
restoreBridge(this, 'PDFIframe.enableCustom');
|
||||
|
||||
if (typeof super.enableCustom === 'function') {
|
||||
super.enableCustom(...arguments);
|
||||
}
|
||||
|
||||
if (signItem && signItem.data && signItem.data.type === 'image' && signItem.el) {
|
||||
setupLazyObserver(signItem.el);
|
||||
}
|
||||
},
|
||||
|
||||
refreshSignItemsForPage(page) {
|
||||
console.log(`[Diagnostic] refreshSignItemsForPage called for page ${page}`);
|
||||
// Restore bridge right before items are evaluated
|
||||
restoreBridge(this, 'PDFIframe.refreshSignItemsForPage');
|
||||
|
||||
try {
|
||||
return super.refreshSignItemsForPage(...arguments);
|
||||
} catch (error) {
|
||||
console.error("[Diagnostic] Error inside refreshSignItemsForPage:", error);
|
||||
}
|
||||
},
|
||||
// Execute original first so elements get un-hidden (d-none removed)
|
||||
const result = super.refreshSignItemsForPage(...arguments);
|
||||
|
||||
refreshSignItems(page = false) {
|
||||
console.log(`[Diagnostic] refreshSignItems called (page: ${page})`);
|
||||
restoreBridge(this, 'PDFIframe.refreshSignItems');
|
||||
return super.refreshSignItems(...arguments);
|
||||
}
|
||||
});
|
||||
|
||||
// 2. Patch SignablePDFIframe (Signer subclass)
|
||||
patch(SignablePDFIframe.prototype, {
|
||||
enableCustom(signItem) {
|
||||
restoreBridge(this, 'SignablePDFIframe.enableCustom');
|
||||
|
||||
if (signItem && signItem.data && signItem.data.type === 'image' && signItem.el) {
|
||||
setupLazyObserver(signItem.el);
|
||||
}
|
||||
|
||||
super.enableCustom(...arguments);
|
||||
|
||||
if (signItem && signItem.data && signItem.data.type === 'image' && signItem.el) {
|
||||
const el = signItem.el;
|
||||
const data = signItem.data;
|
||||
|
||||
if (this.readonly || (data.responsible > 0 && data.responsible !== this.currentRole)) {
|
||||
return;
|
||||
}
|
||||
// Now find all image elements and apply our custom observer
|
||||
if (this.signItems && this.signItems[page]) {
|
||||
for (const id in this.signItems[page]) {
|
||||
const item = this.signItems[page][id];
|
||||
if (item && item.data && item.data.type === 'image' && item.el && !processedItems.has(item.el)) {
|
||||
processedItems.add(item.el);
|
||||
setupLazyObserver(item.el);
|
||||
|
||||
// Add upload logic for active signer
|
||||
if (!this.readonly && item.data.responsible > 0 && item.data.responsible === this.currentRole) {
|
||||
const el = item.el;
|
||||
const data = item.data;
|
||||
el.setAttribute('data-type', 'image');
|
||||
const input = el.querySelector('.o_sign_image_upload_input');
|
||||
if (input && !el._hasImgListener) {
|
||||
@ -218,17 +184,19 @@ patch(SignablePDFIframe.prototype, {
|
||||
el._hasImgListener = true;
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
});
|
||||
|
||||
patch(SignablePDFIframe.prototype, {
|
||||
getSignatureValueFromElement(item) {
|
||||
if (item.data.type === 'image') {
|
||||
return item.el.dataset.value || item.data.value || false;
|
||||
}
|
||||
return super.getSignatureValueFromElement(...arguments);
|
||||
},
|
||||
|
||||
checkSignItemsCompletion() {
|
||||
hookCheckSignItemsCompletion(this);
|
||||
return super.checkSignItemsCompletion(...arguments);
|
||||
}
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user