fix the OWL error at Approval app

This commit is contained in:
Suherdy Yacob 2026-01-09 15:28:25 +07:00
parent 0739fd2a5f
commit 61439c2a1d

View File

@ -1,6 +1,6 @@
/** @odoo-module **/ /** @odoo-module **/
console.log('Loading safe decimal observer...'); console.log('Loading ultra-safe decimal observer...');
// Function to safely apply decimal styling to any element // Function to safely apply decimal styling to any element
function safelyApplyDecimalStyling(element) { function safelyApplyDecimalStyling(element) {
@ -12,6 +12,19 @@ function safelyApplyDecimalStyling(element) {
element.hasAttribute('data-decimal-processed') || element.hasAttribute('data-decimal-processed') ||
element.hasAttribute('data-owl-updating')) return false; element.hasAttribute('data-owl-updating')) return false;
// CRITICAL: Skip any editable elements or elements that might become editable
if (element.isContentEditable ||
element.closest('.o_field_widget:not(.o_readonly_modifier)') ||
element.closest('[contenteditable="true"]') ||
element.closest('input') ||
element.closest('textarea') ||
element.closest('.o_input') ||
element.closest('.o_field_float:not(.o_readonly_modifier)') ||
element.closest('.o_field_monetary:not(.o_readonly_modifier)') ||
element.hasAttribute('contenteditable')) {
return false;
}
try { try {
const text = element.textContent.trim(); const text = element.textContent.trim();
if (!text) return false; if (!text) return false;
@ -34,10 +47,12 @@ function safelyApplyDecimalStyling(element) {
if (match) { if (match) {
const [, prefix, integerPart, decimalPart, suffix] = match; const [, prefix, integerPart, decimalPart, suffix] = match;
console.log('Safe Decimal Observer: Styling', text); console.log('Ultra-Safe Decimal Observer: Styling decimal number', text);
// Double-check the element hasn't changed before updating // Triple-check the element is safe to modify
if (element.textContent.trim() === text && document.contains(element)) { if (element.textContent.trim() === text &&
document.contains(element) &&
!element.closest('.o_field_widget:not(.o_readonly_modifier)')) {
const newHTML = `${prefix || ''}${integerPart}<span class="o_decimal">.${decimalPart}</span>${suffix || ''}`; const newHTML = `${prefix || ''}${integerPart}<span class="o_decimal">.${decimalPart}</span>${suffix || ''}`;
element.innerHTML = newHTML; element.innerHTML = newHTML;
return true; return true;
@ -45,17 +60,20 @@ function safelyApplyDecimalStyling(element) {
} }
} }
// Handle whole numbers in quantity contexts (be more selective) // Handle whole numbers in quantity contexts (be even more selective)
// Only add decimals to numbers that are clearly quantities, not prices with thousands separators // Only add decimals to numbers that are clearly quantities in readonly contexts
if (text.match(/^\d+$/) && text.length > 0 && text.length <= 4 && ( if (text.match(/^\d+$/) && text.length > 0 && text.length <= 4 &&
element.closest('.o_readonly_modifier') && (
element.closest('[name="quantity"]') || element.closest('[name="quantity"]') ||
element.closest('[name="quantity_done"]') || element.closest('[name="quantity_done"]') ||
element.closest('[name="reserved_availability"]') element.closest('[name="reserved_availability"]')
)) { )) {
console.log('Safe Decimal Observer: Adding decimals to quantity', text); console.log('Ultra-Safe Decimal Observer: Adding decimals to readonly quantity', text);
// Double-check before updating // Triple-check before updating
if (element.textContent.trim() === text && document.contains(element)) { if (element.textContent.trim() === text &&
document.contains(element) &&
element.closest('.o_readonly_modifier')) {
element.innerHTML = `${text}<span class="o_decimal">.000</span>`; element.innerHTML = `${text}<span class="o_decimal">.000</span>`;
return true; return true;
} }
@ -64,12 +82,12 @@ function safelyApplyDecimalStyling(element) {
// Do NOT process numbers with commas that don't have decimal points // Do NOT process numbers with commas that don't have decimal points
// These are likely thousands separators (e.g., 8,500 should stay as 8,500) // These are likely thousands separators (e.g., 8,500 should stay as 8,500)
if (text.match(/^\d{1,3}(?:,\d{3})+$/) && !text.includes('.')) { if (text.match(/^\d{1,3}(?:,\d{3})+$/) && !text.includes('.')) {
console.log('Safe Decimal Observer: Skipping thousands separator number', text); console.log('Ultra-Safe Decimal Observer: Skipping thousands separator number', text);
return false; return false;
} }
} catch (error) { } catch (error) {
console.warn('Safe decimal styling error (non-critical):', error); console.warn('Ultra-safe decimal styling error (non-critical):', error);
// Remove the processing flag if there was an error // Remove the processing flag if there was an error
element.removeAttribute('data-decimal-processed'); element.removeAttribute('data-decimal-processed');
} }
@ -77,79 +95,80 @@ function safelyApplyDecimalStyling(element) {
return false; return false;
} }
// Safe processing of containers // Ultra-safe processing of containers
function safelyProcessContainer(container) { function ultraSafelyProcessContainer(container) {
if (!container || !document.contains(container)) return; if (!container || !document.contains(container)) return;
try { try {
// Find all text-containing elements, but be more selective // Only process elements in readonly contexts to avoid editable field conflicts
const elements = container.querySelectorAll('td:not([data-decimal-processed]), span:not([data-decimal-processed]), .o_field_monetary:not([data-decimal-processed]), .o_field_float:not([data-decimal-processed])'); const elements = container.querySelectorAll('.o_readonly_modifier td:not([data-decimal-processed]), .o_readonly_modifier span:not([data-decimal-processed]), .o_field_monetary.o_readonly_modifier:not([data-decimal-processed]), .o_field_float.o_readonly_modifier:not([data-decimal-processed])');
elements.forEach(element => { elements.forEach(element => {
// Only process leaf elements and skip if Owl is managing them // Only process leaf elements in readonly contexts
if ((element.children.length === 0 || if ((element.children.length === 0 ||
(element.children.length === 1 && element.children[0].tagName === 'SPAN')) && (element.children.length === 1 && element.children[0].tagName === 'SPAN')) &&
!element.hasAttribute('data-owl-updating')) { !element.hasAttribute('data-owl-updating') &&
!element.closest('.o_field_widget:not(.o_readonly_modifier)')) {
safelyApplyDecimalStyling(element); safelyApplyDecimalStyling(element);
} }
}); });
} catch (error) { } catch (error) {
console.warn('Safe container processing error (non-critical):', error); console.warn('Ultra-safe container processing error (non-critical):', error);
} }
} }
// More conservative observer // Ultra-conservative observer with longer debounce
const safeObserver = new MutationObserver((mutations) => { const ultraSafeObserver = new MutationObserver((mutations) => {
// Debounce the processing to avoid conflicts // Longer debounce to avoid conflicts with user interactions
clearTimeout(safeObserver.timeout); clearTimeout(ultraSafeObserver.timeout);
safeObserver.timeout = setTimeout(() => { ultraSafeObserver.timeout = setTimeout(() => {
try { try {
mutations.forEach((mutation) => { mutations.forEach((mutation) => {
if (mutation.type === 'childList') { if (mutation.type === 'childList') {
mutation.addedNodes.forEach((node) => { mutation.addedNodes.forEach((node) => {
if (node.nodeType === Node.ELEMENT_NODE && document.contains(node)) { if (node.nodeType === Node.ELEMENT_NODE && document.contains(node)) {
// Use requestAnimationFrame to ensure we run after Owl updates // Use longer delay to ensure Owl has finished all updates
requestAnimationFrame(() => { setTimeout(() => {
safelyProcessContainer(node); ultraSafelyProcessContainer(node);
}); }, 500); // Much longer delay
} }
}); });
} }
}); });
} catch (error) { } catch (error) {
console.warn('Safe observer error (non-critical):', error); console.warn('Ultra-safe observer error (non-critical):', error);
} }
}, 200); // Debounce for 200ms }, 500); // Longer debounce for 500ms
}); });
// Start observing safely // Start ultra-safe observing
function startSafeObserver() { function startUltraSafeObserver() {
console.log('Safe Decimal Observer: Starting conservative observation'); console.log('Ultra-Safe Decimal Observer: Starting ultra-conservative observation');
// Process existing content safely // Process existing content safely with delay
requestAnimationFrame(() => { setTimeout(() => {
safelyProcessContainer(document.body); ultraSafelyProcessContainer(document.body);
}); }, 1000); // Initial delay to let everything load
// Start observing for new content with reduced frequency // Start observing for new content with very reduced frequency
safeObserver.observe(document.body, { ultraSafeObserver.observe(document.body, {
childList: true, childList: true,
subtree: true subtree: true
}); });
// Periodic processing with longer intervals to reduce conflicts // Much less frequent periodic processing to reduce conflicts
setInterval(() => { setInterval(() => {
requestAnimationFrame(() => { ultraSafelyProcessContainer(document.body);
safelyProcessContainer(document.body); }, 10000); // Every 10 seconds instead of 5
});
}, 5000); // Every 5 seconds instead of 2
} }
// Start safely // Start ultra-safely
if (document.readyState === 'loading') { if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', startSafeObserver); document.addEventListener('DOMContentLoaded', () => {
setTimeout(startUltraSafeObserver, 2000); // Extra delay
});
} else { } else {
startSafeObserver(); setTimeout(startUltraSafeObserver, 2000); // Extra delay
} }
console.log('Safe decimal observer loaded'); console.log('Ultra-safe decimal observer loaded');