diff --git a/college/__manifest__.py b/college/__manifest__.py index c7ced21..5586d0f 100644 --- a/college/__manifest__.py +++ b/college/__manifest__.py @@ -59,6 +59,7 @@ 'web.assets_backend': [ 'college/static/src/scss/college_theme.scss', 'college/static/src/scss/hide_about_edit.scss', + 'college/static/src/js/hide_about_edit.js', 'college/static/src/js/hide_company_menu.js', 'college/static/src/js/hide_user_menu_items.js', 'college/static/src/js/hide_activity_menu.js', diff --git a/college/static/src/js/hide_about_edit.js b/college/static/src/js/hide_about_edit.js new file mode 100644 index 0000000..f1c0f9b --- /dev/null +++ b/college/static/src/js/hide_about_edit.js @@ -0,0 +1,46 @@ +/** + * 隐藏「关于页」(yuthon.about.page) 表单右上角的「编辑」按钮。 + * + * 原理:form 视图虽已写 edit="false",但教师/管理员组对该模型有写权限时, + * Odoo 18 控制面板仍会渲染「编辑」按钮。CSS 选择器无法稳定命中控制面板 + * (它与 .o_form_view 是兄弟节点,且资产缓存/浏览器兼容性容易失效), + * 因此通过 patch FormController,在当前模型为 yuthon.about.page 时, + * 在 mounted/patched 生命周期里把控制面板中的编辑按钮隐藏掉。 + * + * 注意:本文件改动需要「升级模块」才会生效(-d <库名> -u college), + * 并强刷浏览器 (Ctrl+Shift+R)。 + */ +/** @odoo-module **/ +import { patch } from "@web/core/utils/patch"; +import { FormController } from "@web/views/form/form_controller"; +import { onMounted, onPatched } from "@odoo/owl"; + +const ABOUT_MODEL = "yuthon.about.page"; + +patch(FormController.prototype, { + setup() { + super.setup(); + if (this.props.resModel !== ABOUT_MODEL) { + 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"; + }); + } + }; + + // 首次渲染及后续 patch 都兜底隐藏,避免异步渲染导致按钮闪现 + onMounted(hideEditButton); + onPatched(hideEditButton); + }, +}); diff --git a/college/static/src/scss/hide_about_edit.scss b/college/static/src/scss/hide_about_edit.scss index 4032cbb..732e9e2 100644 --- a/college/static/src/scss/hide_about_edit.scss +++ b/college/static/src/scss/hide_about_edit.scss @@ -1,7 +1,7 @@ // 关于页(yuthon.about.page)为只读展示页: -// form 视图已设 edit="false",但教师组有该模型写权限时控制面板仍会渲染「编辑」按钮, -// 这里用 CSS :has() 精准隐藏该视图控制面板里的编辑按钮,不影响其他表单。 -.o_view_controller:has(.o_form_view.o_about_full_form) .o_control_panel .o_form_button_edit, -.o_view_controller:has(.o_form_view.o_about_full_form) .o_control_panel button[data-hotkey="e"] { +// form 视图已设 edit="false",但教师组有该模型写权限时控制面板仍会渲染「编辑」按钮。 +// 这里用 CSS 做兜底隐藏(若按钮恰好在 form 内),控制面板中的按钮由同名的 JS patch 处理。 +.o_form_view.o_about_full_form .o_form_button_edit, +.o_form_view.o_about_full_form button[data-hotkey="e"] { display: none !important; }