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.
20 lines
848 B
20 lines
848 B
3 years ago
|
import { Controller } from "@hotwired/stimulus"
|
||
|
|
||
|
export default class extends Controller {
|
||
3 years ago
|
connect() {
|
||
|
const affectedField = this.element.dataset.target;
|
||
|
const targetQuestion = affectedField.split("case-log-")[1].split("-field")[0]
|
||
|
const div = document.getElementById(targetQuestion + "_div");
|
||
|
div.style.display = "block";
|
||
|
}
|
||
|
|
||
3 years ago
|
calculateFields() {
|
||
|
const affectedField = this.element.dataset.target;
|
||
3 years ago
|
const fieldsToAdd = JSON.parse(this.element.dataset.calculated).map(x => `case-log-${x.replaceAll("_","-")}-field`);
|
||
3 years ago
|
const valuesToAdd = fieldsToAdd.map(x => document.getElementById(x).value).filter(x => x);
|
||
3 years ago
|
const newValue = valuesToAdd.map(x => parseFloat(x)).reduce((a, b) => a + b, 0).toFixed(2);
|
||
3 years ago
|
const elementToUpdate = document.getElementById(affectedField);
|
||
|
elementToUpdate.value = newValue;
|
||
|
}
|
||
|
}
|