-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
183 lines (164 loc) · 5.57 KB
/
index.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
178
179
180
181
182
183
var vueInstance;
class Earthquake {
constructor(mag, place, time, tz, lat, lon, depth) {
this.mag = mag
this.place = place
this.time = time
this.tz = tz
this.lat = lat
this.lon = lon
this.depth = depth
}
}
var earthquakeList = []
var filteredList = []
function filterEarthquakesForDate(fromDate, toDate, earthquakes) {
filteredList = filteredList.filter((eq) => {
// console.log(`FROM: ${fromDate}, TO: ${toDate}, CURRENT: ${eq.time}`)
// console.log(`EARTHQUAKE IS IN BETWEEN? ${fromDate <= eq.time && eq.time <= toDate}`)
return (fromDate <= eq.time) && (eq.time <= toDate)
})
}
function filterEarthquakesForMagnitude(from, to, earthquakes) {
var i = 0
filteredList = filteredList.filter((eq) => {
if (eq.mag >= from && eq.mag <= to) i++
return eq.mag >= from && eq.mag <= to
})
}
$(document).ready(function() {
$('#datepicker-from').datepicker({
defaultDate: new Date("01/01/2019"),
onSelect: function () {
var newFromDate = $('#datepicker-from').datepicker('getDate');
var toDate = $('#datepicker-to').datepicker('getDate');
updateFilterForDate(newFromDate.getTime(), toDate.getTime())
updateEarthquakes(filteredList)
}
});
$('#datepicker-to').datepicker({
defaultDate: new Date("09/07/2019"),
onSelect: function (newToDate) {
var fromDate = $('#datepicker-from').datepicker('getDate');
var newToDate = $('#datepicker-to').datepicker('getDate');
updateFilterForDate(fromDate.getTime(), newToDate.getTime())
updateEarthquakes(filteredList)
}
});
$(".js-range-slider").ionRangeSlider({
type: "double",
min: 5,
max: 10,
step: 0.1,
onChange: function (slider) {
updateFilterForMagnitude(slider.from, slider.to)
updateEarthquakes(filteredList)
}
});
$.ajax({
type: "GET",
url: "http://localhost:8000/earthquakes",
dataType: 'json',
success: function(earthquakes){
earthquakeList = earthquakes
filteredList = earthquakes
// Set earthquakes, then filter
var fromDate = $('#datepicker-from').datepicker('getDate');
var toDate = $('#datepicker-to').datepicker('getDate');
var slider = $(".js-range-slider").data("ionRangeSlider").result;
var x = filterEarthquakesForMagnitude(slider.from, slider.to)
filterEarthquakesForDate(fromDate.getTime(), toDate.getTime())
loadEarthquakes(filteredList);
},
error: function( xhr, status, errorThrown ) {
/* Temp, for debugging. Remove/alter before push to production. */
alert( "Something went wrong! Check the console for logs." );
console.log( "Error: " + errorThrown );
console.log( "Status: " + status );
console.dir( xhr );
}
})
$('#toggle-options').click(function() {
toggleOptions();
})
$('#overlay').click(function() {
toggleOptions();
})
})
function updateFilterForMagnitude(from, to) {
filteredList = earthquakeList.slice()
var fromDate = $('#datepicker-from').datepicker('getDate');
var toDate = $('#datepicker-to').datepicker('getDate');
filterEarthquakesForDate(fromDate.getTime(), toDate.getTime())
filterEarthquakesForMagnitude(from, to)
}
function updateFilterForDate(fromDate, toDate) {
filteredList = earthquakeList.slice()
filterEarthquakesForDate(fromDate, toDate)
var slider = $(".js-range-slider").data("ionRangeSlider").result;
filterEarthquakesForMagnitude(slider.from, slider.to)
}
function toggleOptions() {
$('#options').toggle();
$('#overlay').toggle();
}
function loadEarthquakes(earthquakes) {
console.log(earthquakes)
someMarkers = []
for (var eq of earthquakes) {
someMarkers.push({
latLng: [eq.latitude, eq.longitude],
name: eq.place,
mag: eq.mag,
time: eq.time
})
}
$('#world-map').vectorMap({
map: 'world_mill',
markerStyle: {
initial: {
fill: '#f52222',
stroke: '#2e2e2e'
}
},
markers: someMarkers,
onMarkerTipShow: function(event,label,index){
var markers = $('#world-map').vectorMap('get', 'mapObject').markers;
let current = markers[index].config
var timeString = getStringForEpoch(current.time)
label.html(
'<b>'+current.name.charAt(0).toUpperCase() + current.name.slice(1) +'</b><br/>'
+ "Magnitude: " + current.mag +'<br/>' +
"Date: " + timeString + '<br/>'
);
},
onRegionTipShow: function(e, el, code){
e.preventDefault();
}
});
}
function getStringForEpoch(epoch) {
console.log(epoch)
var date = new Date(epoch)
var year = date.getYear() + 1900
var month = date.getMonth() + 1
var day = date.getDate()
var hours = date.getHours()
var minutes = date.getMinutes()
console.log(year)
return `${year}-${month}-${day} ${hours}:${minutes}`
}
function updateEarthquakes(earthquakes) {
/*someMarkers = []
for (var eq of earthquakes) {
someMarkers.push({
latLng: [eq.latitude, eq.longitude],
name: eq.place,
mag: eq.mag
})
}
var x = $('#world-map').vectorMap('get', 'mapObject').markers
console.log(x);*/
$('#world-map').vectorMap('get', 'mapObject').remove()
loadEarthquakes(earthquakes)
}