|
| 1 | +/** @odoo-module **/ |
| 2 | + |
| 3 | +import {registry} from "@web/core/registry"; |
| 4 | + |
| 5 | +const DESCRIPTION_BLOCK_CLASS = "o_dms_document_preview_description"; |
| 6 | +const DESCRIPTION_TEXT_CLASS = "o_dms_document_preview_description_text"; |
| 7 | + |
| 8 | +function parseFileIdFromPreview(previewEl) { |
| 9 | + const contentLink = previewEl.querySelector( |
| 10 | + 'a[href*="/web/content"][href*="model=dms.file"], a[href*="/web/content?id="]' |
| 11 | + ); |
| 12 | + const href = contentLink?.getAttribute("href") || ""; |
| 13 | + if (href) { |
| 14 | + try { |
| 15 | + const url = new URL(href, window.location.origin); |
| 16 | + const id = url.searchParams.get("id"); |
| 17 | + if (id && /^\d+$/.test(id)) { |
| 18 | + return Number.parseInt(id, 10); |
| 19 | + } |
| 20 | + } catch { |
| 21 | + const match = href.match(/[?&]id=(\d+)/); |
| 22 | + if (match) { |
| 23 | + return Number.parseInt(match[1], 10); |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + const image = previewEl.querySelector('img[src*="/web/image/dms.file/"]'); |
| 29 | + const src = image?.getAttribute("src") || ""; |
| 30 | + const imageMatch = src.match(/\/web\/image\/dms\.file\/(\d+)\//); |
| 31 | + if (imageMatch) { |
| 32 | + return Number.parseInt(imageMatch[1], 10); |
| 33 | + } |
| 34 | + |
| 35 | + return null; |
| 36 | +} |
| 37 | + |
| 38 | +function descriptionBlock(previewEl) { |
| 39 | + return previewEl.querySelector(`:scope > .${DESCRIPTION_BLOCK_CLASS}`); |
| 40 | +} |
| 41 | + |
| 42 | +function removeDescription(previewEl) { |
| 43 | + const existing = descriptionBlock(previewEl); |
| 44 | + if (existing) { |
| 45 | + existing.remove(); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +function renderDescription(previewEl, description) { |
| 50 | + const normalized = (description || "").trim(); |
| 51 | + if (!normalized) { |
| 52 | + removeDescription(previewEl); |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + let block = descriptionBlock(previewEl); |
| 57 | + if (!block) { |
| 58 | + block = document.createElement("div"); |
| 59 | + block.className = DESCRIPTION_BLOCK_CLASS; |
| 60 | + |
| 61 | + const title = document.createElement("div"); |
| 62 | + title.className = "o_dms_document_preview_description_title"; |
| 63 | + title.textContent = "Description"; |
| 64 | + |
| 65 | + const text = document.createElement("div"); |
| 66 | + text.className = DESCRIPTION_TEXT_CLASS; |
| 67 | + |
| 68 | + block.append(title, text); |
| 69 | + |
| 70 | + const previewDirectory = previewEl.querySelector( |
| 71 | + ":scope > .o_preview_directory" |
| 72 | + ); |
| 73 | + if (previewDirectory) { |
| 74 | + previewDirectory.insertAdjacentElement("afterend", block); |
| 75 | + } else { |
| 76 | + previewEl.append(block); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + const textEl = block.querySelector(`.${DESCRIPTION_TEXT_CLASS}`); |
| 81 | + if (textEl) { |
| 82 | + textEl.textContent = normalized; |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +const dmsFileDescriptionPreviewService = { |
| 87 | + dependencies: ["orm"], |
| 88 | + start(env, {orm}) { |
| 89 | + const pending = new Map(); |
| 90 | + const minPassiveRefreshMs = 10000; |
| 91 | + let scheduled = null; |
| 92 | + |
| 93 | + async function readDescription(fileId) { |
| 94 | + // Do not cache completed descriptions here. DMS preview panes can be reused |
| 95 | + // after editing a file in a dialog, and stale cached descriptions are very |
| 96 | + // confusing. We only deduplicate concurrent reads for the same file. |
| 97 | + if (pending.has(fileId)) { |
| 98 | + return pending.get(fileId); |
| 99 | + } |
| 100 | + |
| 101 | + const promise = orm |
| 102 | + .read("dms.file", [fileId], ["description"]) |
| 103 | + .then((records) => { |
| 104 | + pending.delete(fileId); |
| 105 | + return records?.[0]?.description || ""; |
| 106 | + }) |
| 107 | + .catch((error) => { |
| 108 | + pending.delete(fileId); |
| 109 | + // Keep this non-fatal. The preview pane should still work even if |
| 110 | + // permissions or transient DMS state prevent reading the description. |
| 111 | + console.warn( |
| 112 | + "Unable to load DMS file description for preview panel", |
| 113 | + fileId, |
| 114 | + error |
| 115 | + ); |
| 116 | + return ""; |
| 117 | + }); |
| 118 | + |
| 119 | + pending.set(fileId, promise); |
| 120 | + return promise; |
| 121 | + } |
| 122 | + |
| 123 | + async function updatePreview(previewEl, {force = false} = {}) { |
| 124 | + const fileId = parseFileIdFromPreview(previewEl); |
| 125 | + if (!fileId) { |
| 126 | + removeDescription(previewEl); |
| 127 | + delete previewEl.dataset.dmsFileDescriptionId; |
| 128 | + delete previewEl.dataset.dmsFileDescriptionCheckedAt; |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + const now = Date.now(); |
| 133 | + const idKey = String(fileId); |
| 134 | + const previousId = previewEl.dataset.dmsFileDescriptionId; |
| 135 | + const lastChecked = Number.parseInt( |
| 136 | + previewEl.dataset.dmsFileDescriptionCheckedAt || "0", |
| 137 | + 10 |
| 138 | + ); |
| 139 | + |
| 140 | + // MutationObserver fires again when this service inserts/updates the block. |
| 141 | + // Avoid repeatedly reading the same preview just because our own DOM changed. |
| 142 | + if ( |
| 143 | + !force && |
| 144 | + previousId === idKey && |
| 145 | + descriptionBlock(previewEl) && |
| 146 | + now - lastChecked < minPassiveRefreshMs |
| 147 | + ) { |
| 148 | + return; |
| 149 | + } |
| 150 | + |
| 151 | + previewEl.dataset.dmsFileDescriptionId = idKey; |
| 152 | + previewEl.dataset.dmsFileDescriptionCheckedAt = String(now); |
| 153 | + |
| 154 | + const description = await readDescription(fileId); |
| 155 | + // The preview may have changed while the RPC was pending. |
| 156 | + if (parseFileIdFromPreview(previewEl) === fileId) { |
| 157 | + renderDescription(previewEl, description); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + function updateAllPreviews(options = {}) { |
| 162 | + for (const previewEl of document.querySelectorAll( |
| 163 | + ".dms_document_preview" |
| 164 | + )) { |
| 165 | + updatePreview(previewEl, options); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + function schedulePassiveUpdate() { |
| 170 | + if (scheduled) { |
| 171 | + window.clearTimeout(scheduled); |
| 172 | + } |
| 173 | + scheduled = window.setTimeout(() => { |
| 174 | + scheduled = null; |
| 175 | + updateAllPreviews({force: false}); |
| 176 | + }, 80); |
| 177 | + } |
| 178 | + |
| 179 | + function scheduleForcedRefreshes() { |
| 180 | + // A form save may still be in-flight when the click happens. Refresh a few |
| 181 | + // times after likely save/close events so the preview catches the new |
| 182 | + // description without requiring a full browser refresh. |
| 183 | + for (const delay of [250, 900, 1800]) { |
| 184 | + window.setTimeout(() => updateAllPreviews({force: true}), delay); |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + const observer = new MutationObserver(schedulePassiveUpdate); |
| 189 | + observer.observe(document.body, { |
| 190 | + childList: true, |
| 191 | + subtree: true, |
| 192 | + attributes: true, |
| 193 | + attributeFilter: ["src", "href", "class"], |
| 194 | + }); |
| 195 | + |
| 196 | + document.body.addEventListener( |
| 197 | + "click", |
| 198 | + (event) => { |
| 199 | + schedulePassiveUpdate(); |
| 200 | + // Refresh after likely file-detail saves or dialog confirm actions. |
| 201 | + if ( |
| 202 | + event.target.closest( |
| 203 | + ".o_form_button_save, .modal-footer .btn-primary, .o_dialog .btn-primary" |
| 204 | + ) |
| 205 | + ) { |
| 206 | + scheduleForcedRefreshes(); |
| 207 | + } |
| 208 | + }, |
| 209 | + true |
| 210 | + ); |
| 211 | + window.addEventListener("focus", scheduleForcedRefreshes); |
| 212 | + |
| 213 | + schedulePassiveUpdate(); |
| 214 | + |
| 215 | + return { |
| 216 | + update() { |
| 217 | + updateAllPreviews({force: true}); |
| 218 | + }, |
| 219 | + }; |
| 220 | + }, |
| 221 | +}; |
| 222 | + |
| 223 | +registry |
| 224 | + .category("services") |
| 225 | + .add( |
| 226 | + "dms_field_file_description_preview.preview_panel", |
| 227 | + dmsFileDescriptionPreviewService |
| 228 | + ); |
0 commit comments