(function () {
function applyTheme() {
try {
const isDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
document.documentElement.setAttribute('data-bs-theme', isDark ? 'dark' : 'light');
if(isDark){
document
.querySelector('#layui_theme_css')
.setAttribute(
'href',
`https://dpangzi.com/core/lib/layui/css/layui-theme-dark.css`
);
}
} catch (_) {
}
}
applyTheme();
if (window.matchMedia) {
const mql = window.matchMedia('(prefers-color-scheme: dark)');
if (mql.addEventListener) {
mql.addEventListener('change', applyTheme);
} else if (mql.addListener) {
mql.addListener(applyTheme);
}
}
})();
window.dpz = window.dpz || {};
window.dpz.openActionDialog = function (config) {
const template = document.getElementById('dpzActionDialogTemplate');
if (!template) {
console.warn('dpzActionDialogTemplate not found');
return;
}
const fragment = template.content.cloneNode(true);
const overlay = fragment.querySelector('.dpz-modal-overlay');
const titleEl = overlay.querySelector('.dpz-modal-title');
const descEl = overlay.querySelector('.dpz-modal-desc');
const bodyEl = overlay.querySelector('.dpz-modal-body');
const cancelBtn = overlay.querySelector('.dpz-btn-cancel');
const confirmBtn = overlay.querySelector('.dpz-btn-confirm');
const {
title = '操作确认',
description = '',
confirmText = '确认',
cancelText = '取消',
fields = [],
onConfirm,
} = config || {};
titleEl.textContent = title;
if (description) {
descEl.innerHTML = description;
} else {
descEl.textContent = '';
descEl.classList.add('visually-hidden');
}
confirmBtn.textContent = confirmText;
cancelBtn.textContent = cancelText;
const inputRefs = [];
const normalizedFields = Array.isArray(fields) ? fields : [];
normalizedFields.forEach(field => {
const wrapper = document.createElement('div');
wrapper.className = 'dpz-modal-field';
if (field.label) {
const label = document.createElement('label');
label.className = 'dpz-modal-label';
label.setAttribute('for', field.id);
label.textContent = field.label;
wrapper.appendChild(label);
}
let input;
if (field.type === 'select') {
input = document.createElement('select');
input.className = 'dpz-modal-select';
(field.options ?? []).forEach(option => {
const optionEl = document.createElement('option');
optionEl.value = option.value ?? '';
optionEl.textContent = option.label ?? option.text ?? optionEl.value;
if (String(optionEl.value) === String(field.value ?? '')) {
optionEl.selected = true;
}
input.appendChild(optionEl);
});
} else {
input = document.createElement('input');
input.className = 'dpz-modal-input';
input.type = field.type === 'pin' ? 'text' : (field.inputType || 'text');
if (field.type === 'pin') {
input.inputMode = 'numeric';
input.pattern = '\\d{6}';
input.maxLength = 6;
}
if (typeof field.value !== 'undefined') {
input.value = field.value;
}
}
input.dataset.fieldId = field.id;
if (field.placeholder) {
input.placeholder = field.placeholder;
}
if (field.autoComplete) {
input.autocomplete = field.autoComplete;
}
wrapper.appendChild(input);
if (field.help) {
const helper = document.createElement('div');
helper.className = 'dpz-modal-helper';
helper.innerHTML = field.help;
wrapper.appendChild(helper);
}
bodyEl.appendChild(wrapper);
inputRefs.push({ field, input });
});
document.body.appendChild(overlay);
function close() {
overlay.remove();
}
cancelBtn?.addEventListener('click', close);
overlay.addEventListener('click', event => {
if (event.target === overlay) {
close();
}
});
overlay.addEventListener('keydown', event => {
if (event.key === 'Escape') {
close();
}
});
confirmBtn?.addEventListener('click', async () => {
const values = {};
for (const { field, input } of inputRefs) {
const rawValue = field.type === 'select' ? input.value : input.value.trim();
const value = rawValue ?? '';
if (field.required && !value) {
window.layer?.msg?.(field.requiredMessage || `请填写 ${field.label ?? ''}`);
input.focus();
return;
}
if (typeof field.validator === 'function') {
const result = field.validator(value);
if (result !== true) {
window.layer?.msg?.(result || '输入不合法');
input.focus();
return;
}
}
values[field.id] = value;
}
if (typeof onConfirm !== 'function') {
close();
return;
}
confirmBtn.disabled = true;
const originalText = confirmBtn.textContent;
confirmBtn.textContent = '处理中...';
try {
const success = await onConfirm(values);
if (success !== false) {
close();
} else {
confirmBtn.disabled = false;
confirmBtn.textContent = originalText;
if (inputRefs[0]?.input) {
inputRefs[0].input.focus();
}
}
} catch (error) {
console.warn('dpz action dialog confirm error', error);
confirmBtn.disabled = false;
confirmBtn.textContent = originalText;
}
});
if (inputRefs[0]?.input) {
inputRefs[0].input.focus();
}
};