Merge pull request #3270 from expsa/zen_fix_safe_confirm

Solve the problem of the buttons stopping instead of refreshing
This commit is contained in:
mazenmuhamad 2025-05-21 15:03:09 +03:00 committed by GitHub
commit c7ac7a0d4f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
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);
});
}
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));
},
});
});
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));
},
});
});