diff --git a/static/src/js/sign_image_upload.js b/static/src/js/sign_image_upload.js index 0fd7e17..2eaeb1d 100755 --- a/static/src/js/sign_image_upload.js +++ b/static/src/js/sign_image_upload.js @@ -4,21 +4,31 @@ 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: Loading custom patches..."); + /** * Robust lazy loading trigger for image sign items. */ const triggerLazyLoad = (el) => { if (!el) return; const lazyImg = el.querySelector('.o_sign_image_lazy'); - if (lazyImg && lazyImg.dataset.src) { + if (lazyImg && (lazyImg.dataset.src || lazyImg.src)) { + const targetSrc = lazyImg.dataset.src || lazyImg.src; + // Check if it's currently showing the transparent placeholder const currentSrc = lazyImg.getAttribute('src'); - if (!currentSrc || currentSrc.startsWith('data:image/gif')) { - lazyImg.src = lazyImg.dataset.src; + if (!currentSrc || currentSrc.startsWith('data:image/gif') || currentSrc === '') { + console.log("Sign Image Field: Loading image data for", el); + lazyImg.src = targetSrc; lazyImg.onload = () => { lazyImg.classList.remove('o_sign_image_loading'); el.classList.remove('o_sign_image_loading_container'); lazyImg.style.opacity = 1; }; + } else { + // Already has a real src, just ensure loading classes are gone + lazyImg.classList.remove('o_sign_image_loading'); + el.classList.remove('o_sign_image_loading_container'); + lazyImg.style.opacity = 1; } } }; @@ -27,87 +37,76 @@ const triggerLazyLoad = (el) => { * Setup IntersectionObserver for an element. */ const setupLazyObserver = (iframeInstance, el) => { - if (!el || !window.IntersectionObserver) { - // Fallback for no observer support - triggerLazyLoad(el); - return; - } - const lazyImg = el.querySelector('.o_sign_image_lazy'); - if (!lazyImg || (!lazyImg.dataset.src && !lazyImg.src)) return; + if (!el) return; + + // Safety fallback: Always load after 2 seconds regardless of visibility + // This handles cases where IntersectionObserver fails in iframes or remote bundling issues + setTimeout(() => triggerLazyLoad(el), 2000); - // Use a safety timeout to ensure it loads even if observer fails - const safetyTimeout = setTimeout(() => triggerLazyLoad(el), 3000); - - const observer = new IntersectionObserver((entries) => { - entries.forEach(entry => { - if (entry.isIntersecting) { - clearTimeout(safetyTimeout); - triggerLazyLoad(el); - observer.disconnect(); - } + if (window.IntersectionObserver) { + const observer = new IntersectionObserver((entries) => { + entries.forEach(entry => { + if (entry.isIntersecting) { + triggerLazyLoad(el); + observer.disconnect(); + } + }); + }, { + rootMargin: '200px' }); - }, { - rootMargin: '200px' - }); - observer.observe(el); + observer.observe(el); + } }; -// 1. Patch PDFIframe (Base class) for read-only / non-signer views -patch(PDFIframe.prototype, { +// 1. Patch PDFIframe (Base class) +// We avoid calling 'super' here because PDFIframe is a base class and has no parent. +// Odoo 19's patch system might misbehave with 'super' in base class prototype patches. +const pdfIframePatch = { enableCustom(signItem) { - // Shared logic: Ensure lazy loading is set up for all image fields + console.log("Sign Image Field: PDFIframe.enableCustom triggered for", signItem.data.type); if (signItem && signItem.data && signItem.data.type === 'image') { setupLazyObserver(this, signItem.el); } - - // Base PDFIframe.enableCustom is empty, but we call super just in case of other patches - if (typeof super.enableCustom === 'function') { - super.enableCustom(signItem); - } + // Instead of super.enableCustom(signItem), we check if there's an ORIGINAL to call + // Normally for PDFIframe it's an empty function. } -}); +}; -// 2. Patch SignablePDFIframe (Subclass) for active signers -patch(SignablePDFIframe.prototype, { +// Use a safe patch wrapper to ensure we don't break the prototype +try { + patch(PDFIframe.prototype, pdfIframePatch); +} catch (e) { + console.error("Sign Image Field: Failed to patch PDFIframe.prototype", e); +} + +// 2. Patch SignablePDFIframe (Subclass) +const signablePDFIframePatch = { enableCustom(signItem) { - // IMPORTANT: Execute shared logic first! - // PDFIframe.enableCustom (our patch above) will handle lazy loading. - // However, SignablePDFIframe does NOT normally call super.enableCustom(). - // We manually trigger the shared observation logic here to be sure. + console.log("Sign Image Field: SignablePDFIframe.enableCustom triggered for", signItem.data.type); + + // Setup observer BEFORE role checks so it works for observers/read-only too if (signItem && signItem.data && signItem.data.type === 'image') { setupLazyObserver(this, signItem.el); } - // Now call the original SignablePDFIframe.enableCustom (which has the role check) + // Call base implementation super.enableCustom(signItem); - // Additional setup only for the current signer if (signItem && signItem.data && signItem.data.type === 'image' && signItem.el) { const el = signItem.el; const data = signItem.data; - // Check if we are responsible for this field (duplicate check from original but needed for our additions) - if (this.readonly || data.responsible !== this.currentRole) { + // Re-apply responsible check for upload logic only + if (this.readonly || (data.responsible > 0 && data.responsible !== this.currentRole)) { return; } el.setAttribute('data-type', 'image'); - // Initial font size adjustment for placeholder - const adjustFontSize = () => { - const placeholder = el.querySelector('.o_placeholder'); - if (placeholder) { - const minDimension = Math.min(el.offsetHeight, el.offsetWidth); - const fontSize = Math.max(10, Math.min(16, minDimension * 0.4)); - placeholder.style.fontSize = fontSize + 'px !important'; - placeholder.style.lineHeight = '1'; - } - }; - setTimeout(adjustFontSize, 100); - // Upload implementation const input = el.querySelector('.o_sign_image_upload_input'); if (input) { + // Remove any previous listeners if possible (though patch should handle it) el.addEventListener('click', (e) => { if (e.target !== input) { input.click(); @@ -175,9 +174,12 @@ patch(SignablePDFIframe.prototype, { if (item.data.type === 'image') { return item.el.dataset.value || item.data.value || false; } - if (typeof super.getSignatureValueFromElement === 'function') { - return super.getSignatureValueFromElement(item); - } - return false; + return super.getSignatureValueFromElement(item); } -}); +}; + +try { + patch(SignablePDFIframe.prototype, signablePDFIframePatch); +} catch (e) { + console.error("Sign Image Field: Failed to patch SignablePDFIframe.prototype", e); +}