2024-04-15 19:39:03 +02:00
|
|
|
import { conditionsMet } from "./FormLogicConditionChecker"
|
2023-12-18 10:35:00 +01:00
|
|
|
class FormLogicPropertyResolver {
|
2024-04-15 19:39:03 +02:00
|
|
|
conditionsMet = conditionsMet
|
|
|
|
|
property = null
|
|
|
|
|
formData = null
|
|
|
|
|
logic = false
|
2023-12-18 10:35:00 +01:00
|
|
|
|
2024-04-15 19:39:03 +02:00
|
|
|
constructor(property, formData) {
|
2023-12-18 10:35:00 +01:00
|
|
|
this.property = property
|
|
|
|
|
this.formData = formData
|
2024-04-15 19:39:03 +02:00
|
|
|
this.logic = property.logic !== undefined ? property.logic : false
|
2023-12-18 10:35:00 +01:00
|
|
|
}
|
|
|
|
|
|
2024-04-15 19:39:03 +02:00
|
|
|
isHidden() {
|
2023-12-18 10:35:00 +01:00
|
|
|
if (!this.logic) {
|
|
|
|
|
return this.property.hidden
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-15 19:39:03 +02:00
|
|
|
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
|
2024-04-15 19:39:03 +02:00
|
|
|
} 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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-15 19:39:03 +02:00
|
|
|
isRequired() {
|
2023-12-18 10:35:00 +01:00
|
|
|
if (!this.logic) {
|
|
|
|
|
return this.property.required
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-15 19:39:03 +02:00
|
|
|
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
|
2024-04-15 19:39:03 +02:00
|
|
|
} 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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-15 19:39:03 +02:00
|
|
|
isDisabled() {
|
2023-12-18 10:35:00 +01:00
|
|
|
if (!this.logic) {
|
|
|
|
|
return this.property.disabled
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-15 19:39:03 +02:00
|
|
|
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
|
2024-04-15 19:39:03 +02:00
|
|
|
} 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
|