From fea48a262333832b5197e0e9d4e81a1e475f29b9 Mon Sep 17 00:00:00 2001 From: qitangbo Date: Mon, 12 Oct 2026 16:53:12 +0800 Subject: [PATCH] =?UTF-8?q?fix(college):=20=E5=85=B3=E4=BA=8E=E9=A1=B5?= =?UTF-8?q?=E7=BC=96=E8=BE=91=E6=8C=89=E9=92=AE=E5=AF=B9=E6=89=80=E6=9C=89?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E9=9A=90=E8=97=8F=EF=BC=88MutationObserver?= =?UTF-8?q?=20=E5=85=9C=E5=BA=95=E7=9B=91=E5=90=AC=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- college/static/src/js/hide_about_edit.js | 70 ++++++++++++++++-------- 1 file changed, 46 insertions(+), 24 deletions(-) diff --git a/college/static/src/js/hide_about_edit.js b/college/static/src/js/hide_about_edit.js index f1c0f9b..954e5c4 100644 --- a/college/static/src/js/hide_about_edit.js +++ b/college/static/src/js/hide_about_edit.js @@ -1,22 +1,48 @@ /** * 隐藏「关于页」(yuthon.about.page) 表单右上角的「编辑」按钮。 * - * 原理:form 视图虽已写 edit="false",但教师/管理员组对该模型有写权限时, - * Odoo 18 控制面板仍会渲染「编辑」按钮。CSS 选择器无法稳定命中控制面板 - * (它与 .o_form_view 是兄弟节点,且资产缓存/浏览器兼容性容易失效), - * 因此通过 patch FormController,在当前模型为 yuthon.about.page 时, - * 在 mounted/patched 生命周期里把控制面板中的编辑按钮隐藏掉。 + * 原理:form 视图已写 edit="false",Odoo 18 控制面板本就不该渲染编辑按钮; + * 但为保险(防止某些角色/动作仍带出编辑入口),这里在 FormController 上 patch: + * 当模型为 yuthon.about.page 时,用 MutationObserver 持续监听控制面板, + * 一旦编辑按钮(无论何时、以何种形式出现)被渲染,立刻隐藏。 * * 注意:本文件改动需要「升级模块」才会生效(-d <库名> -u college), - * 并强刷浏览器 (Ctrl+Shift+R)。 + * 并强刷浏览器 (Ctrl+Shift+R)。仅 git pull + 重启不会重建前端资源包。 */ /** @odoo-module **/ import { patch } from "@web/core/utils/patch"; import { FormController } from "@web/views/form/form_controller"; -import { onMounted, onPatched } from "@odoo/owl"; +import { onMounted, onWillUnmount } from "@odoo/owl"; const ABOUT_MODEL = "yuthon.about.page"; +// 判定一个按钮是否为「编辑」按钮(兼容不同渲染形态) +function isEditButton(btn) { + if (!btn || btn.tagName !== "BUTTON") { + return false; + } + if (btn.classList.contains("o_form_button_edit")) { + return true; + } + if (btn.getAttribute("data-hotkey") === "e") { + return true; + } + const text = (btn.textContent || "").trim(); + if (text === "编辑" || text.toLowerCase() === "edit") { + return true; + } + return false; +} + +// 在控制面板内隐藏所有编辑按钮 +function hideAboutEditButtons(root) { + (root || document).querySelectorAll(".o_control_panel button").forEach((btn) => { + if (isEditButton(btn)) { + btn.style.display = "none"; + } + }); +} + patch(FormController.prototype, { setup() { super.setup(); @@ -24,23 +50,19 @@ patch(FormController.prototype, { return; } - const hideEditButton = () => { - // Odoo 18 控制面板中「编辑」按钮的常见形态: - // 1) class="o_form_button_edit" - // 2) 快捷键 data-hotkey="e" 的主按钮 - const selectors = [ - ".o_control_panel .o_form_button_edit", - ".o_control_panel button[data-hotkey='e']", - ]; - for (const selector of selectors) { - document.querySelectorAll(selector).forEach((btn) => { - btn.style.display = "none"; - }); + let observer = null; + onMounted(() => { + hideAboutEditButtons(document); + // 控制面板按钮可能异步/重渲染后才出现,持续监听最稳妥 + const cp = document.querySelector(".o_control_panel"); + const target = cp || document.body; + observer = new MutationObserver(() => hideAboutEditButtons(target)); + observer.observe(target, { childList: true, subtree: true }); + }); + onWillUnmount(() => { + if (observer) { + observer.disconnect(); } - }; - - // 首次渲染及后续 patch 都兜底隐藏,避免异步渲染导致按钮闪现 - onMounted(hideEditButton); - onPatched(hideEditButton); + }); }, });