-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexButtons.js
111 lines (90 loc) · 3.87 KB
/
indexButtons.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
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
// * JS Code for Buttons
// * Get button objects and assign them to aliases
const openButtonProgress = document.getElementById("openDoorProgress");
const openButton = document.getElementById("openDoorButton");
const closeButtonProgress = document.getElementById("closeDoorProgress");
const closeButton = document.getElementById("closeDoorButton");
const stopButton = document.getElementById("stopDoorButton");
// * Basic Fetch Function
async function fetchURL(url) {
try {
// * Load this webpage subdirectory asynchronously so that we don't reload the page.
const response = await fetch(url)
// * Log the Response
console.log('HTTP status code:', response.status);
}
catch(error) {
// * Handle any errors that occurred during the fetch operation
console.error('Error:', error);
}
}
function setPressedStyle(button) {
button.style.backgroundColor = 'hsl(228, 66%, 70%)';
}
function setReleasedStyle(button) {
button.style.backgroundColor = 'hsl(228, 66%, 47%)';
}
function updateProgress(button){
// * Define iterator variable
let i = 0;
// * Define graphic update logic
return function (interval){
if (i <= 100) {
button.style.width = i + '%';
i++;
} else {
clearInterval(interval)
i=0;
}
}
}
// * Define what happens when the open button is pressed
function openButtonPressedEventHandler() {
// Asynchronously send open button event
fetchURL('/openButton/click')
setPressedStyle(openButton)
// Create a new instance of updateProgress for openButton
const openButtonUpdateProgress = updateProgress(openButtonProgress);
openInterval = setInterval(()=>{openButtonUpdateProgress(openInterval);}, 100);
}
// * Define what happens when the open button is released
function openButtonReleasedEventHandler() {
// setReleasedStyle(openButton)
}
// * Define what happens when the close button is pressed
function closeButtonPressedEventHandler() {
// Asynchronously send close button event
fetchURL('/closeButton/click')
setPressedStyle(closeButton)
// Create a new instance of updateProgress for closeButton
const closeButtonUpdateProgress = updateProgress(closeButtonProgress);
closeInterval = setInterval(()=>{closeButtonUpdateProgress(closeInterval);}, 100);
}
// * Define what happens when the close button is released
function closeButtonReleasedEventHandler() {
// setReleasedStyle(closeButton)
}
// * Define what happens when the stop button is pressed
function stopButtonPressedEventHandler() {
// Asynchronously send close button event
fetchURL('/closeButton/click')
stopButton.style.backgroundColor = 'hsl(0, 100%, 30%)';
}
// * Define what happens when the stop button is released
function stopButtonReleasedEventHandler() {
stopButton.style.backgroundColor = 'hsl(0, 100%, 50%)';
}
// * Create an event listener (( Interrupt )) mouse or finger press and release
// * On interrupt run relevant function.
openButton.addEventListener("mousedown", openButtonPressedEventHandler);
openButton.addEventListener("touchstart", openButtonPressedEventHandler);
openButton.addEventListener("mouseup", openButtonReleasedEventHandler);
openButton.addEventListener("touchend", openButtonReleasedEventHandler);
closeButton.addEventListener("mousedown", closeButtonPressedEventHandler);
closeButton.addEventListener("touchstart", closeButtonPressedEventHandler);
closeButton.addEventListener("mouseup", closeButtonReleasedEventHandler);
closeButton.addEventListener("touchend", closeButtonReleasedEventHandler);
stopButton.addEventListener("mousedown", stopButtonPressedEventHandler);
stopButton.addEventListener("touchstart", stopButtonPressedEventHandler);
stopButton.addEventListener("mouseup", stopButtonReleasedEventHandler);
stopButton.addEventListener("touchend", stopButtonReleasedEventHandler);