|
382 | 382 | } |
383 | 383 | }); |
384 | 384 |
|
| 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 | + |
385 | 437 | const fileType = document.getElementsByName('file_type')[0]?.value; |
386 | 438 |
|
387 | 439 | // This is either the div containing the CodeMirror editor or the problemContents textarea in the case that |
|
0 commit comments