Files
upload/static/js/form-components.js

151 lines
7.3 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* FormComponents — 表单字段组件库(create/edit 左栏调色板)
*
* 用法:
* <script src="/static/js/form-components.js"></script>
* <script>
* FormComponents.renderPalette(
* document.getElementById('componentPalette'),
* (type) => addField({ type }) // 点击组件时回调
* );
* </script>
*
* 组件瓦片可点击添加,也可拖拽:dragstart 写入
* dataTransfer['application/x-field-type'] = type,
* 由 create/edit 页面字段列表的 drop 事件读取(拖到指定位置插入)。
*/
(function () {
'use strict';
if (window.FormComponents) return;
const ICONS = {
text: '<path d="M4 7V5h16v2M12 5v14M9 19h6"/>',
textarea: '<path d="M4 6h16M4 10h16M4 14h10"/><rect x="3" y="3" width="18" height="18" rx="3" fill="none"/>',
select: '<path d="M3 6h18M3 12h18M3 18h10"/><path d="M16 15l2 2 4-4"/>',
radio: '<circle cx="12" cy="12" r="9" fill="none"/><circle cx="12" cy="12" r="4"/>',
checkbox: '<rect x="3" y="3" width="18" height="18" rx="4" fill="none"/><path d="M8 12l3 3 5-6"/>',
rating: '<path d="M12 3l2.7 5.5 6 .9-4.3 4.2 1 6-5.4-2.8L6.6 19.6l1-6L3.3 9.4l6-.9L12 3z"/>',
number: '<path d="M6 5v14M18 5v14M6 12h12"/>',
date: '<rect x="3" y="5" width="18" height="16" rx="2" fill="none"/><path d="M3 10h18M8 3v4M16 3v4"/>',
time: '<circle cx="12" cy="12" r="9" fill="none"/><path d="M12 7v5l3 3"/>',
file: '<path d="M12 16V4M12 4L8 8M12 4l4 4M4 17v1a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2v-1"/>',
slider: '<path d="M3 12h18"/><circle cx="9" cy="12" r="3" fill="none"/><circle cx="17" cy="12" r="3" fill="none"/>',
page_break: '<rect x="3" y="4" width="18" height="6" rx="1.5"/><rect x="3" y="14" width="18" height="6" rx="1.5"/><path d="M12 10v4"/>',
};
const CATALOG = [
{ type: 'text', label: '文本', hint: '短文本输入' },
{ type: 'textarea', label: '多行文本', hint: '长文本输入' },
{ type: 'number', label: '数字', hint: '数字输入' },
{ type: 'select', label: '下拉框', hint: '单选下拉' },
{ type: 'radio', label: '单选', hint: '多个选项选一个' },
{ type: 'checkbox', label: '多选', hint: '多个选项可多选' },
{ type: 'date', label: '日期', hint: '日期选择' },
{ type: 'time', label: '时间', hint: '时间选择' },
{ type: 'rating', label: '评分', hint: '星级评分' },
{ type: 'slider', label: '滑块', hint: '范围取值' },
{ type: 'file', label: '文件上传', hint: '附件收集' },
{ type: 'page_break', label: '分页', hint: '从这里开始新的一页' },
];
const CSS = `
.fc-palette { display: grid; grid-template-columns: repeat(4, minmax(0,1fr)); gap: 8px; }
@media (max-width: 480px) { .fc-palette { grid-template-columns: repeat(3, minmax(0,1fr)); } }
.fc-tile {
display: flex; flex-direction: column; align-items: center; gap: 6px;
padding: 12px 6px 10px; border-radius: 12px; cursor: grab;
border: 1px solid var(--border-color);
background: transparent;
width: 100%; color: var(--text-secondary); font: inherit; text-align: left;
transition: border-color .18s ease, background-color .18s ease, transform .18s ease;
user-select: none; -webkit-user-select: none;
}
.fc-tile:focus-visible { outline: 2px solid var(--border-focus); outline-offset: 2px; }
.fc-tile:hover {
border-color: var(--accent-bg, #6366f1);
background: color-mix(in srgb, var(--accent-bg, #6366f1) 8%, transparent);
transform: translateY(-1px);
}
.fc-tile:active { cursor: grabbing; }
.fc-tile svg { width: 20px; height: 20px; stroke: var(--text-secondary); fill: none;
stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.fc-tile:hover svg { stroke: var(--accent-bg, #6366f1); }
.fc-tile .fc-label { font-size: 11px; color: var(--text-secondary); white-space: nowrap; }
`;
function tileHtml(item) {
return `
<button type="button" class="fc-tile" draggable="true" data-field-type="${item.type}" title="${item.hint}"
aria-label="${item.type === 'page_break' ? '插入分页控件' : `添加${item.label}字段`}">
<svg viewBox="0 0 24 24">${ICONS[item.type] || ICONS.text}</svg>
<span class="fc-label">${item.label}</span>
</button>
`;
}
window.FormComponents = {
CATALOG,
createPageBreakCard(fieldId, title, onRemove) {
const card = document.createElement('div');
card.className = 'field-card page-break-card';
card.id = fieldId;
card.innerHTML = `
<div class="flex items-center justify-between gap-3 mb-3">
<div class="flex items-center gap-2">
<button type="button" class="drag-handle" title="拖拽排序" aria-label="拖动以调整分页顺序">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<circle cx="9" cy="5" r="1"/><circle cx="9" cy="12" r="1"/><circle cx="9" cy="19" r="1"/>
<circle cx="15" cy="5" r="1"/><circle cx="15" cy="12" r="1"/><circle cx="15" cy="19" r="1"/>
</svg>
</button>
<span class="page-break-number page-break-label">分页</span>
</div>
<div class="field-card-actions flex items-center justify-end gap-2">
<button type="button" class="page-break-remove btn-danger w-7 h-7 rounded-full flex items-center justify-center text-xs" aria-label="删除分页控件">×</button>
</div>
</div>
<label class="block text-xs mb-2" style="color:var(--text-secondary)">下一页标题(可选)</label>
<input type="text" class="field-edit-input page-title w-full" placeholder="例如:上传材料">
<p class="text-xs mt-3" style="color:var(--text-muted)">填写者完成当前页后,点击“下一页”进入这里。</p>
<input type="hidden" class="field-type" value="page_break">
`;
const input = card.querySelector('.page-title');
input.id = `${fieldId}_page_title`;
input.value = title || '';
card.querySelector('label').htmlFor = input.id;
card.querySelector('.page-break-remove').addEventListener('click', onRemove);
return card;
},
/* 渲染组件调色板。onAdd(type):点击组件回调;拖拽落位由字段列表处理。 */
renderPalette(container, onAdd) {
if (!container) return;
if (!document.getElementById('fc-palette-style')) {
const style = document.createElement('style');
style.id = 'fc-palette-style';
style.textContent = CSS;
document.head.appendChild(style);
}
container.classList.add('fc-palette');
container.innerHTML = CATALOG.map(tileHtml).join('');
container.querySelectorAll('.fc-tile').forEach(tile => {
const type = tile.dataset.fieldType;
tile.addEventListener('click', () => { if (onAdd) onAdd(type); });
tile.addEventListener('dragstart', (e) => {
e.dataTransfer.setData('application/x-field-type', type);
e.dataTransfer.setData('text/plain', 'fc:' + type);
e.dataTransfer.effectAllowed = 'copy';
tile.classList.add('fc-dragging');
});
tile.addEventListener('dragend', () => tile.classList.remove('fc-dragging'));
});
},
};
})();