From bde31aa89103364300559c2eaac2d0077d5319c3 Mon Sep 17 00:00:00 2001 From: Shaun O'Connell Date: Sun, 11 Feb 2024 16:42:35 +1300 Subject: [PATCH 1/3] Switch to templates for HTML authoring --- assets/mjs/wc/game-controls.mjs | 60 +++++++++++++-------------------- index.html | 20 +++++++++-- 2 files changed, 41 insertions(+), 39 deletions(-) diff --git a/assets/mjs/wc/game-controls.mjs b/assets/mjs/wc/game-controls.mjs index 90cbb0a..65ab00d 100644 --- a/assets/mjs/wc/game-controls.mjs +++ b/assets/mjs/wc/game-controls.mjs @@ -4,63 +4,49 @@ import { MODEL } from './../model.mjs'; export default class GameControls extends GameElement { #formElements = MODEL; - createFormControlWrapper() { - let wrapper = document.createElement('div'); - wrapper.className = 'control'; - - return wrapper; - } - - createFormControlLabel(labelValue, controlId) { - let label = document.createElement('label'); + createRangeControl(keyName, modelValue) { + let controlId = `rng_${keyName}`; + let wrapper = this.querySelector('#tmpl_control_range').content.cloneNode(true); + let label = wrapper.querySelector('label'); + label.textContent = modelValue.name; label.setAttribute('for', controlId); - label.textContent = labelValue; - return label; - } + let input = wrapper.querySelector('input'); + input.min = modelValue.min; + input.max = modelValue.max; + input.name = keyName; + input.id = controlId; + input.value = modelValue.initial; + + let output = wrapper.querySelector('output'); + output.setAttribute('for', keyName); + output.name = `result_${keyName}`; - createRangeControl(keyName, modelValue) { - let wrapper = this.createFormControlWrapper(); - let br = document.createElement('br'); - let label = this.createFormControlLabel(modelValue.name, `rng_${keyName}`); - let input = document.createElement('input'); - - input.setAttribute('type', 'range'); - input.setAttribute('min', modelValue.min); - input.setAttribute('max', modelValue.max); - input.setAttribute('name', keyName); - input.setAttribute('id', `rng_${keyName}`); - input.setAttribute('value', modelValue.initial); - - wrapper.appendChild(label); - wrapper.appendChild(br); - wrapper.appendChild(input); return wrapper; } createRadioControl(keyName, modelValue, state) { let controlId = `rdo_${keyName}_${state}`; let labelValue = state === 'true' ? modelValue.labelTrue : modelValue.labelFalse; - let label = this.createFormControlLabel(labelValue, controlId); - let input = document.createElement('input'); - - input.setAttribute('type', 'radio'); - input.setAttribute('name', controlId); - input.setAttribute('value', state); + let label = this.querySelector('#tmpl_control_radio').content.children[0].cloneNode(true); + label.setAttribute('for', controlId); + label.append(labelValue); + let input = label.querySelector('input'); + input.name = keyName; + input.id = controlId; + input.value = state; if (modelValue.initial === state) { input.setAttribute('checked', 'checked'); } - label.prepend(input); - return label; } createRadioControlGroup(keyName, modelValue) { if (modelValue.type === 'boolean') { - let wrapper = this.createFormControlWrapper(); + let wrapper = this.querySelector('#tmpl_control_boolean').content.children[0].cloneNode(true); ['true', 'false'].forEach((state) => { wrapper.appendChild(this.createRadioControl(keyName, modelValue, state)); diff --git a/index.html b/index.html index 764abb6..3f18a14 100644 --- a/index.html +++ b/index.html @@ -15,7 +15,23 @@ - + -->