You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
1.6 KiB
56 lines
1.6 KiB
3 years ago
|
import { Controller } from "@hotwired/stimulus"
|
||
|
|
||
|
export default class extends Controller {
|
||
|
initialize() {
|
||
|
this.displayConditional()
|
||
|
}
|
||
|
|
||
|
displayConditional() {
|
||
3 years ago
|
switch(this.element.type) {
|
||
|
case "number":
|
||
|
this.displayConditionalNumeric()
|
||
|
case "radio":
|
||
|
this.displayConditionalRadio()
|
||
|
default:
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
displayConditionalRadio() {
|
||
3 years ago
|
if(this.element.checked) {
|
||
3 years ago
|
let selectedValue = this.element.value
|
||
3 years ago
|
let conditional_for = JSON.parse(this.element.dataset.info)
|
||
|
|
||
3 years ago
|
Object.entries(conditional_for).map(([targetQuestion, conditions]) => {
|
||
3 years ago
|
let div = document.getElementById(targetQuestion + "_div")
|
||
|
if(conditions.includes(selectedValue)) {
|
||
3 years ago
|
div.style.display = "block"
|
||
|
} else {
|
||
|
div.style.display = "none"
|
||
3 years ago
|
let buttons = document.getElementsByName(`case_log[${targetQuestion}]`);
|
||
|
if (buttons.length == 0){
|
||
|
buttons = document.getElementsByName(`case_log[${targetQuestion}][]`);
|
||
|
}
|
||
3 years ago
|
Object.entries(buttons).map(([idx, button]) => {
|
||
3 years ago
|
button.checked = false;
|
||
|
})
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|
||
3 years ago
|
|
||
|
displayConditionalNumeric() {
|
||
|
let enteredValue = this.element.value
|
||
|
let conditional_for = JSON.parse(this.element.dataset.info)
|
||
|
|
||
3 years ago
|
Object.entries(conditional_for).map(([targetQuestion, condition]) => {
|
||
3 years ago
|
let div = document.getElementById(targetQuestion + "_div")
|
||
|
if(eval((enteredValue + condition))) {
|
||
|
div.style.display = "block"
|
||
|
} else {
|
||
|
div.style.display = "none"
|
||
|
}
|
||
|
})
|
||
|
}
|
||
3 years ago
|
}
|