|
| 1 | +import { cloneSwatch, normalizeHex } from "../modules/paletteUtils.js"; |
| 2 | + |
| 3 | +export class PaletteHarmonyControl { |
| 4 | + constructor({ documentRef, refs, app }) { |
| 5 | + this.document = documentRef; |
| 6 | + this.refs = refs; |
| 7 | + this.app = app; |
| 8 | + } |
| 9 | + |
| 10 | + bind() { |
| 11 | + this.renderOptions(); |
| 12 | + this.refs.harmonyMatchSourceSelect.addEventListener("change", () => { |
| 13 | + this.app.setHarmonyMatchSource(this.refs.harmonyMatchSourceSelect.value); |
| 14 | + }); |
| 15 | + this.refs.harmonySchemeSelect.addEventListener("change", () => { |
| 16 | + this.app.setHarmonyScheme(this.refs.harmonySchemeSelect.value); |
| 17 | + }); |
| 18 | + this.refs.addSelectedHarmonyButton.addEventListener("click", () => { |
| 19 | + this.app.addSelectedHarmonyColor(); |
| 20 | + }); |
| 21 | + this.refs.addAllHarmonyButton.addEventListener("click", () => { |
| 22 | + this.app.addAllHarmonyColors(); |
| 23 | + }); |
| 24 | + } |
| 25 | + |
| 26 | + renderOptions() { |
| 27 | + this.refs.harmonyMatchSourceSelect.replaceChildren(); |
| 28 | + this.app.getHarmonyMatchSourceOptions().forEach((option) => { |
| 29 | + const element = this.document.createElement("option"); |
| 30 | + element.value = option.value; |
| 31 | + element.textContent = option.label; |
| 32 | + this.refs.harmonyMatchSourceSelect.appendChild(element); |
| 33 | + }); |
| 34 | + |
| 35 | + this.refs.harmonySchemeSelect.replaceChildren(); |
| 36 | + this.app.getHarmonySchemeOptions().forEach((option) => { |
| 37 | + const element = this.document.createElement("option"); |
| 38 | + element.value = option.value; |
| 39 | + element.textContent = option.label; |
| 40 | + this.refs.harmonySchemeSelect.appendChild(element); |
| 41 | + }); |
| 42 | + } |
| 43 | + |
| 44 | + render() { |
| 45 | + this.refs.harmonyMatchSourceSelect.value = this.app.getHarmonyMatchSource(); |
| 46 | + this.refs.harmonySchemeSelect.value = this.app.getHarmonyScheme(); |
| 47 | + this.renderParameterControls(); |
| 48 | + this.renderHarmonyColors(); |
| 49 | + } |
| 50 | + |
| 51 | + renderParameterControls() { |
| 52 | + this.refs.harmonyParameterControls.replaceChildren(); |
| 53 | + const parameter = this.app.getHarmonySchemeParameter(); |
| 54 | + if (!parameter) { |
| 55 | + return; |
| 56 | + } |
| 57 | + const label = this.document.createElement("label"); |
| 58 | + label.className = "palette-manager-v2__field palette-manager-v2__field--compact"; |
| 59 | + label.textContent = parameter.label; |
| 60 | + |
| 61 | + const input = this.document.createElement("input"); |
| 62 | + input.id = "harmonyParameterInput"; |
| 63 | + input.type = "number"; |
| 64 | + input.min = String(parameter.min); |
| 65 | + input.max = String(parameter.max); |
| 66 | + input.step = String(parameter.step); |
| 67 | + input.value = String(this.app.getHarmonyParameterValue()); |
| 68 | + input.addEventListener("input", () => { |
| 69 | + this.app.setHarmonyParameterValue(input.value); |
| 70 | + }); |
| 71 | + |
| 72 | + label.appendChild(input); |
| 73 | + this.refs.harmonyParameterControls.appendChild(label); |
| 74 | + } |
| 75 | + |
| 76 | + renderHarmonyColors() { |
| 77 | + this.refs.harmonyColorList.replaceChildren(); |
| 78 | + const colors = this.app.getHarmonyColors(); |
| 79 | + this.refs.addSelectedHarmonyButton.disabled = colors.length === 0; |
| 80 | + this.refs.addAllHarmonyButton.disabled = colors.length === 0; |
| 81 | + |
| 82 | + if (!this.app.getSelectedSwatch()) { |
| 83 | + const empty = this.document.createElement("p"); |
| 84 | + empty.className = "palette-manager-v2__meta"; |
| 85 | + empty.textContent = "Select a user or source swatch."; |
| 86 | + this.refs.harmonyColorList.appendChild(empty); |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + if (colors.length === 0) { |
| 91 | + const empty = this.document.createElement("p"); |
| 92 | + empty.className = "palette-manager-v2__meta"; |
| 93 | + empty.textContent = "No harmony colors could be generated."; |
| 94 | + this.refs.harmonyColorList.appendChild(empty); |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + colors.forEach((color, index) => { |
| 99 | + this.refs.harmonyColorList.appendChild(this.createHarmonyColorButton(color, index)); |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + createHarmonyColorButton(color, index) { |
| 104 | + const cleanSwatch = cloneSwatch(color.swatch || {}); |
| 105 | + const button = this.document.createElement("button"); |
| 106 | + button.type = "button"; |
| 107 | + button.className = "palette-manager-v2__harmony-color"; |
| 108 | + button.classList.toggle("is-selected", index === this.app.getSelectedHarmonyColorIndex()); |
| 109 | + button.setAttribute("aria-pressed", String(index === this.app.getSelectedHarmonyColorIndex())); |
| 110 | + button.dataset.harmonyIndex = String(index); |
| 111 | + button.dataset.harmonyHex = normalizeHex(color.hex); |
| 112 | + button.title = `${color.name}: ${normalizeHex(color.hex)}`; |
| 113 | + button.addEventListener("click", () => { |
| 114 | + this.app.setSelectedHarmonyColorIndex(index); |
| 115 | + }); |
| 116 | + |
| 117 | + const chip = this.document.createElement("span"); |
| 118 | + chip.className = "palette-manager-v2__harmony-chip"; |
| 119 | + chip.style.background = normalizeHex(color.hex); |
| 120 | + chip.setAttribute("aria-hidden", "true"); |
| 121 | + |
| 122 | + const text = this.document.createElement("span"); |
| 123 | + text.className = "palette-manager-v2__harmony-text"; |
| 124 | + text.textContent = cleanSwatch.name || color.name || normalizeHex(color.hex); |
| 125 | + |
| 126 | + const meta = this.document.createElement("span"); |
| 127 | + meta.className = "palette-manager-v2__harmony-meta"; |
| 128 | + meta.textContent = normalizeHex(color.hex); |
| 129 | + |
| 130 | + button.append(chip, text, meta); |
| 131 | + return button; |
| 132 | + } |
| 133 | +} |
0 commit comments