[IMP] website_form: show warning while changing form parameters

If user changes the form-parameters (like targetted model), it removes
all the existing fields without any warning and re-renders a new form
according to changed parameters.

This commit improves above behavior to prevent accidental loss of a well
designed form. Now when user changes form parameters, a warging will be
shown to user before re-rendering the form if the model is not same.

Part of https://github.com/odoo/odoo/pull/30595
task-1874350
This commit is contained in:
Dharmraj Jhala
2019-11-06 15:39:16 +00:00
committed by qsm-odoo
parent 03425f1306
commit 3a9b563306
@@ -363,32 +363,56 @@ odoo.define('website_form_editor', function (require) {
init_form: function () {
var self = this;
var modelName = this.activeForm.model;
var currentModel = this.$target.attr('data-model_name');
var formKey = this.activeForm.website_form_key;
if (modelName !== this.$target.attr('data-model_name')) {
this.$target.attr('data-model_name', modelName);
this.$target.find(".form-field:not(:has('.o_website_form_send')), .o_form_heading").remove();
if (!currentModel) {
// Directly change the parameters if model is being set for the first time
this.changeFormParameters(modelName, formKey);
} else if (modelName !== currentModel) {
var warningMessage = _t("Are you sure you want to change the parameters of your form? All the current fields will be discarded.");
new Dialog(this, {
title: _t("Warning"),
size: 'medium',
$content: $('<div>', {text: warningMessage}),
buttons: [{
text: _t("Ok"),
classes: 'btn-primary',
click: function () {
self.changeFormParameters(modelName, formKey);
},
close: true,
}, {
text: _t("Discard"),
close: true,
}],
}).open({shouldFocusButtons: true});
}
},
if (formKey) {
var formInfo = FormEditorRegistry.get(formKey);
ajax.loadXML(formInfo.defaultTemplatePath, qweb).then(function () {
// Append form title
$('<h1>', {
class: 'o_form_heading',
text: self.activeForm.website_form_label,
}).prependTo(self.$target.find('.container'));
self.$target.find('.form-group:has(".o_website_form_send")').before($(qweb.render(formInfo.defaultTemplateName)));
changeFormParameters: function (modelName, formKey) {
var self = this;
this.$target.attr('data-model_name', modelName);
this.$target.find(".form-field:not(:has('.o_website_form_send')), .o_form_heading").remove();
if (formKey) {
var formInfo = FormEditorRegistry.get(formKey);
ajax.loadXML(formInfo.defaultTemplatePath, qweb).then(function () {
// Append form title
$('<h1>', {
class: 'o_form_heading',
text: self.activeForm.website_form_label,
}).prependTo(self.$target.find('.container'));
self.$target.find('.form-group:has(".o_website_form_send")').before($(qweb.render(formInfo.defaultTemplateName)));
});
} else {
// Force fetch the fields of the new model
// and render all model required fields
this.fetch_model_fields().then(function (fields) {
_.each(fields, function (field, field_name) {
if (field.required) {
self.append_field(field);
}
});
} else {
// Force fetch the fields of the new model
// and render all model required fields
this.fetch_model_fields().then(function (fields) {
_.each(fields, function (field, field_name){
if (field.required) {
self.append_field(field);
}
});
});
}
});
}
},