import './css/checkbox.scss'; import { createElement } from "../functions"; import { createIcon } from "./icon"; import { isPositive, nullOrEmpty } from '../utility'; /** @typedef {import('../types').CheckboxOptions} CheckboxOptions */ /** * @private * @param {HTMLElement} container * @param {string} [type="fa-regular"] * @param {string | HTMLElement} [label] * @param {number} [tabindex] * @param {string} [title] * @param {string} [charactor] */ function fillCheckbox(container, type = 'fa-regular', label, tabindex = -1, title, charactor = 'check') { const checkIcon = createIcon(type, charactor); checkIcon.classList.add('ui-check-icon'); const indeterminateIcon = createIcon(type, 'minus'); indeterminateIcon.classList.add('ui-indeterminate-icon') container.appendChild( createElement('layer', layer => { layer.className = 'ui-check-inner'; layer.addEventListener('keypress', e => { if (e.key === ' ' || e.key === 'Enter') { const input = container.querySelector('input'); if (input != null) { input.checked = !input.checked; input.dispatchEvent(new Event('change')); } } }); if (tabindex >= 0) { layer.tabIndex = tabindex; } }, checkIcon, indeterminateIcon) ); if (label instanceof HTMLElement) { container.appendChild(label); } else if (label != null && String(label).length > 0) { container.appendChild( createElement('span', span => { span.innerText = label; if (title != null) { span.title = title; } }) ); } } /** * 创建一个单选框 * @param {CheckboxOptions} opts * @returns {HTMLLabelElement} */ export function createRadiobox(opts = {}) { const container = createElement('label', 'ui-check-wrapper ui-radio-wrapper', createElement('input', input => { input.setAttribute('type', 'radio'); if (!nullOrEmpty(opts.name)) { input.name = opts.name; } if (opts.checked === true) { input.checked = true; } if (opts.enabled === false) { input.disabled = true; } if (opts.customAttributes != null) { for (let entry of Object.entries(opts.customAttributes)) { input.setAttribute(entry[0], entry[1]); } } if (typeof opts.onchange === 'function') { input.addEventListener('change', opts.onchange); } })); if (opts.className) { container.classList.add(opts.className); } fillCheckbox(container, opts.type, opts.label, opts.tabIndex, opts.title, 'circle'); return container; } /** * 创建一个复选框 * @param {CheckboxOptions} opts * @returns {HTMLLabelElement} */ export function createCheckbox(opts = {}) { const container = createElement('label', opts.switch ? 'ui-switch' : 'ui-check-wrapper', createElement('input', input => { input.setAttribute('type', 'checkbox'); if (opts.checked === true) { input.checked = true; } if (opts.indeterminate === true) { input.indeterminate = true; } if (opts.enabled === false) { input.disabled = true; } if (opts.customAttributes != null) { for (let entry of Object.entries(opts.customAttributes)) { input.setAttribute(entry[0], entry[1]); } } if (typeof opts.onchange === 'function') { input.addEventListener('change', opts.onchange); } })); if (opts.className) { container.classList.add(opts.className); } if (opts.enabled === false) { container.classList.add('disabled'); } if (opts.switch) { const label = opts.label; if (label instanceof Element) { container.appendChild(label); } else { container.appendChild( createElement('span', span => { if (label != null && String(label).length > 0) { span.innerText = label; } if (opts.title != null) { span.title = opts.title; } }) ); } } else if (opts.checkedNode != null && opts.uncheckedNode != null) { container.classList.add('ui-check-image-wrapper'); let height = opts.imageHeight; if (!isPositive(height)) { height = 14; } opts.checkedNode.classList.add('checked'); container.appendChild(opts.checkedNode); opts.uncheckedNode.classList.add('unchecked'); container.appendChild(opts.uncheckedNode); } else { fillCheckbox(container, opts.type, opts.label, opts.tabIndex, opts.title); } return container; } /** * 解析容器元素内符合条件的子元素为复选框元素 * @param {HTMLElement} container * @param {boolean} [legacy] * @returns {HTMLElement} */ export function resolveCheckbox(container = document.body, legacy) { if (legacy) { const checks = container.querySelectorAll('input[type="checkbox"]'); for (let chk of checks) { if (!(chk instanceof HTMLInputElement) || chk.parentElement?.classList.contains('ui-check-wrapper')) { // skip continue; } const id = chk.id; let label, text; if (id != null) { label = container.querySelector(`label[for="${id}"]`); } if (label == null) { const e = chk.nextElementSibling; if (e instanceof HTMLElement) { if (e.tagName === 'LABEL') { label = e; } else if (e.tagName === 'SPAN' && e.dataset.lgid != null) { text = e.innerText; e.style.display = 'none'; } } } if (label == null) { const e = chk.previousElementSibling; if (e instanceof HTMLElement) { if (e.tagName === 'LABEL') { label = e; } else if (text == null && e.tagName === 'SPAN' && e.dataset.lgid != null) { text = e.innerText; e.style.display = 'none'; } } } if (!(label instanceof HTMLElement)) { label = createElement('label'); chk.parentElement?.insertBefore(label, chk); } else { text = label.innerText; } if (chk.disabled) { label.className = 'ui-check-wrapper disabled'; } else { label.className = 'ui-check-wrapper'; } label.replaceChildren(); fillCheckbox(label, 'fa-regular', text, chk.tabIndex, label.title); label.insertBefore(chk, label.firstChild); } } const boxes = container.querySelectorAll('label[data-checkbox]'); for (let box of boxes) { if (!(box instanceof HTMLLabelElement)) { continue; } if (!box.classList.contains('ui-check-wrapper')) { box.classList.add('ui-check-wrapper'); } if (box.hasChildNodes()) { if (!box.classList.contains('ui-check-image-wrapper')) { box.classList.add('ui-check-image-wrapper'); } } else { fillCheckbox(box, box.dataset.type, box.dataset.label, Number(box.dataset.tabIndex), box.title); box.removeAttribute('title'); box.removeAttribute('data-type'); box.removeAttribute('data-label'); } const input = createElement('input'); const id = box.dataset.id; if (!nullOrEmpty(id)) { input.id = id; } if (box.dataset.checked != null) { input.checked = true; } input.setAttribute('type', 'checkbox'); box.insertBefore(input, box.firstChild); } return container; }