-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_controls.js
177 lines (136 loc) · 3.63 KB
/
update_controls.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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// toggling circle that will be shown.
function toggelCircle() {
let counter = 0
circles.forEach((circle, idx) => {
let chance = densityPercent;
let randNum = Math.random()*100;
if (randNum <= chance) {
circle.setMap(map)
counter++
} else {
circle.setMap(null)
}
})
(counter)
}
const scaling = 0.5
function updateCircleSize() {
circles.forEach( (circle, idx) => {
let radius = circle.populartimes[day].data[hour];
radius = radius * scaling;
circle.setRadius(radius) // for non growing animation
});
hourSlider.value = hour;
}
function contupdateCircleSize() {
if (hour > 23) {
hour = 0;
day += 1;
if (day > 6) {
day = 0
}
handleDay(day)
}
circles.forEach((circle, idx) => {
let radius = circle.populartimes[day].data[hour];
radius = radius * scaling;
circle.setRadius(radius) // for non growing animation
})
hourSlider.value = hour;
// changing the hourSlider programmatically does not trigger the onchange
// listener so we manually set the innerHTML for this reason
for (let i = 0 ; i < hourHTMLclass.length; i++) {
if (hour < 10) {
hourHTMLclass[i].innerHTML = "0" + hour
} else {
hourHTMLclass[i].innerHTML = hour
}
}
hour += 1;
}
// Setting initial day, hour, and restaurant density
let day = 6;
let hour = 0;
let densityPercent = 10;
let hourSlider = document.getElementById("hourSlider");
let hourHTMLclass = document.getElementsByClassName("hour");
// Update the current slider value (each time you drag the slider handle)
hourSlider.onchange = function() {
// "this" refers to the hour slider position
hour = parseInt(this.value);
for (let i = 0 ; i < hourHTMLclass.length; i++) {
if (hour < 10) {
hourHTMLclass[i].innerHTML = "0" + this.value
} else {
hourHTMLclass[i].innerHTML = this.value
}
}
updateCircleSize()
}
let densityRangeSlider = document.getElementById("densityRangeSlider");
let densityPercentHTML = document.getElementById("densityPercent");
densityRangeSlider.value = densityPercent;
// Display the default slider value
densityPercentHTML.innerHTML = densityRangeSlider.value;
// Update the current slider value (each time you drag the slider handle)
densityRangeSlider.oninput = function() {
densityPercentHTML.innerHTML = this.value;
densityPercent = parseInt(this.value);
// toggling circle that will be shown.
toggelCircle()
}
// update day on change of radio button
let dayRadio = document.getElementById("day")
dayRadio.onchange = function(event) {
day = parseInt(event.target.value);
updateCircleSize()
}
let dayHTML = document.getElementById("dayofweek")
function handleDay(day) {
if (!(typeof day === "number")) {
day = parseInt(day.value);
}
let dayname;
switch (day) {
case 0:
dayname = "Monday"
break
case 1:
dayname = "Tuesday"
break
case 2:
dayname = "Wednesday"
break
case 3:
dayname = "Thursday"
break
case 4:
dayname = "Friday"
break
case 5:
dayname = "Saturday"
break
case 6:
dayname = "Sunday"
break
}
// if not user generated click, update radio button
if (!(event)) {
document.getElementById(dayname.toLowerCase()).click()
}
// update day HTML on map
dayHTML.innerHTML = dayname;
}
let alreadyPressed = false // so users can't spam play button
let setIntervalValid;
function handlePlay() {
if (!alreadyPressed) {
alreadyPressed = true;
setIntervalValid = setInterval(contupdateCircleSize, 500);
}
}
function handlePause() {
alreadyPressed = false;
clearInterval(setIntervalValid);
}
//