diff options
| author | Dharmraj Jhala <dja@odoo.com> | 2019-11-01 11:24:04 +0000 |
|---|---|---|
| committer | qsm-odoo <qsm@odoo.com> | 2019-11-06 15:39:16 +0000 |
| commit | 3a9b5633061005090dcdc975650e4e80cf87141b (patch) | |
| tree | 35256fd547b8d4aafe520fcfbef34bd057ca64fb | |
| parent | 03425f13061cd9da238ed640229302340cb643d4 (diff) | |
[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
| -rw-r--r-- | addons/website_form/static/src/js/website_form_editor.js | 72 |
1 files changed, 48 insertions, 24 deletions
diff --git a/addons/website_form/static/src/js/website_form_editor.js b/addons/website_form/static/src/js/website_form_editor.js index ae723594402..6c412573013 100644 --- a/addons/website_form/static/src/js/website_form_editor.js +++ b/addons/website_form/static/src/js/website_form_editor.js @@ -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 (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); - } - }); + 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}); + } + }, + + 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); + } }); - } + }); } }, |
