-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshow_program.js
43 lines (39 loc) · 1.49 KB
/
show_program.js
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
window.onload = function () {
var select = document.getElementById("example_lbl");
var prevSelectedIndex = select.options.selectedIndex;
var textarea = document.getElementById("program_area");
function changeProgram(path) {
fetch(path)
.then(function (response) {
if (response.status !== 200) {
console.log("Error", response.status);
return;
}
response.text()
.then(function (text) { textarea.value = text.trim(); })
.catch(function (error) { console.error(error); })
})
.catch(function (error) { console.error(error); });
}
select.addEventListener('change', function (event) {
var selectedIndex = event.target.options.selectedIndex;
var fileOption = select.options[selectedIndex];
var option = fileOption.value;
if (option != "none") {
if (textarea.value !== "" && prevSelectedIndex === 0) {
if (confirm("Delete existing user-supplied program?")) {
changeProgram(option);
}
}
else {
changeProgram(option);
}
}
prevSelectedIndex = selectedIndex;
});
textarea.addEventListener('input', function (event) {
if (select.options.selectedIndex !== 0) {
select.options.selectedIndex = prevSelectedIndex = 0;
}
});
};