Files
baclight/app/javascript/controllers/add_exception_controller.js
T
2026-03-03 22:53:21 -05:00

53 lines
1.6 KiB
JavaScript

import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static values = {
formColor: { type: Array, default: [] } // Declares 'items' as an Array value
}
static targets = ["exceptionTemplate", "exceptionContainer", "exception", "exceptionButton"]
connect(){
console.log(this.formColorValue)
}
addExemption(event) {
event.preventDefault()
event.stopPropagation()
const content = this.#newExemption()
this.exceptionButtonTarget.insertAdjacentHTML('beforebegin', content);
// this.containerTarget.insertAdjacentHTML("beforeend", content)
}
removeExemption(event) {
event.preventDefault();
const wrapper = event.target.closest(".network-item");
if (wrapper.dataset.newRecord === "true") {
wrapper.remove();
} else {
wrapper.style.display = "none";
const destroyInput = wrapper.querySelector("input[name*='[_destroy]']");
if (destroyInput) {
destroyInput.value = "1";
}
}
}
#newExemption() {
const nextIndex = this.exceptionTargets.length
const num_of_colors = this.formColorValue.length
let colorIndex = 0
if (nextIndex != 0) {
colorIndex = nextIndex % num_of_colors
}
const newColor = this.formColorValue[colorIndex]
return this.exceptionTemplateTarget.innerHTML
.replace(/NEW_RECORD/g, nextIndex)
.replace(/NEXT_COLOR/g, newColor)
}
}