Skip to content

Commit 72bf420

Browse files
bso-odooFrancoisGe
authored andcommitted
studio form options adaptations
1 parent 14fa97b commit 72bf420

File tree

9 files changed

+80
-51
lines changed

9 files changed

+80
-51
lines changed

addons/html_builder/static/src/utils/sync_cache.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ export class SyncCache {
77
}
88
async preload(params) {
99
const result = await this.asyncCache.read(params);
10-
this.syncCache.set(params, result);
10+
this.syncCache.set(JSON.stringify(params), result);
1111
return result;
1212
}
1313
get(params) {
14-
return this.syncCache.get(params);
14+
return this.syncCache.get(JSON.stringify(params));
1515
}
1616
invalidate() {
1717
this.asyncCache.invalidate();

addons/html_builder/static/src/website_builder/plugins/form/form_action_fields_option.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class FormActionFieldsOption extends BaseOptionComponent {
2020
}
2121
async getFormInfo(props = this.props) {
2222
const el = this.env.getEditingElement();
23-
const formInfo = await this.props.prepareFormModel(el, props.activeForm);
23+
const formInfo = await props.prepareFormModel(el, props.activeForm);
2424
Object.assign(this.state.formInfo, formInfo);
2525
}
2626
}

addons/html_builder/static/src/website_builder/plugins/form/form_model_required_field_alert.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ export class FormModelRequiredFieldAlert extends Component {
1818
}
1919
async handleProps(props) {
2020
// Get list of website_form compatible models, needed for alert message.
21-
const models = await props.fetchModels();
21+
const el = this.env.getEditingElement();
22+
const models = await props.fetchModels(el);
2223
const model = models.find((model) => model.model === props.modelName);
2324
const actionName = model?.website_form_label || props.modelName;
2425
this.state.message = _t("The field “%(field)s” is mandatory for the action “%(action)s”.", {
Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import { BaseOptionComponent, useDomState } from "@html_builder/core/utils";
2-
import { onWillStart } from "@odoo/owl";
1+
import { BaseOptionComponent } from "@html_builder/core/utils";
2+
import { onWillStart, onWillUpdateProps, useState } from "@odoo/owl";
33
import { getParsedDataFor } from "./utils";
44
import { FormActionFieldsOption } from "./form_action_fields_option";
55
import { session } from "@web/session";
66

77
export class FormOption extends BaseOptionComponent {
88
static template = "html_builder.website.s_website_form_form_option";
99
static props = {
10+
modelName: String,
1011
fetchModels: Function,
1112
prepareFormModel: Function,
1213
fetchFieldRecords: Function,
@@ -22,41 +23,8 @@ export class FormOption extends BaseOptionComponent {
2223
const el = this.env.getEditingElement();
2324
this.messageEl = el.parentElement.querySelector(".s_website_form_end_message");
2425
this.showEndMessage = false;
25-
onWillStart(async () => {
26-
// Hide change form parameters option for forms
27-
// e.g. User should not be enable to change existing job application form
28-
// to opportunity form in 'Apply job' page.
29-
this.modelCantChange = !!el.getAttribute("hide-change-model");
30-
31-
// Get list of website_form compatible models.
32-
this.models = await this.props.fetchModels();
33-
34-
const targetModelName = el.dataset.model_name || "mail.mail";
35-
this.domState.activeForm = this.models.find((m) => m.model === targetModelName);
36-
37-
// If the form has no model it means a new snippet has been dropped.
38-
// Apply the default model selected in willStart on it.
39-
if (!el.dataset.model_name) {
40-
const formInfo = await this.props.prepareFormModel(el, this.domState.activeForm);
41-
this.props.applyFormModel(
42-
el,
43-
this.domState.activeForm,
44-
this.domState.activeForm.id,
45-
formInfo
46-
);
47-
}
48-
});
49-
this.domState = useDomState((el) => {
50-
if (!this.models) {
51-
return {
52-
activeForm: {},
53-
};
54-
}
55-
const targetModelName = el.dataset.model_name || "mail.mail";
56-
const activeForm = this.models.find((m) => m.model === targetModelName);
57-
return {
58-
activeForm,
59-
};
26+
this.state = useState({
27+
activeForm: {},
6028
});
6129
// Get the email_to value from the data-for attribute if it exists. We
6230
// use it if there is no value on the email_to input.
@@ -65,5 +33,25 @@ export class FormOption extends BaseOptionComponent {
6533
if (dataForValues) {
6634
this.dataForEmailTo = dataForValues["email_to"];
6735
}
36+
onWillStart(async () => this.handleProps(this.props));
37+
onWillUpdateProps(async (props) => this.handleProps(props));
38+
}
39+
async handleProps(props) {
40+
const el = this.env.getEditingElement();
41+
// Hide change form parameters option for forms
42+
// e.g. User should not be enable to change existing job application form
43+
// to opportunity form in 'Apply job' page.
44+
this.modelCantChange = !!el.getAttribute("hide-change-model");
45+
46+
// Get list of website_form compatible models.
47+
this.models = await props.fetchModels(el);
48+
this.state.activeForm = this.models.find((m) => m.model === props.modelName);
49+
50+
// If the form has no model it means a new snippet has been dropped.
51+
// Apply the default model selected in willStart on it.
52+
if (!el.dataset.model_name) {
53+
const formInfo = await props.prepareFormModel(el, this.state.activeForm);
54+
props.applyFormModel(el, this.state.activeForm, this.state.activeForm.id, formInfo);
55+
}
6856
}
6957
}

addons/html_builder/static/src/website_builder/plugins/form/form_option.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</t>
1111
</BuilderSelect>
1212
</BuilderRow>
13-
<FormActionFieldsOption activeForm="domState.activeForm" prepareFormModel="props.prepareFormModel"/>
13+
<FormActionFieldsOption activeForm="state.activeForm" prepareFormModel="props.prepareFormModel"/>
1414
<BuilderRow label.translate="Marked Fields">
1515
<BuilderSelect id="'field_mark_select'" action="'updateLabelsMark'">
1616
<BuilderSelectItem classAction="''">None</BuilderSelectItem>

addons/html_builder/static/src/website_builder/plugins/form/form_option_plugin.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Cache } from "@web/core/utils/cache";
33
import { Plugin } from "@html_editor/plugin";
44
import { reactive } from "@odoo/owl";
55
import { ConfirmationDialog } from "@web/core/confirmation_dialog/confirmation_dialog";
6-
import { FormOption } from "./form_option";
6+
import { FormOptionRedraw } from "./form_option_redraw";
77
import { FormFieldOptionRedraw } from "./form_field_option_redraw";
88
import { FormOptionAddFieldButton } from "./form_option_add_field_button";
99
import {
@@ -19,6 +19,7 @@ import {
1919
getFieldType,
2020
getLabelPosition,
2121
getMark,
22+
getModelName,
2223
getMultipleInputs,
2324
getNewRecordId,
2425
getQuotesEncodedName,
@@ -84,7 +85,7 @@ export class FormOptionPlugin extends Plugin {
8485
},
8586
builder_options: [
8687
{
87-
OptionComponent: FormOption,
88+
OptionComponent: FormOptionRedraw,
8889
props: {
8990
fetchModels: this.fetchModels.bind(this),
9091
prepareFormModel: this.prepareFormModel.bind(this),
@@ -152,9 +153,9 @@ export class FormOptionPlugin extends Plugin {
152153
if (modelCantChange) {
153154
return;
154155
}
155-
const activeForm = this.modelsCache
156-
.get()
157-
.find((model) => model.id === parseInt(modelId));
156+
const activeForm = this.getModelsCache(el).find(
157+
(model) => model.id === parseInt(modelId)
158+
);
158159
return { activeForm, formInfo: await this.prepareFormModel(el, activeForm) };
159160
},
160161
apply: ({ editingElement: el, value: modelId, loadResult }) => {
@@ -169,8 +170,8 @@ export class FormOptionPlugin extends Plugin {
169170
);
170171
},
171172
isApplied: ({ editingElement: el, value: modelId }) => {
172-
const models = this.modelsCache.get();
173-
const targetModelName = el.dataset.model_name || "mail.mail";
173+
const models = this.getModelsCache(el);
174+
const targetModelName = getModelName(el);
174175
const activeForm = models.find((m) => m.model === targetModelName);
175176
return parseInt(modelId) === activeForm.id;
176177
},
@@ -541,7 +542,11 @@ export class FormOptionPlugin extends Plugin {
541542
this.fieldRecordsCache.invalidate();
542543
this.authorizedFieldsCache.invalidate();
543544
}
544-
async fetchModels() {
545+
getModelsCache(formEl) {
546+
// Through a method so that it can be overridden.
547+
return this.modelsCache.get();
548+
}
549+
async fetchModels(formEl) {
545550
return this.modelsCache.preload();
546551
}
547552
async _fetchModels() {
@@ -666,7 +671,7 @@ export class FormOptionPlugin extends Plugin {
666671
for (const fieldEl of el.querySelectorAll(".s_website_form_field")) {
667672
fieldEl.remove();
668673
}
669-
activeForm = this.modelsCache.get().find((model) => model.id === modelId);
674+
activeForm = this.getModelsCache(el).find((model) => model.id === modelId);
670675
}
671676
// Success page
672677
if (!el.dataset.successMode) {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { BaseOptionComponent, useDomState } from "@html_builder/core/utils";
2+
import { FormOption } from "./form_option";
3+
import { getModelName } from "./utils";
4+
import { xml } from "@odoo/owl";
5+
6+
const formOptionRedrawProps = { ...FormOption.props };
7+
delete formOptionRedrawProps.modelName;
8+
9+
export class FormOptionRedraw extends BaseOptionComponent {
10+
static template = xml`<FormOption t-props="getProps()"/>`;
11+
static props = formOptionRedrawProps;
12+
static components = { FormOption };
13+
14+
setup() {
15+
super.setup();
16+
this.domState = useDomState((formEl) => {
17+
const modelName = getModelName(formEl);
18+
return {
19+
modelName,
20+
};
21+
});
22+
}
23+
24+
getProps() {
25+
return {
26+
...this.props,
27+
modelName: this.domState.modelName,
28+
};
29+
}
30+
}

addons/html_builder/static/src/website_builder/plugins/form/utils.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,10 @@ export function getDomain(formEl, name, type, relation) {
483483
return field && field.domain;
484484
}
485485

486+
export function getModelName(formEl) {
487+
return formEl.dataset.model_name || "mail.mail";
488+
}
489+
486490
export function getListItems(fieldEl) {
487491
const selectEl = getSelect(fieldEl);
488492
const multipleInputsEl = getMultipleInputs(fieldEl);

addons/website_sale/__manifest__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@
156156
],
157157
'html_builder.assets': [
158158
'website_sale/static/src/plugins/**/*',
159+
'website_sale/static/src/js/website_sale_form_editor.js',
159160
],
160161
'website.assets_wysiwyg': [
161162
'website_sale/static/src/scss/website_sale.editor.scss',

0 commit comments

Comments
 (0)