0351d front end linting (#377)
* feat: disable custom script for trial users * after lint fix * frontend linting --------- Co-authored-by: Julien Nahum <julien@nahum.net>
This commit is contained in:
319
client/lib/forms/FormLogicConditionChecker.js
vendored
319
client/lib/forms/FormLogicConditionChecker.js
vendored
@@ -1,24 +1,27 @@
|
||||
export function conditionsMet (conditions, formData) {
|
||||
export function conditionsMet(conditions, formData) {
|
||||
if (conditions === undefined || conditions === null) {
|
||||
return false
|
||||
}
|
||||
|
||||
// If it's not a group, just a single condition
|
||||
if (conditions.operatorIdentifier === undefined) {
|
||||
return propertyConditionMet(conditions.value, conditions.value ? formData[conditions.value.property_meta.id] : null)
|
||||
return propertyConditionMet(
|
||||
conditions.value,
|
||||
conditions.value ? formData[conditions.value.property_meta.id] : null,
|
||||
)
|
||||
}
|
||||
|
||||
if (conditions.operatorIdentifier === 'and') {
|
||||
if (conditions.operatorIdentifier === "and") {
|
||||
let isvalid = true
|
||||
conditions.children.forEach(childrenCondition => {
|
||||
conditions.children.forEach((childrenCondition) => {
|
||||
if (!conditionsMet(childrenCondition, formData)) {
|
||||
isvalid = false
|
||||
}
|
||||
})
|
||||
return isvalid
|
||||
} else if (conditions.operatorIdentifier === 'or') {
|
||||
} else if (conditions.operatorIdentifier === "or") {
|
||||
let isvalid = false
|
||||
conditions.children.forEach(childrenCondition => {
|
||||
conditions.children.forEach((childrenCondition) => {
|
||||
if (conditionsMet(childrenCondition, formData)) {
|
||||
isvalid = true
|
||||
}
|
||||
@@ -26,302 +29,344 @@ export function conditionsMet (conditions, formData) {
|
||||
return isvalid
|
||||
}
|
||||
|
||||
throw new Error('Unexcepted operatorIdentifier:' + conditions.operatorIdentifier)
|
||||
throw new Error(
|
||||
"Unexcepted operatorIdentifier:" + conditions.operatorIdentifier,
|
||||
)
|
||||
}
|
||||
|
||||
function propertyConditionMet (propertyCondition, value) {
|
||||
function propertyConditionMet(propertyCondition, value) {
|
||||
if (!propertyCondition) {
|
||||
return false
|
||||
}
|
||||
switch (propertyCondition.property_meta.type) {
|
||||
case 'text':
|
||||
case 'url':
|
||||
case 'email':
|
||||
case 'phone_number':
|
||||
case "text":
|
||||
case "url":
|
||||
case "email":
|
||||
case "phone_number":
|
||||
return textConditionMet(propertyCondition, value)
|
||||
case 'number':
|
||||
case 'rating':
|
||||
case 'scale':
|
||||
case 'slider':
|
||||
case "number":
|
||||
case "rating":
|
||||
case "scale":
|
||||
case "slider":
|
||||
return numberConditionMet(propertyCondition, value)
|
||||
case 'checkbox':
|
||||
case "checkbox":
|
||||
return checkboxConditionMet(propertyCondition, value)
|
||||
case 'select':
|
||||
case "select":
|
||||
return selectConditionMet(propertyCondition, value)
|
||||
case 'date':
|
||||
case "date":
|
||||
return dateConditionMet(propertyCondition, value)
|
||||
case 'multi_select':
|
||||
case "multi_select":
|
||||
return multiSelectConditionMet(propertyCondition, value)
|
||||
case 'files':
|
||||
case "files":
|
||||
return filesConditionMet(propertyCondition, value)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
function checkEquals (condition, fieldValue) {
|
||||
function checkEquals(condition, fieldValue) {
|
||||
return condition.value === fieldValue
|
||||
}
|
||||
|
||||
function checkContains (condition, fieldValue) {
|
||||
return (fieldValue) ? fieldValue.includes(condition.value) : false
|
||||
function checkContains(condition, fieldValue) {
|
||||
return fieldValue ? fieldValue.includes(condition.value) : false
|
||||
}
|
||||
|
||||
function checkListContains (condition, fieldValue) {
|
||||
function checkListContains(condition, fieldValue) {
|
||||
if (!fieldValue) return false
|
||||
|
||||
if (Array.isArray(condition.value)) {
|
||||
return condition.value.every(r => fieldValue.includes(r))
|
||||
return condition.value.every((r) => fieldValue.includes(r))
|
||||
} else {
|
||||
return fieldValue.includes(condition.value)
|
||||
}
|
||||
}
|
||||
|
||||
function checkStartsWith (condition, fieldValue) {
|
||||
function checkStartsWith(condition, fieldValue) {
|
||||
return fieldValue?.startsWith(condition.value)
|
||||
}
|
||||
|
||||
function checkendsWith (condition, fieldValue) {
|
||||
function checkendsWith(condition, fieldValue) {
|
||||
return fieldValue?.endsWith(condition.value)
|
||||
}
|
||||
|
||||
function checkIsEmpty (condition, fieldValue) {
|
||||
return (!fieldValue || fieldValue.length === 0)
|
||||
function checkIsEmpty(condition, fieldValue) {
|
||||
return !fieldValue || fieldValue.length === 0
|
||||
}
|
||||
|
||||
function checkGreaterThan (condition, fieldValue) {
|
||||
return (condition.value && fieldValue && parseFloat(fieldValue) > parseFloat(condition.value))
|
||||
function checkGreaterThan(condition, fieldValue) {
|
||||
return (
|
||||
condition.value &&
|
||||
fieldValue &&
|
||||
parseFloat(fieldValue) > parseFloat(condition.value)
|
||||
)
|
||||
}
|
||||
|
||||
function checkGreaterThanEqual (condition, fieldValue) {
|
||||
return (condition.value && fieldValue && parseFloat(fieldValue) >= parseFloat(condition.value))
|
||||
function checkGreaterThanEqual(condition, fieldValue) {
|
||||
return (
|
||||
condition.value &&
|
||||
fieldValue &&
|
||||
parseFloat(fieldValue) >= parseFloat(condition.value)
|
||||
)
|
||||
}
|
||||
|
||||
function checkLessThan (condition, fieldValue) {
|
||||
return (condition.value && fieldValue && parseFloat(fieldValue) < parseFloat(condition.value))
|
||||
function checkLessThan(condition, fieldValue) {
|
||||
return (
|
||||
condition.value &&
|
||||
fieldValue &&
|
||||
parseFloat(fieldValue) < parseFloat(condition.value)
|
||||
)
|
||||
}
|
||||
|
||||
function checkLessThanEqual (condition, fieldValue) {
|
||||
return (condition.value && fieldValue && parseFloat(fieldValue) <= parseFloat(condition.value))
|
||||
function checkLessThanEqual(condition, fieldValue) {
|
||||
return (
|
||||
condition.value &&
|
||||
fieldValue &&
|
||||
parseFloat(fieldValue) <= parseFloat(condition.value)
|
||||
)
|
||||
}
|
||||
|
||||
function checkBefore (condition, fieldValue) {
|
||||
return (condition.value && fieldValue && fieldValue > condition.value)
|
||||
function checkBefore(condition, fieldValue) {
|
||||
return condition.value && fieldValue && fieldValue > condition.value
|
||||
}
|
||||
|
||||
function checkAfter (condition, fieldValue) {
|
||||
return (condition.value && fieldValue && fieldValue < condition.value)
|
||||
function checkAfter(condition, fieldValue) {
|
||||
return condition.value && fieldValue && fieldValue < condition.value
|
||||
}
|
||||
|
||||
function checkOnOrBefore (condition, fieldValue) {
|
||||
return (condition.value && fieldValue && fieldValue >= condition.value)
|
||||
function checkOnOrBefore(condition, fieldValue) {
|
||||
return condition.value && fieldValue && fieldValue >= condition.value
|
||||
}
|
||||
|
||||
function checkOnOrAfter (condition, fieldValue) {
|
||||
return (condition.value && fieldValue && fieldValue <= condition.value)
|
||||
function checkOnOrAfter(condition, fieldValue) {
|
||||
return condition.value && fieldValue && fieldValue <= condition.value
|
||||
}
|
||||
|
||||
function checkPastWeek (condition, fieldValue) {
|
||||
function checkPastWeek(condition, fieldValue) {
|
||||
if (!fieldValue) return false
|
||||
const fieldDate = new Date(fieldValue)
|
||||
const today = new Date()
|
||||
return (fieldDate <= today && fieldDate >= new Date(today.getFullYear(), today.getMonth(), today.getDate() - 7))
|
||||
return (
|
||||
fieldDate <= today &&
|
||||
fieldDate >=
|
||||
new Date(today.getFullYear(), today.getMonth(), today.getDate() - 7)
|
||||
)
|
||||
}
|
||||
|
||||
function checkPastMonth (condition, fieldValue) {
|
||||
function checkPastMonth(condition, fieldValue) {
|
||||
if (!fieldValue) return false
|
||||
const fieldDate = new Date(fieldValue)
|
||||
const today = new Date()
|
||||
return (fieldDate <= today && fieldDate >= new Date(today.getFullYear(), today.getMonth() - 1, today.getDate()))
|
||||
return (
|
||||
fieldDate <= today &&
|
||||
fieldDate >=
|
||||
new Date(today.getFullYear(), today.getMonth() - 1, today.getDate())
|
||||
)
|
||||
}
|
||||
|
||||
function checkPastYear (condition, fieldValue) {
|
||||
function checkPastYear(condition, fieldValue) {
|
||||
if (!fieldValue) return false
|
||||
const fieldDate = new Date(fieldValue)
|
||||
const today = new Date()
|
||||
return (fieldDate <= today && fieldDate >= new Date(today.getFullYear() - 1, today.getMonth(), today.getDate()))
|
||||
return (
|
||||
fieldDate <= today &&
|
||||
fieldDate >=
|
||||
new Date(today.getFullYear() - 1, today.getMonth(), today.getDate())
|
||||
)
|
||||
}
|
||||
|
||||
function checkNextWeek (condition, fieldValue) {
|
||||
function checkNextWeek(condition, fieldValue) {
|
||||
if (!fieldValue) return false
|
||||
const fieldDate = new Date(fieldValue)
|
||||
const today = new Date()
|
||||
return (fieldDate >= today && fieldDate <= new Date(today.getFullYear(), today.getMonth(), today.getDate() + 7))
|
||||
return (
|
||||
fieldDate >= today &&
|
||||
fieldDate <=
|
||||
new Date(today.getFullYear(), today.getMonth(), today.getDate() + 7)
|
||||
)
|
||||
}
|
||||
|
||||
function checkNextMonth (condition, fieldValue) {
|
||||
function checkNextMonth(condition, fieldValue) {
|
||||
if (!fieldValue) return false
|
||||
const fieldDate = new Date(fieldValue)
|
||||
const today = new Date()
|
||||
return (fieldDate >= today && fieldDate <= new Date(today.getFullYear(), today.getMonth() + 1, today.getDate()))
|
||||
return (
|
||||
fieldDate >= today &&
|
||||
fieldDate <=
|
||||
new Date(today.getFullYear(), today.getMonth() + 1, today.getDate())
|
||||
)
|
||||
}
|
||||
|
||||
function checkNextYear (condition, fieldValue) {
|
||||
function checkNextYear(condition, fieldValue) {
|
||||
if (!fieldValue) return false
|
||||
const fieldDate = new Date(fieldValue)
|
||||
const today = new Date()
|
||||
return (fieldDate >= today && fieldDate <= new Date(today.getFullYear() + 1, today.getMonth(), today.getDate()))
|
||||
return (
|
||||
fieldDate >= today &&
|
||||
fieldDate <=
|
||||
new Date(today.getFullYear() + 1, today.getMonth(), today.getDate())
|
||||
)
|
||||
}
|
||||
|
||||
function checkLength (condition, fieldValue, operator = '===') {
|
||||
if(!fieldValue || fieldValue.length === 0) return false;
|
||||
function checkLength(condition, fieldValue, operator = "===") {
|
||||
if (!fieldValue || fieldValue.length === 0) return false
|
||||
switch (operator) {
|
||||
case '===':
|
||||
case "===":
|
||||
return fieldValue.length === parseInt(condition.value)
|
||||
case '!==':
|
||||
case "!==":
|
||||
return fieldValue.length !== parseInt(condition.value)
|
||||
case '>':
|
||||
case ">":
|
||||
return fieldValue.length > parseInt(condition.value)
|
||||
case '>=':
|
||||
case ">=":
|
||||
return fieldValue.length >= parseInt(condition.value)
|
||||
case '<':
|
||||
case "<":
|
||||
return fieldValue.length < parseInt(condition.value)
|
||||
case '<=':
|
||||
case "<=":
|
||||
return fieldValue.length <= parseInt(condition.value)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
function textConditionMet (propertyCondition, value) {
|
||||
function textConditionMet(propertyCondition, value) {
|
||||
switch (propertyCondition.operator) {
|
||||
case 'equals':
|
||||
case "equals":
|
||||
return checkEquals(propertyCondition, value)
|
||||
case 'does_not_equal':
|
||||
case "does_not_equal":
|
||||
return !checkEquals(propertyCondition, value)
|
||||
case 'contains':
|
||||
case "contains":
|
||||
return checkContains(propertyCondition, value)
|
||||
case 'does_not_contain':
|
||||
case "does_not_contain":
|
||||
return !checkContains(propertyCondition, value)
|
||||
case 'starts_with':
|
||||
case "starts_with":
|
||||
return checkStartsWith(propertyCondition, value)
|
||||
case 'ends_with':
|
||||
case "ends_with":
|
||||
return checkendsWith(propertyCondition, value)
|
||||
case 'is_empty':
|
||||
case "is_empty":
|
||||
return checkIsEmpty(propertyCondition, value)
|
||||
case 'is_not_empty':
|
||||
case "is_not_empty":
|
||||
return !checkIsEmpty(propertyCondition, value)
|
||||
case 'content_length_equals':
|
||||
return checkLength(propertyCondition, value, '===')
|
||||
case 'content_length_does_not_equal':
|
||||
return checkLength(propertyCondition, value, '!==')
|
||||
case 'content_length_greater_than':
|
||||
return checkLength(propertyCondition, value, '>')
|
||||
case 'content_length_greater_than_or_equal_to':
|
||||
return checkLength(propertyCondition, value, '>=')
|
||||
case 'content_length_less_than':
|
||||
return checkLength(propertyCondition, value, '<')
|
||||
case 'content_length_less_than_or_equal_to':
|
||||
return checkLength(propertyCondition, value, '<=')
|
||||
case "content_length_equals":
|
||||
return checkLength(propertyCondition, value, "===")
|
||||
case "content_length_does_not_equal":
|
||||
return checkLength(propertyCondition, value, "!==")
|
||||
case "content_length_greater_than":
|
||||
return checkLength(propertyCondition, value, ">")
|
||||
case "content_length_greater_than_or_equal_to":
|
||||
return checkLength(propertyCondition, value, ">=")
|
||||
case "content_length_less_than":
|
||||
return checkLength(propertyCondition, value, "<")
|
||||
case "content_length_less_than_or_equal_to":
|
||||
return checkLength(propertyCondition, value, "<=")
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
function numberConditionMet (propertyCondition, value) {
|
||||
function numberConditionMet(propertyCondition, value) {
|
||||
switch (propertyCondition.operator) {
|
||||
case 'equals':
|
||||
case "equals":
|
||||
return checkEquals(propertyCondition, value)
|
||||
case 'does_not_equal':
|
||||
case "does_not_equal":
|
||||
return !checkEquals(propertyCondition, value)
|
||||
case 'greater_than':
|
||||
case "greater_than":
|
||||
return checkGreaterThan(propertyCondition, value)
|
||||
case 'less_than':
|
||||
case "less_than":
|
||||
return checkLessThan(propertyCondition, value)
|
||||
case 'greater_than_or_equal_to':
|
||||
case "greater_than_or_equal_to":
|
||||
return checkGreaterThanEqual(propertyCondition, value)
|
||||
case 'less_than_or_equal_to':
|
||||
case "less_than_or_equal_to":
|
||||
return checkLessThanEqual(propertyCondition, value)
|
||||
case 'is_empty':
|
||||
case "is_empty":
|
||||
return checkIsEmpty(propertyCondition, value)
|
||||
case 'is_not_empty':
|
||||
case "is_not_empty":
|
||||
return checkIsEmpty(propertyCondition, value)
|
||||
case 'content_length_equals':
|
||||
return checkLength(propertyCondition, value, '===')
|
||||
case 'content_length_does_not_equal':
|
||||
return checkLength(propertyCondition, value, '!==')
|
||||
case 'content_length_greater_than':
|
||||
return checkLength(propertyCondition, value, '>')
|
||||
case 'content_length_greater_than_or_equal_to':
|
||||
return checkLength(propertyCondition, value, '>=')
|
||||
case 'content_length_less_than':
|
||||
return checkLength(propertyCondition, value, '<')
|
||||
case 'content_length_less_than_or_equal_to':
|
||||
return checkLength(propertyCondition, value, '<=')
|
||||
case "content_length_equals":
|
||||
return checkLength(propertyCondition, value, "===")
|
||||
case "content_length_does_not_equal":
|
||||
return checkLength(propertyCondition, value, "!==")
|
||||
case "content_length_greater_than":
|
||||
return checkLength(propertyCondition, value, ">")
|
||||
case "content_length_greater_than_or_equal_to":
|
||||
return checkLength(propertyCondition, value, ">=")
|
||||
case "content_length_less_than":
|
||||
return checkLength(propertyCondition, value, "<")
|
||||
case "content_length_less_than_or_equal_to":
|
||||
return checkLength(propertyCondition, value, "<=")
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
function checkboxConditionMet (propertyCondition, value) {
|
||||
function checkboxConditionMet(propertyCondition, value) {
|
||||
switch (propertyCondition.operator) {
|
||||
case 'equals':
|
||||
case "equals":
|
||||
return checkEquals(propertyCondition, value)
|
||||
case 'does_not_equal':
|
||||
case "does_not_equal":
|
||||
return !checkEquals(propertyCondition, value)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
function selectConditionMet (propertyCondition, value) {
|
||||
function selectConditionMet(propertyCondition, value) {
|
||||
switch (propertyCondition.operator) {
|
||||
case 'equals':
|
||||
case "equals":
|
||||
return checkEquals(propertyCondition, value)
|
||||
case 'does_not_equal':
|
||||
case "does_not_equal":
|
||||
return !checkEquals(propertyCondition, value)
|
||||
case 'is_empty':
|
||||
case "is_empty":
|
||||
return checkIsEmpty(propertyCondition, value)
|
||||
case 'is_not_empty':
|
||||
case "is_not_empty":
|
||||
return !checkIsEmpty(propertyCondition, value)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
function dateConditionMet (propertyCondition, value) {
|
||||
function dateConditionMet(propertyCondition, value) {
|
||||
switch (propertyCondition.operator) {
|
||||
case 'equals':
|
||||
case "equals":
|
||||
return checkEquals(propertyCondition, value)
|
||||
case 'before':
|
||||
case "before":
|
||||
return checkBefore(propertyCondition, value)
|
||||
case 'after':
|
||||
case "after":
|
||||
return checkAfter(propertyCondition, value)
|
||||
case 'on_or_before':
|
||||
case "on_or_before":
|
||||
return checkOnOrBefore(propertyCondition, value)
|
||||
case 'on_or_after':
|
||||
case "on_or_after":
|
||||
return checkOnOrAfter(propertyCondition, value)
|
||||
case 'is_empty':
|
||||
case "is_empty":
|
||||
return checkIsEmpty(propertyCondition, value)
|
||||
case 'past_week':
|
||||
case "past_week":
|
||||
return checkPastWeek(propertyCondition, value)
|
||||
case 'past_month':
|
||||
case "past_month":
|
||||
return checkPastMonth(propertyCondition, value)
|
||||
case 'past_year':
|
||||
case "past_year":
|
||||
return checkPastYear(propertyCondition, value)
|
||||
case 'next_week':
|
||||
case "next_week":
|
||||
return checkNextWeek(propertyCondition, value)
|
||||
case 'next_month':
|
||||
case "next_month":
|
||||
return checkNextMonth(propertyCondition, value)
|
||||
case 'next_year':
|
||||
case "next_year":
|
||||
return checkNextYear(propertyCondition, value)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
function multiSelectConditionMet (propertyCondition, value) {
|
||||
function multiSelectConditionMet(propertyCondition, value) {
|
||||
switch (propertyCondition.operator) {
|
||||
case 'contains':
|
||||
case "contains":
|
||||
return checkListContains(propertyCondition, value)
|
||||
case 'does_not_contain':
|
||||
case "does_not_contain":
|
||||
return !checkListContains(propertyCondition, value)
|
||||
case 'is_empty':
|
||||
case "is_empty":
|
||||
return checkIsEmpty(propertyCondition, value)
|
||||
case 'is_not_empty':
|
||||
case "is_not_empty":
|
||||
return !checkIsEmpty(propertyCondition, value)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
function filesConditionMet (propertyCondition, value) {
|
||||
function filesConditionMet(propertyCondition, value) {
|
||||
switch (propertyCondition.operator) {
|
||||
case 'is_empty':
|
||||
case "is_empty":
|
||||
return checkIsEmpty(propertyCondition, value)
|
||||
case 'is_not_empty':
|
||||
case "is_not_empty":
|
||||
return !checkIsEmpty(propertyCondition, value)
|
||||
}
|
||||
return false
|
||||
|
||||
77
client/lib/forms/FormLogicPropertyResolver.js
vendored
77
client/lib/forms/FormLogicPropertyResolver.js
vendored
@@ -1,55 +1,94 @@
|
||||
import { conditionsMet } from './FormLogicConditionChecker'
|
||||
import { conditionsMet } from "./FormLogicConditionChecker"
|
||||
class FormLogicPropertyResolver {
|
||||
conditionsMet = conditionsMet;
|
||||
property = null;
|
||||
formData = null;
|
||||
logic = false;
|
||||
conditionsMet = conditionsMet
|
||||
property = null
|
||||
formData = null
|
||||
logic = false
|
||||
|
||||
constructor (property, formData) {
|
||||
constructor(property, formData) {
|
||||
this.property = property
|
||||
this.formData = formData
|
||||
this.logic = (property.logic !== undefined) ? property.logic : false
|
||||
this.logic = property.logic !== undefined ? property.logic : false
|
||||
}
|
||||
|
||||
isHidden () {
|
||||
isHidden() {
|
||||
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')) {
|
||||
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")
|
||||
) {
|
||||
return false
|
||||
} else if (conditionsMet && !this.property.hidden && this.logic.actions.length > 0 && this.logic.actions.includes('hide-block')) {
|
||||
} else if (
|
||||
conditionsMet &&
|
||||
!this.property.hidden &&
|
||||
this.logic.actions.length > 0 &&
|
||||
this.logic.actions.includes("hide-block")
|
||||
) {
|
||||
return true
|
||||
} else {
|
||||
return this.property.hidden
|
||||
}
|
||||
}
|
||||
|
||||
isRequired () {
|
||||
isRequired() {
|
||||
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')) {
|
||||
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")
|
||||
) {
|
||||
return false
|
||||
} else if (conditionsMet && !this.property.required && this.logic.actions.length > 0 && this.logic.actions.includes('require-answer')) {
|
||||
} else if (
|
||||
conditionsMet &&
|
||||
!this.property.required &&
|
||||
this.logic.actions.length > 0 &&
|
||||
this.logic.actions.includes("require-answer")
|
||||
) {
|
||||
return true
|
||||
} else {
|
||||
return this.property.required
|
||||
}
|
||||
}
|
||||
|
||||
isDisabled () {
|
||||
isDisabled() {
|
||||
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')) {
|
||||
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")
|
||||
) {
|
||||
return false
|
||||
} else if (conditionsMet && !this.property.disabled && this.logic.actions.length > 0 && this.logic.actions.includes('disable-block')) {
|
||||
} else if (
|
||||
conditionsMet &&
|
||||
!this.property.disabled &&
|
||||
this.logic.actions.length > 0 &&
|
||||
this.logic.actions.includes("disable-block")
|
||||
) {
|
||||
return true
|
||||
} else {
|
||||
return this.property.disabled
|
||||
|
||||
128
client/lib/forms/FormPropertyLogicRule.js
vendored
128
client/lib/forms/FormPropertyLogicRule.js
vendored
@@ -1,79 +1,93 @@
|
||||
import OpenFilters from '../../data/open_filters.json'
|
||||
import OpenFilters from "../../data/open_filters.json"
|
||||
class FormPropertyLogicRule {
|
||||
property = null
|
||||
logic = null
|
||||
isConditionCorrect = true
|
||||
isActionCorrect = true
|
||||
ACTIONS_VALUES = [
|
||||
'show-block',
|
||||
'hide-block',
|
||||
'make-it-optional',
|
||||
'require-answer',
|
||||
'enable-block',
|
||||
'disable-block'
|
||||
"show-block",
|
||||
"hide-block",
|
||||
"make-it-optional",
|
||||
"require-answer",
|
||||
"enable-block",
|
||||
"disable-block",
|
||||
]
|
||||
CONDITION_MAPPING = OpenFilters
|
||||
|
||||
constructor (property) {
|
||||
constructor(property) {
|
||||
this.property = property
|
||||
this.logic = (property.logic !== undefined && property.logic) ? property.logic : null
|
||||
this.logic =
|
||||
property.logic !== undefined && property.logic ? property.logic : null
|
||||
}
|
||||
|
||||
isValid () {
|
||||
if (this.logic && this.logic['conditions']) {
|
||||
this.checkConditions(this.logic['conditions'])
|
||||
this.checkActions((this.logic && this.logic['actions']) ? this.logic['actions'] : null)
|
||||
isValid() {
|
||||
if (this.logic && this.logic["conditions"]) {
|
||||
this.checkConditions(this.logic["conditions"])
|
||||
this.checkActions(
|
||||
this.logic && this.logic["actions"] ? this.logic["actions"] : null,
|
||||
)
|
||||
}
|
||||
|
||||
return this.isConditionCorrect && this.isActionCorrect
|
||||
}
|
||||
|
||||
checkConditions (conditions) {
|
||||
if (conditions && conditions['operatorIdentifier']) {
|
||||
if ((conditions['operatorIdentifier'] !== 'and') && (conditions['operatorIdentifier'] !== 'or')) {
|
||||
checkConditions(conditions) {
|
||||
if (conditions && conditions["operatorIdentifier"]) {
|
||||
if (
|
||||
conditions["operatorIdentifier"] !== "and" &&
|
||||
conditions["operatorIdentifier"] !== "or"
|
||||
) {
|
||||
this.isConditionCorrect = false
|
||||
return
|
||||
}
|
||||
|
||||
if (conditions['operatorIdentifier']['children'] !== undefined || !Array.isArray(conditions['children'])) {
|
||||
if (
|
||||
conditions["operatorIdentifier"]["children"] !== undefined ||
|
||||
!Array.isArray(conditions["children"])
|
||||
) {
|
||||
this.isConditionCorrect = false
|
||||
return
|
||||
}
|
||||
|
||||
conditions['children'].forEach(childrenCondition => {
|
||||
conditions["children"].forEach((childrenCondition) => {
|
||||
this.checkConditions(childrenCondition)
|
||||
})
|
||||
} else if (conditions && conditions['identifier']) {
|
||||
} else if (conditions && conditions["identifier"]) {
|
||||
this.checkBaseCondition(conditions)
|
||||
}
|
||||
}
|
||||
|
||||
checkBaseCondition (condition) {
|
||||
if (condition['value'] === undefined ||
|
||||
condition['value']['property_meta'] === undefined ||
|
||||
condition['value']['property_meta']['type'] === undefined ||
|
||||
condition['value']['operator'] === undefined ||
|
||||
condition['value']['value'] === undefined
|
||||
) {
|
||||
checkBaseCondition(condition) {
|
||||
if (
|
||||
condition["value"] === undefined ||
|
||||
condition["value"]["property_meta"] === undefined ||
|
||||
condition["value"]["property_meta"]["type"] === undefined ||
|
||||
condition["value"]["operator"] === undefined ||
|
||||
condition["value"]["value"] === undefined
|
||||
) {
|
||||
this.isConditionCorrect = false
|
||||
return
|
||||
}
|
||||
|
||||
const typeField = condition['value']['property_meta']['type']
|
||||
const operator = condition['value']['operator']
|
||||
const value = condition['value']['value']
|
||||
const typeField = condition["value"]["property_meta"]["type"]
|
||||
const operator = condition["value"]["operator"]
|
||||
const value = condition["value"]["value"]
|
||||
|
||||
if (this.CONDITION_MAPPING[typeField] === undefined ||
|
||||
this.CONDITION_MAPPING[typeField]['comparators'][operator] === undefined
|
||||
) {
|
||||
if (
|
||||
this.CONDITION_MAPPING[typeField] === undefined ||
|
||||
this.CONDITION_MAPPING[typeField]["comparators"][operator] === undefined
|
||||
) {
|
||||
this.isConditionCorrect = false
|
||||
return
|
||||
}
|
||||
|
||||
const type = this.CONDITION_MAPPING[typeField]['comparators'][operator]['expected_type']
|
||||
|
||||
const type =
|
||||
this.CONDITION_MAPPING[typeField]["comparators"][operator][
|
||||
"expected_type"
|
||||
]
|
||||
if (Array.isArray(type)) {
|
||||
let foundCorrectType = false
|
||||
type.forEach(subtype => {
|
||||
type.forEach((subtype) => {
|
||||
if (this.valueHasCorrectType(subtype, value)) {
|
||||
foundCorrectType = true
|
||||
}
|
||||
@@ -88,26 +102,43 @@ class FormPropertyLogicRule {
|
||||
}
|
||||
}
|
||||
|
||||
valueHasCorrectType (type, value) {
|
||||
valueHasCorrectType(type, value) {
|
||||
if (
|
||||
(type === 'string' && typeof value !== 'string') ||
|
||||
(type === 'boolean' && typeof value !== 'boolean') ||
|
||||
(type === 'number' && typeof value !== 'number') ||
|
||||
(type === 'object' && !Array.isArray(value))
|
||||
(type === "string" && typeof value !== "string") ||
|
||||
(type === "boolean" && typeof value !== "boolean") ||
|
||||
(type === "number" && typeof value !== "number") ||
|
||||
(type === "object" && !Array.isArray(value))
|
||||
) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
checkActions (conditions) {
|
||||
checkActions(conditions) {
|
||||
if (Array.isArray(conditions) && conditions.length > 0) {
|
||||
conditions.forEach(val => {
|
||||
if (this.ACTIONS_VALUES.indexOf(val) === -1 ||
|
||||
(['nf-text', 'nf-code', 'nf-page-break', 'nf-divider', 'nf-image'].indexOf(this.property["type"]) > -1 && ['hide-block', 'show-block'].indexOf(val) === -1) ||
|
||||
(this.property["hidden"] !== undefined && this.property["hidden"] && ['show-block', 'require-answer'].indexOf(val) === -1) ||
|
||||
(this.property["required"] !== undefined && this.property["required"] && ['make-it-optional', 'hide-block', 'disable-block'].indexOf(val) === -1) ||
|
||||
(this.property["disabled"] !== undefined && this.property["disabled"] && ['enable-block', 'require-answer', 'make-it-optional'].indexOf(val) === -1)
|
||||
conditions.forEach((val) => {
|
||||
if (
|
||||
this.ACTIONS_VALUES.indexOf(val) === -1 ||
|
||||
([
|
||||
"nf-text",
|
||||
"nf-code",
|
||||
"nf-page-break",
|
||||
"nf-divider",
|
||||
"nf-image",
|
||||
].indexOf(this.property["type"]) > -1 &&
|
||||
["hide-block", "show-block"].indexOf(val) === -1) ||
|
||||
(this.property["hidden"] !== undefined &&
|
||||
this.property["hidden"] &&
|
||||
["show-block", "require-answer"].indexOf(val) === -1) ||
|
||||
(this.property["required"] !== undefined &&
|
||||
this.property["required"] &&
|
||||
["make-it-optional", "hide-block", "disable-block"].indexOf(val) ===
|
||||
-1) ||
|
||||
(this.property["disabled"] !== undefined &&
|
||||
this.property["disabled"] &&
|
||||
["enable-block", "require-answer", "make-it-optional"].indexOf(
|
||||
val,
|
||||
) === -1)
|
||||
) {
|
||||
this.isActionCorrect = false
|
||||
return
|
||||
@@ -117,7 +148,6 @@ class FormPropertyLogicRule {
|
||||
this.isActionCorrect = false
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default FormPropertyLogicRule
|
||||
export default FormPropertyLogicRule
|
||||
|
||||
Reference in New Issue
Block a user