-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuserInterface.js
More file actions
139 lines (120 loc) · 4.27 KB
/
userInterface.js
File metadata and controls
139 lines (120 loc) · 4.27 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
initPlayBackSlider();
initAddSourceBtn();
enableHelpForCoordinateFields();
initStillMediumMessage();
initModalSheet();
function initModalSheet() {
var modal = document.querySelector(".modalsheet");
var openBtn = document.getElementById("question-mark");
var closeBtn = document.querySelector(".close");
openBtn.onclick = function () {
modal.style.display = "block";
};
closeBtn.onclick = function () {
modal.style.display = "none";
};
window.onclick = function (event) {
if (event.target == modal) {
modal.style.display = "none";
}
};
}
function initStillMediumMessage() {
let addSourceButton = document.querySelector("#add-source-button");
window.onload = handleTableDisplay;
addSourceButton.addEventListener("click", handleTableDisplay);
}
function handleTableDisplay() {
let stillMessage = document.querySelector(".no-disturbances-message");
let sourcesTable = document.querySelector(".sources-table");
if (sources.length == 0) {
stillMessage.style.display = "block";
sourcesTable.style.display = "none";
} else {
stillMessage.style.display = "none";
sourcesTable.style.display = "block";
}
}
function enableHelpForCoordinateFields() {
let xCoordinateField = document.querySelector("#x-coordinate");
let yCoordinateField = document.querySelector("#y-coordinate");
xCoordinateField.addEventListener("click", UnhideHelpText);
yCoordinateField.addEventListener("click", UnhideHelpText);
}
function UnhideHelpText() {
let alert = document.querySelector(".alert");
let help = document.querySelector(".help");
alert.style.display = "none"; // do not want to display 2 messages
help.style.display = "block";
setTimeout(() => (help.style.display = "none"), 5000);
}
function showUnfilledFieldsAlert() {
let alert = document.querySelector(".alert");
let help = document.querySelector(".help");
help.style.display = "none"; // do not want to display 2 messages
alert.style.display = "block";
}
function initPlayBackSlider() {
let playbackSlider = document.querySelector("#playback-speed-slider");
let playBackSpeedValue = document.querySelector(".playback-speed-value");
playbackSlider.addEventListener("input", () => {
playbackSpeed = playbackSlider.value;
playBackSpeedValue.textContent = playbackSlider.value + "x";
});
}
function initAddSourceBtn() {
let amplitudeField = document.querySelector("#amplitude");
let frequencyField = document.querySelector("#frequency");
let wavelengthField = document.querySelector("#wavelength");
let xCoordinateField = document.querySelector("#x-coordinate");
let yCoordinateField = document.querySelector("#y-coordinate");
let addSourceButton = document.querySelector("#add-source-button");
let alert = document.querySelector(".alert");
addSourceButton.addEventListener("click", () => {
// check for unfilled fields
if (
amplitudeField.value == 0 ||
wavelengthField.value == 0 ||
frequencyField.value == 0 ||
xCoordinateField.value == 0 ||
yCoordinateField.value == 0
)
showUnfilledFieldsAlert();
else {
sources.push(
new Source(
parseFloat(amplitudeField.value),
parseFloat(wavelengthField.value),
parseFloat(frequencyField.value),
parseFloat(xCoordinateField.value),
parseFloat(yCoordinateField.value)
)
);
setSumOfAmplitudes();
alert.style.display = "none";
addSourceToTable(sources[sources.length - 1]);
}
});
}
function addSourceToTable(source) {
let tableBody = document.querySelector(".table-body");
let row = document.createElement("tr");
let numberCell = document.createElement("td");
numberCell.textContent = sources.length;
let amplitudeCell = document.createElement("td");
amplitudeCell.textContent = source.amplitude + " m";
let wavelengthCell = document.createElement("td");
wavelengthCell.textContent = source.wavelength + " m";
let frequencyCell = document.createElement("td");
frequencyCell.textContent = source.frequency + " Hz";
let coordinateCell = document.createElement("td");
coordinateCell.textContent = "(" + source.x + ", " + source.y + ")";
row.append(
numberCell,
amplitudeCell,
wavelengthCell,
frequencyCell,
coordinateCell
);
tableBody.appendChild(row);
}