opnform-host-nginx/client/lib/forms/FormLogicPropertyResolver.js

100 lines
2.2 KiB
JavaScript
Raw Normal View History

import { conditionsMet } from "./FormLogicConditionChecker"
2023-12-18 10:35:00 +01:00
class FormLogicPropertyResolver {
conditionsMet = conditionsMet
property = null
formData = null
logic = false
2023-12-18 10:35:00 +01:00
constructor(property, formData) {
2023-12-18 10:35:00 +01:00
this.property = property
this.formData = formData
this.logic = property.logic !== undefined ? property.logic : false
2023-12-18 10:35:00 +01:00
}
isHidden() {
2023-12-18 10:35:00 +01:00
if (!this.logic) {
return this.property.hidden
}
const conditionsMet = this.conditionsMet(
this.logic.conditions,
this.formData,
)
if (
conditionsMet &&
this.property.hidden &&
this.logic.actions.length > 0 &&
this.logic.actions.includes("show-block")
) {
2023-12-18 10:35:00 +01:00
return false
} else if (
conditionsMet &&
!this.property.hidden &&
this.logic.actions.length > 0 &&
this.logic.actions.includes("hide-block")
) {
2023-12-18 10:35:00 +01:00
return true
} else {
return this.property.hidden
}
}
isRequired() {
2023-12-18 10:35:00 +01:00
if (!this.logic) {
return this.property.required
}
const conditionsMet = this.conditionsMet(
this.logic.conditions,
this.formData,
)
if (
conditionsMet &&
this.property.required &&
this.logic.actions.length > 0 &&
this.logic.actions.includes("make-it-optional")
) {
2023-12-18 10:35:00 +01:00
return false
} else if (
conditionsMet &&
!this.property.required &&
this.logic.actions.length > 0 &&
this.logic.actions.includes("require-answer")
) {
2023-12-18 10:35:00 +01:00
return true
} else {
return this.property.required
}
}
isDisabled() {
2023-12-18 10:35:00 +01:00
if (!this.logic) {
return this.property.disabled
}
const conditionsMet = this.conditionsMet(
this.logic.conditions,
this.formData,
)
if (
conditionsMet &&
this.property.disabled &&
this.logic.actions.length > 0 &&
this.logic.actions.includes("enable-block")
) {
2023-12-18 10:35:00 +01:00
return false
} else if (
conditionsMet &&
!this.property.disabled &&
this.logic.actions.length > 0 &&
this.logic.actions.includes("disable-block")
) {
2023-12-18 10:35:00 +01:00
return true
} else {
return this.property.disabled
}
}
}
export default FormLogicPropertyResolver