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) {
"use strict";
odoo.define("to_safe_confirm_button.safe_confirm", function (require) {
"use strict";
var Dialog = require('web.Dialog')
var form_controller = require('web.FormController')
var Dialog = require("web.Dialog");
var form_controller = require("web.FormController");
form_controller.include({
_onButtonClicked: function (event) {
event.stopPropagation();
var self = this;
var def;
this._disableButtons();
function saveAndExecuteAction () {
return self.saveRecord(self.handle, {
stayInEdit: true,
}).then(function () {
var record = self.model.get(event.data.record.id);
return self._callButtonAction(attrs, record);
});
}
var attrs = event.data.attrs;
if (attrs.confirm) {
var d = $.Deferred();
Dialog.confirm(this, attrs.confirm, {
confirm_callback: saveAndExecuteAction,
}).on("closed", null, function () {
d.resolve();
});
def = d.promise();
}
else if (attrs.safe_confirm){
var d = $.Deferred();
Dialog.safeConfirm(this, attrs.safe_confirm, {
confirm_callback: saveAndExecuteAction,
}).on("closed", null, function () {
d.resolve();
});
def = d.promise();
}
else if (attrs.special === 'cancel') {
def = this._callButtonAction(attrs, event.data.record);
} else if (!attrs.special || attrs.special === 'save') {
// save the record but don't switch to readonly mode
def = saveAndExecuteAction();
}
def.then(this._enableButtons.bind(this));
},
});
});
form_controller.include({
/**
* @private
* @param {OdooEvent} ev
*/
_onButtonClicked: function (ev) {
// stop the event's propagation as a form controller might have other
// form controllers in its descendants (e.g. in a FormViewDialog)
ev.stopPropagation();
var self = this;
var def;
this._disableButtons();
function saveAndExecuteAction() {
return self
.saveRecord(self.handle, {
stayInEdit: true,
})
.then(function () {
// 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
// new.
var record = self.model.get(ev.data.record.id);
return self._callButtonAction(attrs, record);
});
}
var attrs = ev.data.attrs;
if (attrs.confirm) {
def = new Promise(function (resolve, reject) {
Dialog.confirm(self, attrs.confirm, {
confirm_callback: saveAndExecuteAction,
}).on("closed", null, resolve);
});
} else if (attrs.safe_confirm) {
def = new Promise(function (resolve, reject) {
Dialog.safeConfirm(self, attrs.confirm, {
confirm_callback: saveAndExecuteAction,
}).on("closed", null, resolve);
});
} 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));
},
});
});