|
| 1 | +import { BaseOptionComponent, useDomState } from "@html_builder/core/utils"; |
| 2 | +import { onWillStart, onWillUpdateProps, useState } from "@odoo/owl"; |
| 3 | +import { FormActionFieldsOption } from "./form_action_fields_option"; |
| 4 | +import { getDependencyEl, getMultipleInputs, isFieldCustom } from "./utils"; |
| 5 | + |
| 6 | +export class FormFieldOption extends BaseOptionComponent { |
| 7 | + static template = "html_builder.website.s_website_form_field_option"; |
| 8 | + static props = { |
| 9 | + loadFieldOptionData: Function, |
| 10 | + redrawSequence: { type: Number, optional: true }, |
| 11 | + }; |
| 12 | + static components = { FormActionFieldsOption }; |
| 13 | + |
| 14 | + setup() { |
| 15 | + super.setup(); |
| 16 | + this.state = useState({ |
| 17 | + availableFields: [], |
| 18 | + conditionInputs: [], |
| 19 | + conditionValueList: [], |
| 20 | + dependencyEl: null, |
| 21 | + }); |
| 22 | + this.domState = useDomState((el) => ({ el })); |
| 23 | + onWillStart(async () => { |
| 24 | + const el = this.env.getEditingElement(); |
| 25 | + const fieldOptionData = await this.props.loadFieldOptionData(el); |
| 26 | + this.state.availableFields.push(...fieldOptionData.availableFields); |
| 27 | + this.state.conditionInputs.push(...fieldOptionData.conditionInputs); |
| 28 | + this.state.conditionValueList.push(...fieldOptionData.conditionValueList); |
| 29 | + this.state.dependencyEl = getDependencyEl(el); |
| 30 | + }); |
| 31 | + onWillUpdateProps(async (props) => { |
| 32 | + const el = this.env.getEditingElement(); |
| 33 | + const fieldOptionData = await props.loadFieldOptionData(el); |
| 34 | + this.state.availableFields.length = 0; |
| 35 | + this.state.availableFields.push(...fieldOptionData.availableFields); |
| 36 | + this.state.conditionInputs.length = 0; |
| 37 | + this.state.conditionInputs.push(...fieldOptionData.conditionInputs); |
| 38 | + this.state.conditionValueList.length = 0; |
| 39 | + this.state.conditionValueList.push(...fieldOptionData.conditionValueList); |
| 40 | + this.state.dependencyEl = getDependencyEl(el); |
| 41 | + }); |
| 42 | + // TODO select field's hack ? |
| 43 | + } |
| 44 | + get isTextConditionValueVisible() { |
| 45 | + const el = this.env.getEditingElement(); |
| 46 | + const dependencyEl = getDependencyEl(el); |
| 47 | + if ( |
| 48 | + !el.classList.contains("s_website_form_field_hidden_if") || |
| 49 | + (dependencyEl && |
| 50 | + (["checkbox", "radio"].includes(dependencyEl.type) || |
| 51 | + dependencyEl.nodeName === "SELECT")) |
| 52 | + ) { |
| 53 | + return false; |
| 54 | + } |
| 55 | + if (!dependencyEl) { |
| 56 | + return true; |
| 57 | + } |
| 58 | + if (dependencyEl?.classList.contains("datetimepicker-input")) { |
| 59 | + return false; |
| 60 | + } |
| 61 | + return ( |
| 62 | + (["text", "email", "tel", "url", "search", "password", "number"].includes( |
| 63 | + dependencyEl.type |
| 64 | + ) || |
| 65 | + dependencyEl.nodeName === "TEXTAREA") && |
| 66 | + !["set", "!set"].includes(el.dataset.visibilityComparator) |
| 67 | + ); |
| 68 | + } |
| 69 | + get isTextConditionOperatorVisible() { |
| 70 | + const el = this.env.getEditingElement(); |
| 71 | + const dependencyEl = getDependencyEl(el); |
| 72 | + if ( |
| 73 | + !el.classList.contains("s_website_form_field_hidden_if") || |
| 74 | + dependencyEl?.classList.contains("datetimepicker-input") |
| 75 | + ) { |
| 76 | + return false; |
| 77 | + } |
| 78 | + return ( |
| 79 | + !dependencyEl || |
| 80 | + ["text", "email", "tel", "url", "search", "password"].includes(dependencyEl.type) || |
| 81 | + dependencyEl.nodeName === "TEXTAREA" |
| 82 | + ); |
| 83 | + } |
| 84 | + get isExisingFieldSelectType() { |
| 85 | + const el = this.env.getEditingElement(); |
| 86 | + return !isFieldCustom(el) && ["selection", "many2one"].includes(el.dataset.type); |
| 87 | + } |
| 88 | + get isMultipleInputs() { |
| 89 | + const el = this.env.getEditingElement(); |
| 90 | + return !!getMultipleInputs(el); |
| 91 | + } |
| 92 | + get isMaxFilesVisible() { |
| 93 | + // Do not display the option if only one file is supposed to be |
| 94 | + // uploaded in the field. |
| 95 | + const el = this.env.getEditingElement(); |
| 96 | + const fieldEl = el.closest(".s_website_form_field"); |
| 97 | + return ( |
| 98 | + fieldEl.classList.contains("s_website_form_custom") || |
| 99 | + ["one2many", "many2many"].includes(fieldEl.dataset.type) |
| 100 | + ); |
| 101 | + } |
| 102 | +} |
0 commit comments