2025-12-03 11:42:15 -05:00
|
|
|
import { Controller } from "@hotwired/stimulus"
|
|
|
|
|
|
|
|
|
|
export default class extends Controller {
|
|
|
|
|
static values = {
|
|
|
|
|
formColor: { type: Array, default: [] } // Declares 'items' as an Array value
|
|
|
|
|
}
|
|
|
|
|
static targets = ["template", "container", "plan", "button"]
|
|
|
|
|
|
|
|
|
|
add(event) {
|
2025-12-10 13:22:33 -05:00
|
|
|
console.log("start")
|
2025-12-03 11:42:15 -05:00
|
|
|
event.preventDefault()
|
|
|
|
|
event.stopPropagation()
|
|
|
|
|
|
|
|
|
|
const content = this.#updateTemplatePlan()
|
|
|
|
|
|
|
|
|
|
this.buttonTarget.insertAdjacentHTML("beforebegin", content)
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-15 11:37:50 -05:00
|
|
|
remove(event) {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
const wrapper = event.target.closest(".plan-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";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-03 11:42:15 -05:00
|
|
|
#updateTemplatePlan() {
|
|
|
|
|
const nextIndex = this.planTargets.length
|
|
|
|
|
const num_of_colors = this.formColorValue.length
|
|
|
|
|
let colorIndex = 0
|
2025-12-10 13:22:33 -05:00
|
|
|
let newSecondaryColor = "copper"
|
2025-12-03 11:42:15 -05:00
|
|
|
if (nextIndex != 0) {
|
|
|
|
|
colorIndex = nextIndex % num_of_colors
|
2025-12-10 13:22:33 -05:00
|
|
|
if (nextIndex % 2 == 1) {
|
|
|
|
|
newSecondaryColor = "bronze"
|
|
|
|
|
}
|
2025-12-03 11:42:15 -05:00
|
|
|
}
|
|
|
|
|
const newColor = this.formColorValue[colorIndex]
|
|
|
|
|
return this.templateTarget.innerHTML
|
|
|
|
|
.replace(/NEW_RECORD/g, nextIndex)
|
|
|
|
|
.replace(/NEW_PLAN/g, nextIndex + 1)
|
|
|
|
|
.replace(/NEXT_COLOR/g, newColor)
|
2025-12-10 13:22:33 -05:00
|
|
|
.replace(/NEXT_SECONDARY_COLOR/g, newSecondaryColor)
|
2025-12-03 11:42:15 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|