From ea9b714e6c8f250534cfa1b404b8eeb2ae216e01 Mon Sep 17 00:00:00 2001 From: William Braeckman Date: Wed, 12 Mar 2025 09:52:24 +0100 Subject: [PATCH] [IMP] runbot: prevent saving on switching tab Auto save mechanism when switching tab is unwanted in runbot, most of the time we will use other tabs to get the data necessary for the record we are modifying/creating and we don't care about saving yet. We also prevent closing the window if the record is dirty. Auto save in pager and context switch is still kept. --- runbot/__manifest__.py | 1 + runbot/static/src/js/views/form_controller.js | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 runbot/static/src/js/views/form_controller.js diff --git a/runbot/__manifest__.py b/runbot/__manifest__.py index 25a07b90..5085df56 100644 --- a/runbot/__manifest__.py +++ b/runbot/__manifest__.py @@ -64,6 +64,7 @@ 'assets': { 'web.assets_backend': [ 'runbot/static/src/libs/diff_match_patch/diff_match_patch.js', + 'runbot/static/src/js/views/**/*', 'runbot/static/src/js/fields/*', ], 'runbot.assets_frontend': [ diff --git a/runbot/static/src/js/views/form_controller.js b/runbot/static/src/js/views/form_controller.js new file mode 100644 index 00000000..f7a1ea72 --- /dev/null +++ b/runbot/static/src/js/views/form_controller.js @@ -0,0 +1,19 @@ +/** @odoo-module **/ + +import { FormController } from '@web/views/form/form_controller'; +import { patch } from '@web/core/utils/patch'; + + +patch(FormController.prototype, { + // Prevent saving on tab switching + beforeVisibilityChange: () => {}, + // Prevent closing page with dirty fields + async beforeUnload(ev) { + if (await this.model.root.isDirty()) { + ev.preventDefault(); + ev.returnValue = 'Unsaved changes'; + } else { + super.beforeUnload(ev); + } + } +})