confirm_many2one_create: simplify js
This commit is contained in:
parent
d1e97e3448
commit
401818765a
1 changed files with 19 additions and 59 deletions
|
|
@ -1,69 +1,29 @@
|
||||||
/** @odoo-module **/
|
/** @odoo-module **/
|
||||||
|
|
||||||
import {Many2OneField} from "@web/views/fields/many2one/many2one_field";
|
import {Many2OneField, m2oTupleFromData} from "@web/views/fields/many2one/many2one_field";
|
||||||
import {patch} from "@web/core/utils/patch";
|
import {patch} from "@web/core/utils/patch";
|
||||||
import {_t} from "@web/core/l10n/translation";
|
|
||||||
import {Dialog} from "@web/core/dialog/dialog";
|
|
||||||
import {Component, markup} from "@odoo/owl";
|
|
||||||
|
|
||||||
// Create a custom confirmation dialog component
|
|
||||||
class CreateConfirmationDialog extends Component {
|
|
||||||
static template = "web.Many2OneField.CreateConfirmationDialog";
|
|
||||||
static components = {Dialog};
|
|
||||||
|
|
||||||
get title() {
|
|
||||||
return _t("New: %s", this.props.name);
|
|
||||||
}
|
|
||||||
|
|
||||||
get dialogContent() {
|
|
||||||
return markup(
|
|
||||||
_t(
|
|
||||||
"Create <strong>%s</strong> as a new %s?",
|
|
||||||
this.props.value,
|
|
||||||
this.props.name
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
async onCreate() {
|
|
||||||
await this.props.create();
|
|
||||||
this.props.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
patch(Many2OneField.prototype, {
|
patch(Many2OneField.prototype, {
|
||||||
setup() {
|
/**
|
||||||
// Call the original setup method
|
* The original Many2OneField already has a quickCreate method and an openConfirmationDialog method.
|
||||||
super.setup();
|
* The quickCreate method directly updates the record, while openConfirmationDialog shows a dialog
|
||||||
|
* before calling quickCreate.
|
||||||
|
*
|
||||||
|
* We just need to modify the Many2XAutocompleteProps getter to use openConfirmationDialog
|
||||||
|
* instead of quickCreate for the quickCreate property.
|
||||||
|
*/
|
||||||
|
get Many2XAutocompleteProps() {
|
||||||
|
const props = super.Many2XAutocompleteProps;
|
||||||
|
|
||||||
// Store the original quickCreate function
|
// If quickCreate is defined, replace it with openConfirmationDialog
|
||||||
const originalQuickCreate = this.quickCreate;
|
if (props.quickCreate && this.openConfirmationDialog) {
|
||||||
|
// Store the original quickCreate function
|
||||||
|
const originalQuickCreate = props.quickCreate;
|
||||||
|
|
||||||
// Replace the quickCreate function with our own implementation
|
// Replace with openConfirmationDialog
|
||||||
if (originalQuickCreate) {
|
props.quickCreate = (name) => this.openConfirmationDialog(name);
|
||||||
// Save the original quickCreate function
|
|
||||||
this._originalQuickCreate = originalQuickCreate;
|
|
||||||
|
|
||||||
// Override quickCreate to show a confirmation dialog
|
|
||||||
this.quickCreate = async (name) => {
|
|
||||||
this.state.isFloating = false;
|
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
this.addDialog(CreateConfirmationDialog, {
|
|
||||||
name: this.string,
|
|
||||||
value: name,
|
|
||||||
create: async () => {
|
|
||||||
try {
|
|
||||||
// Call the original quickCreate function
|
|
||||||
await this._originalQuickCreate(name);
|
|
||||||
resolve();
|
|
||||||
} catch (e) {
|
|
||||||
reject(e);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return props;
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue