ui: fix objective

This commit is contained in:
undefined committed 2024-05-29 19:05:36 +08:00
1 parent a34312607b
commit 8619261bd4
1 file changed
+11 -12
@@ -253,19 +253,17 @@ const page = new NamedPage(['problem_detail', 'contest_detail_problem', 'homewor
} }
function loadAns() { function loadAns() {
const saved = localStorage.getItem(`${cacheKey}#objective`); const saved = localStorage.getItem(`${cacheKey}#objective`);
if (saved) { if (!saved) return;
Object.assign(ans, JSON.parse(saved)); const isValidOption = (v) => v.length === 1 && v.charCodeAt(0) >= 65 && v.charCodeAt(0) <= 90;
for (const [id, val] of Object.entries(ans)) { Object.assign(ans, JSON.parse(saved));
if (Array.isArray(val)) { for (const [id, val] of Object.entries(ans)) {
for (const v of val) { if (Array.isArray(val)) {
if (val.length === 1 for (const v of val) {
&& val.charCodeAt(0) >= 65 && val.charCodeAt(0) <= 90) $(`.objective_${id} input[value=${v}]`).prop('checked', true); if (isValidOption(v)) $(`.objective_${id} input[value="${v}"]`).prop('checked', true);
}
} }
$(`.objective_${id} input[type=text], .objective_${id} textarea`).val(val);
if (val.length === 1
&& val.charCodeAt(0) >= 65 && val.charCodeAt(0) <= 90) $(`.objective_${id}.radiobox [value="${val}"]`).prop('checked', true);
} }
$(`.objective_${id} input[type=text], .objective_${id} textarea`).val(val);
if (isValidOption(val)) $(`.objective_${id}.radiobox [value="${val}"]`).prop('checked', true);
} }
} }
@@ -280,8 +278,9 @@ const page = new NamedPage(['problem_detail', 'contest_detail_problem', 'homewor
}); });
$('input.objective-input[type=checkbox]').on('input', (e) => { $('input.objective-input[type=checkbox]').on('input', (e) => {
if (e.target.checked) { if (e.target.checked) {
if (ans[e.target.name] === undefined) ans[e.target.name] = []; ans[e.target.name] ||= [];
ans[e.target.name].push(e.target.value); ans[e.target.name].push(e.target.value);
ans[e.target.name] = [...new Set(ans[e.target.name])].sort((a, b) => a.charCodeAt(0) - b.charCodeAt(0));
} else { } else {
ans[e.target.name] = ans[e.target.name].filter((v) => v !== e.target.value); ans[e.target.name] = ans[e.target.name].filter((v) => v !== e.target.value);
} }