Files
baclight/app/javascript/controllers/add_alt_network_logo_controller.js
T

38 lines
1.3 KiB
JavaScript
Raw Normal View History

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", "networkLogo", "button"]
add(event) {
event.preventDefault()
event.stopPropagation()
const content = this.#updateTemplateNetwork()
this.buttonTarget.insertAdjacentHTML('beforebegin', content);
// this.containerTarget.insertAdjacentHTML("beforeend", content)
}
#updateTemplateNetwork() {
const nextIndex = this.networkLogoTargets.length
const num_of_colors = this.formColorValue.length
let colorIndex = 0
let newSecondaryColor = "copper"
if (nextIndex != 0) {
colorIndex = nextIndex % num_of_colors
if (nextIndex % 2 == 1) {
newSecondaryColor = "bronze"
}
}
const newColor = this.formColorValue[colorIndex]
return this.templateTarget.innerHTML
.replace(/NEW_RECORD/g, nextIndex)
.replace(/NEXT_COLOR/g, newColor)
.replace(/NEXT_SECONDARY_COLOR/g, newSecondaryColor)
}
}