-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
107 lines (94 loc) · 2.17 KB
/
utils.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
'use strict'
Array.prototype.l = function() {
return this[this.length - 1]
}
Array.prototype.f = function() {
return this[0]
}
String.prototype.bw = function(char) {
return this.length > 0 && this[0] === char
}
function get(url, cb) {
let xhttp = new XMLHttpRequest()
xhttp.onreadystatechange = function() {
if (xhttp.readyState === 4 && xhttp.status === 200) {
cb(xhttp.responseText);
}
}
xhttp.open("GET", url, true)
xhttp.send()
}
const base = 'http://localhost:4000/'
function colorForTotalTime(totalTime, withMaxTime) {
let color = 'rgb('
if (totalTime > (withMaxTime * (1 / 3))) {
color += '255,'
} else {
color += '0,'
}
if (totalTime < (withMaxTime * (2 / 3))) {
color += '255,'
} else {
color += '0,'
}
color += '0)'
return color
}
/**
* @typedef {Object} Stop
* @property {Number} id
* @property {String} name
* @property {Number} lat
* @property {Number} lon
*/
/**
* @typedef {Object} StopTime
* @property {Number} stop_id
* @property {String} departure
* @property {Number} total_time
*/
/**
* @typedef {Object} NodePath
* @property {Number} from
* @property {Number} to
* @property {Number} time
*/
/**
* @param {Function.<Array.<Stop>>} cb
*/
function fetchStops(cb) {
get(base + 'stops', (body) => {
cb(JSON.parse(body))
})
}
/**
* @param {String} stopId
* @param {Function.<Array.<String>>} cb
*/
function fetchDepTimesForStop(stopId, cb) {
get(base + `departures?stop_id=${stopId}`, (body) => {
cb(JSON.parse(body))
})
}
/**
* @param {String} stopId
* @param {String} maxTime
* @param {String} departureTime
* @param {Function.<Array.<StopTime>>} cb
*/
function fetchHeatmap(stopId, maxTime, departureTime, cb) {
get(base + `heatmap?stop_id=${stopId}&from_time=${departureTime}&max_time_seconds=${maxTime}`, (body) => {
cb(JSON.parse(body))
})
}
/**
* @param {String} stopId
* @param {String} maxTime
* @param {String} departureTime
* @param {Function.<Array.<NodePath>>} cb
*/
function fetchPaths(stopId, maxTime, departureTime, cb) {
get(base + `paths?stop_id=${stopId}&from_time=${departureTime}&max_time_seconds=${maxTime}`, (body) => {
cb(JSON.parse(body))
})
}