Solve the problem of the buttons stopping instead of refreshing

This commit is contained in:
Mazen Abdo 2025-05-21 14:42:34 +03:00
parent bf7c6deda6
commit 4df025b433
1 changed files with 63 additions and 47 deletions

View File

@ -1,50 +1,66 @@
odoo.define('to_safe_confirm_button.safe_confirm', function (require) { odoo.define("to_safe_confirm_button.safe_confirm", function (require) {
"use strict"; "use strict";
var Dialog = require('web.Dialog') var Dialog = require("web.Dialog");
var form_controller = require('web.FormController') var form_controller = require("web.FormController");
form_controller.include({ form_controller.include({
_onButtonClicked: function (event) { /**
event.stopPropagation(); * @private
var self = this; * @param {OdooEvent} ev
var def; */
this._disableButtons(); _onButtonClicked: function (ev) {
function saveAndExecuteAction () { // stop the event's propagation as a form controller might have other
return self.saveRecord(self.handle, { // form controllers in its descendants (e.g. in a FormViewDialog)
stayInEdit: true, ev.stopPropagation();
}).then(function () { var self = this;
var record = self.model.get(event.data.record.id); var def;
return self._callButtonAction(attrs, record);
}); this._disableButtons();
}
var attrs = event.data.attrs; function saveAndExecuteAction() {
if (attrs.confirm) { return self
var d = $.Deferred(); .saveRecord(self.handle, {
Dialog.confirm(this, attrs.confirm, { stayInEdit: true,
confirm_callback: saveAndExecuteAction, })
}).on("closed", null, function () { .then(function () {
d.resolve(); // we need to reget the record to make sure we have changes made
}); // by the basic model, such as the new res_id, if the record is
def = d.promise(); // new.
} var record = self.model.get(ev.data.record.id);
else if (attrs.safe_confirm){ return self._callButtonAction(attrs, record);
var d = $.Deferred(); });
Dialog.safeConfirm(this, attrs.safe_confirm, { }
confirm_callback: saveAndExecuteAction, var attrs = ev.data.attrs;
}).on("closed", null, function () { if (attrs.confirm) {
d.resolve(); def = new Promise(function (resolve, reject) {
}); Dialog.confirm(self, attrs.confirm, {
def = d.promise(); confirm_callback: saveAndExecuteAction,
} }).on("closed", null, resolve);
else if (attrs.special === 'cancel') { });
def = this._callButtonAction(attrs, event.data.record); } else if (attrs.safe_confirm) {
} else if (!attrs.special || attrs.special === 'save') { def = new Promise(function (resolve, reject) {
// save the record but don't switch to readonly mode Dialog.safeConfirm(self, attrs.confirm, {
def = saveAndExecuteAction(); confirm_callback: saveAndExecuteAction,
} }).on("closed", null, resolve);
def.then(this._enableButtons.bind(this)); });
}, } else if (attrs.special === "cancel") {
def = this._callButtonAction(attrs, ev.data.record);
} else if (!attrs.special || attrs.special === "save") {
// save the record but don't switch to readonly mode
def = saveAndExecuteAction();
} else {
console.warn("Unhandled button event", ev);
return;
}
// Kind of hack for FormViewDialog: button on footer should trigger the dialog closing
// if the `close` attribute is set
def.then(function () {
self._enableButtons();
if (attrs.close) {
self.trigger_up("close_dialog");
}
}).guardedCatch(this._enableButtons.bind(this));
},
});
}); });
});