初版:上传接口(从线上实例导出,适配容器端口)
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
/* URL question numbers follow the displayed order; page breaks have no number. */
|
||||
(function () {
|
||||
'use strict';
|
||||
const supported = new Set(['text', 'textarea', 'number', 'select', 'radio', 'checkbox', 'date', 'time', 'rating', 'slider']);
|
||||
|
||||
function parse(fields, search) {
|
||||
const questions = fields.filter(field => field.type !== 'page_break');
|
||||
const params = new URLSearchParams(search);
|
||||
const locked = new Map();
|
||||
const hidden = new Set();
|
||||
const invalid = new Set();
|
||||
for (const key of new Set(params.keys())) {
|
||||
if (!/^[1-9]\d*$/.test(key)) continue;
|
||||
const field = questions[Number(key) - 1];
|
||||
if (!field || !supported.has(field.type)) continue;
|
||||
const values = params.getAll(key);
|
||||
let value = values[values.length - 1];
|
||||
if (['select', 'radio', 'checkbox'].includes(field.type)) {
|
||||
const options = (field.options || '').split(',').map(option => option.trim()).filter(Boolean);
|
||||
const selected = field.type === 'checkbox'
|
||||
? [...new Set(values.flatMap(item => item.split(',')).map(item => item.trim()).filter(Boolean))]
|
||||
: [value];
|
||||
if ((field.required && !selected.length) || selected.some(item => !options.includes(item))) {
|
||||
invalid.add(field.name);
|
||||
continue;
|
||||
}
|
||||
value = field.type === 'checkbox' ? selected : value;
|
||||
} else if (field.required && !value.trim()) {
|
||||
invalid.add(field.name);
|
||||
continue;
|
||||
}
|
||||
locked.set(field.name, { field, value });
|
||||
}
|
||||
for (const item of params.getAll('hidden').flatMap(value => value.split(','))) {
|
||||
const number = item.trim();
|
||||
if (!/^[1-9]\d*$/.test(number)) continue;
|
||||
const field = questions[Number(number) - 1];
|
||||
if (field) hidden.add(field.name);
|
||||
}
|
||||
return { locked, hidden, invalid };
|
||||
}
|
||||
|
||||
function apply(preset, getWrapper, hooks = {}) {
|
||||
const changed = [];
|
||||
for (const [name, entry] of preset.locked) {
|
||||
const { field } = entry;
|
||||
const wrapper = getWrapper(field);
|
||||
if (!wrapper) continue;
|
||||
const controls = [...wrapper.querySelectorAll('input, select, textarea')];
|
||||
const input = controls[0];
|
||||
let value = entry.value;
|
||||
if (hooks.normalize) value = hooks.normalize(field, value);
|
||||
let valid = input && value !== null;
|
||||
if (valid && ['radio', 'checkbox'].includes(field.type)) {
|
||||
for (const control of controls) {
|
||||
control.checked = field.type === 'checkbox' ? value.includes(control.value) : control.value === value;
|
||||
}
|
||||
} else if (valid) {
|
||||
const previous = input.value;
|
||||
input.value = value;
|
||||
valid = String(value) === input.value && input.validity.valid;
|
||||
if (field.type === 'rating') {
|
||||
const max = Number(field.ratingMax) === 10 ? 10 : 5;
|
||||
valid = value !== '' && Number.isInteger(Number(value)) && Number(value) >= (field.required ? 1 : 0) && Number(value) <= max;
|
||||
}
|
||||
if (!valid) input.value = previous;
|
||||
}
|
||||
if (!valid) {
|
||||
preset.locked.delete(name);
|
||||
preset.invalid.add(name);
|
||||
continue;
|
||||
}
|
||||
entry.value = value;
|
||||
wrapper.classList.add('url-prefilled');
|
||||
for (const control of controls) control.disabled = true;
|
||||
// Ratings are custom keyboard/pointer controls rather than native inputs.
|
||||
const rating = wrapper.querySelector('.rating-stars');
|
||||
if (rating) {
|
||||
rating.setAttribute('aria-disabled', 'true');
|
||||
rating.tabIndex = -1;
|
||||
hooks.rating?.(rating, value);
|
||||
}
|
||||
changed.push(...controls);
|
||||
}
|
||||
for (const name of preset.hidden) {
|
||||
const wrapper = getWrapper({ name });
|
||||
if (!wrapper) continue;
|
||||
wrapper.hidden = true;
|
||||
wrapper.dataset.urlHidden = 'true';
|
||||
for (const control of wrapper.querySelectorAll('input, select, textarea')) control.required = false;
|
||||
}
|
||||
// Apply all values before refreshing dependencies, identity and choice visuals.
|
||||
for (const control of changed) {
|
||||
if (control.type === 'radio' && !control.checked) continue;
|
||||
control.dispatchEvent(new Event('input', { bubbles: true }));
|
||||
control.dispatchEvent(new Event('change', { bubbles: true }));
|
||||
}
|
||||
}
|
||||
|
||||
window.FormLinkPreset = { parse, apply };
|
||||
})();
|
||||
Reference in New Issue
Block a user