fix: Enhance iframe communication stability with a comprehensive bridge restoration and refine image field lazy loading and upload event handling.
This commit is contained in:
parent
799fc9717a
commit
db3635d237
@ -4,7 +4,40 @@ import { patch } from "@web/core/utils/patch";
|
|||||||
import { PDFIframe } from "@sign/components/sign_request/PDF_iframe";
|
import { PDFIframe } from "@sign/components/sign_request/PDF_iframe";
|
||||||
import { SignablePDFIframe } from "@sign/components/sign_request/signable_PDF_iframe";
|
import { SignablePDFIframe } from "@sign/components/sign_request/signable_PDF_iframe";
|
||||||
|
|
||||||
console.log("Sign Image Field: Initializing universal stability patches...");
|
console.log("Sign Image Field: Initializing bulletproof stability patches...");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bulletproof Bridge Restoration
|
||||||
|
* Re-connects Odoo parent to PDF viewer iframe on multiple levels to ensure viewer.js
|
||||||
|
* can always find the refreshSignItemsForPage method.
|
||||||
|
*/
|
||||||
|
const restoreBridge = (instance) => {
|
||||||
|
if (!instance || !instance.root || !instance.root.defaultView) return;
|
||||||
|
|
||||||
|
const iwin = instance.root.defaultView;
|
||||||
|
const iframe = iwin.frameElement;
|
||||||
|
|
||||||
|
// 1. Parent-side element (Standard Odoo mechanism)
|
||||||
|
if (iframe && !iframe.odoo_iframe_instance) {
|
||||||
|
console.log("Sign Image Field: Restoring bridge on iframe element");
|
||||||
|
iframe.odoo_iframe_instance = instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Iframe-side global (In-frame customization)
|
||||||
|
if (!iwin.odoo_iframe_instance) {
|
||||||
|
iwin.odoo_iframe_instance = instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. Document-side global (In-frame customization fallback)
|
||||||
|
if (iwin.document && !iwin.document.odoo_iframe_instance) {
|
||||||
|
iwin.document.odoo_iframe_instance = instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. Attach directly to PDFViewerApplication if ready
|
||||||
|
if (iwin.PDFViewerApplication && !iwin.PDFViewerApplication.odoo_iframe_instance) {
|
||||||
|
iwin.PDFViewerApplication.odoo_iframe_instance = instance;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Shared utility for lazy loading image fields.
|
* Shared utility for lazy loading image fields.
|
||||||
@ -23,20 +56,14 @@ const triggerLazyLoad = (el) => {
|
|||||||
lazyImg.style.opacity = 1;
|
lazyImg.style.opacity = 1;
|
||||||
};
|
};
|
||||||
|
|
||||||
// If already loaded or assigned
|
|
||||||
if (lazyImg.src && !lazyImg.src.startsWith('data:image/gif') && lazyImg.src !== '') {
|
if (lazyImg.src && !lazyImg.src.startsWith('data:image/gif') && lazyImg.src !== '') {
|
||||||
finalize();
|
finalize();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Assign BEFORE src to avoid race with cache
|
|
||||||
lazyImg.onload = finalize;
|
lazyImg.onload = finalize;
|
||||||
lazyImg.src = dataSrc;
|
lazyImg.src = dataSrc;
|
||||||
|
if (lazyImg.complete) finalize();
|
||||||
// Immediate check if already complete (from cache)
|
|
||||||
if (lazyImg.complete) {
|
|
||||||
finalize();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -45,8 +72,7 @@ const triggerLazyLoad = (el) => {
|
|||||||
const setupLazyObserver = (el) => {
|
const setupLazyObserver = (el) => {
|
||||||
if (!el) return;
|
if (!el) return;
|
||||||
|
|
||||||
// Safety fallback: Load after 1.5s regardless of intersection
|
// Safety fallback: Force load after 1.5s
|
||||||
// This is vital for remote connections where observer triggers might be erratic
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
if (el.classList.contains('o_sign_image_loading_container')) {
|
if (el.classList.contains('o_sign_image_loading_container')) {
|
||||||
triggerLazyLoad(el);
|
triggerLazyLoad(el);
|
||||||
@ -68,35 +94,26 @@ const setupLazyObserver = (el) => {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Restore the Odoo-Iframe bridge if it's missing (Odoo 19 systemic issue)
|
|
||||||
* This fixes the "refreshSignItemsForPage is not a function" error in viewer.js
|
|
||||||
*/
|
|
||||||
const restoreBridge = (instance) => {
|
|
||||||
if (instance.root && instance.root.defaultView && instance.root.defaultView.frameElement) {
|
|
||||||
const iframe = instance.root.defaultView.frameElement;
|
|
||||||
if (!iframe.odoo_iframe_instance) {
|
|
||||||
console.log("Sign Image Field: Restoring odoo_iframe_instance bridge on iframe");
|
|
||||||
iframe.odoo_iframe_instance = instance;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// 1. Patch PDFIframe (ROOT)
|
// 1. Patch PDFIframe (ROOT)
|
||||||
patch(PDFIframe.prototype, {
|
patch(PDFIframe.prototype, {
|
||||||
enableCustom(signItem) {
|
enableCustom(signItem) {
|
||||||
// Restore communication bridge immediately to prevent viewer.js crashes
|
|
||||||
restoreBridge(this);
|
restoreBridge(this);
|
||||||
|
|
||||||
// Defensive super call to preserve original Odoo behavior and other patches
|
// Call original implementation FIRST
|
||||||
if (typeof super.enableCustom === 'function') {
|
if (typeof super.enableCustom === 'function') {
|
||||||
super.enableCustom(...arguments);
|
super.enableCustom(...arguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
// OUR LOGIC: Setup lazy loading if it's an image
|
// Setup lazy loading for images
|
||||||
if (signItem && signItem.data && signItem.data.type === 'image' && signItem.el) {
|
if (signItem && signItem.data && signItem.data.type === 'image' && signItem.el) {
|
||||||
setupLazyObserver(signItem.el);
|
setupLazyObserver(signItem.el);
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
refreshSignItems(page = false) {
|
||||||
|
// Ensure bridge is active during page rendering events
|
||||||
|
restoreBridge(this);
|
||||||
|
return super.refreshSignItems(...arguments);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -105,34 +122,30 @@ patch(SignablePDFIframe.prototype, {
|
|||||||
enableCustom(signItem) {
|
enableCustom(signItem) {
|
||||||
restoreBridge(this);
|
restoreBridge(this);
|
||||||
|
|
||||||
// Setup observer early to ensure it runs for all documents/roles
|
// Setup image observers early for ALL users
|
||||||
if (signItem && signItem.data && signItem.data.type === 'image' && signItem.el) {
|
if (signItem && signItem.data && signItem.data.type === 'image' && signItem.el) {
|
||||||
setupLazyObserver(signItem.el);
|
setupLazyObserver(signItem.el);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Call original implementation (which handles text/signature etc)
|
// Call original implementation to populate Nama, NIK, etc.
|
||||||
super.enableCustom(...arguments);
|
super.enableCustom(...arguments);
|
||||||
|
|
||||||
|
// Add upload logic for active signer
|
||||||
if (signItem && signItem.data && signItem.data.type === 'image' && signItem.el) {
|
if (signItem && signItem.data && signItem.data.type === 'image' && signItem.el) {
|
||||||
const el = signItem.el;
|
const el = signItem.el;
|
||||||
const data = signItem.data;
|
const data = signItem.data;
|
||||||
|
|
||||||
// Strictly check responsibility for UPLOAD logic
|
|
||||||
if (this.readonly || (data.responsible > 0 && data.responsible !== this.currentRole)) {
|
if (this.readonly || (data.responsible > 0 && data.responsible !== this.currentRole)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
el.setAttribute('data-type', 'image');
|
el.setAttribute('data-type', 'image');
|
||||||
const input = el.querySelector('.o_sign_image_upload_input');
|
const input = el.querySelector('.o_sign_image_upload_input');
|
||||||
if (input) {
|
if (input && !el._hasImgListener) {
|
||||||
// Ensure we don't double-attach if enableCustom is called multiple times
|
el.addEventListener('click', (e) => {
|
||||||
if (!el._hasImgListener) {
|
if (e.target !== input) input.click();
|
||||||
el.addEventListener('click', (e) => {
|
});
|
||||||
if (e.target !== input) input.click();
|
|
||||||
});
|
|
||||||
el._hasImgListener = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
input.addEventListener('change', (e) => {
|
input.addEventListener('change', (e) => {
|
||||||
const file = e.target.files[0];
|
const file = e.target.files[0];
|
||||||
if (!file) return;
|
if (!file) return;
|
||||||
@ -187,6 +200,7 @@ patch(SignablePDFIframe.prototype, {
|
|||||||
};
|
};
|
||||||
reader.readAsDataURL(file);
|
reader.readAsDataURL(file);
|
||||||
});
|
});
|
||||||
|
el._hasImgListener = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user