forked from ietf-tools/datatracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload-material.js
More file actions
62 lines (56 loc) · 1.95 KB
/
upload-material.js
File metadata and controls
62 lines (56 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright The IETF Trust 2021, All Rights Reserved
(
function () {
'use strict';
/**
* Hide the inactive input mb-3
* @param form form to process
*/
function showUrlOrFile(form) {
const useUrlInput = form.elements.namedItem('id_use_url');
const urlGroup = form.elements.namedItem('id_external_url')
.closest('div');
const fileGroup = form.elements.namedItem('id_file')
.closest('div');
if (useUrlInput.checked) {
urlGroup.hidden = false;
fileGroup.hidden = true;
} else {
urlGroup.hidden = true;
fileGroup.hidden = false;
}
}
/**
* Dispatch showUrlOrFile from a UI event on the enclosing form
* @param evt change event instance
*/
function handleFormChange(evt) {
showUrlOrFile(evt.currentTarget); // currentTarget is the form
}
/**
* Clear hidden file input values before submitting form to avoid
* needlessly sending a file when use_url is selected
* @param evt submit event instance
*/
function handleFormSubmit(evt) {
const form = evt.currentTarget;
const fileInput = form.elements.namedItem('file');
if (fileInput.hidden) {
fileInput.value = '';
}
}
/**
* Register event handlers and other initialization tasks.
*/
function initialize() {
const forms = document.querySelectorAll('form.upload-material');
for (let i = 0; i < forms.length; i++) {
const form = forms[i];
form.addEventListener('change', handleFormChange);
form.addEventListener('submit', handleFormSubmit);
showUrlOrFile(form);
}
}
initialize();
}
)();