Merge remote-tracking branch 'origin/qtb'

This commit is contained in:
root
2026-10-12 16:55:48 +08:00
+46 -24
View File
@@ -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);
});
},
});