Skip to content

Commit f3abb2f

Browse files
authored
Merge pull request #3017 from drgrice1/pgeditor-improvements
Improvements for the "Save As" form in the PG editor.
2 parents 6f377ef + 558704e commit f3abb2f

12 files changed

Lines changed: 330 additions & 262 deletions

File tree

htdocs/js/PGProblemEditor/pgproblemeditor.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,58 @@
382382
}
383383
});
384384

385+
const removeValidationErrors = (input) => {
386+
input?.classList.remove('is-invalid');
387+
input.setCustomValidity('');
388+
};
389+
390+
const validateInput = (e, input, isInvalid, defaultMessage, report) => {
391+
if (isInvalid) {
392+
e.preventDefault();
393+
input.classList.add('is-invalid');
394+
input.setCustomValidity(input.dataset.errorMessage ?? defaultMessage);
395+
if (report) input.reportValidity();
396+
return false;
397+
}
398+
removeValidationErrors(input);
399+
return true;
400+
};
401+
402+
// Validation of the target file for the save as tab.
403+
const saveAsTargetFile = document.getElementsByName('action.save_as.target_file')?.[0];
404+
saveAsTargetFile?.addEventListener('keyup', () => {
405+
if (saveAsTargetFile.value) removeValidationErrors(saveAsTargetFile);
406+
});
407+
408+
// Validation of the target set for the save as tab.
409+
const saveAsSaveModeRadios = document.getElementsByName('action.save_as.saveMode');
410+
const saveToTargetSetRadio = Array.from(saveAsSaveModeRadios).find(
411+
(r) => r.id === 'action_save_as_saveMode_new_problem_id' || r.id === 'action_save_as_saveMode_set_header_id'
412+
);
413+
const targetSetSelect = document.getElementsByName('action.save_as.targetSet')?.[0];
414+
const actionSaveAs = document.getElementById('save_as');
415+
for (const radio of saveAsSaveModeRadios) {
416+
radio.addEventListener('change', () => removeValidationErrors(targetSetSelect));
417+
}
418+
const saveToTargetSetSelected = () => {
419+
saveToTargetSetRadio.checked = true;
420+
if (targetSetSelect?.value) removeValidationErrors(targetSetSelect);
421+
};
422+
targetSetSelect?.addEventListener('change', saveToTargetSetSelected);
423+
targetSetSelect?.addEventListener('focusin', saveToTargetSetSelected);
424+
425+
document.forms.editor?.addEventListener('submit', (e) => {
426+
if (actionSaveAs && actionSaveAs.classList.contains('active')) {
427+
let report = true;
428+
for (const validationData of [
429+
[saveAsTargetFile, saveAsTargetFile?.value === '', 'Please enter a filename.'],
430+
[targetSetSelect, saveToTargetSetRadio?.checked && !targetSetSelect?.value, 'Please select a set.']
431+
]) {
432+
if (!validateInput(e, ...validationData, report)) report = false;
433+
}
434+
}
435+
});
436+
385437
const fileType = document.getElementsByName('file_type')[0]?.value;
386438

387439
// This is either the div containing the CodeMirror editor or the problemContents textarea in the case that

0 commit comments

Comments
 (0)