-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
137 lines (127 loc) · 4.55 KB
/
script.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
const GRORUD = 'NSR:StopPlace:5848'
const KOLSAS = 'NSR:StopPlace:4060'
const BRYNSENG = 'NSR:StopPlace:6086'
const TOYEN = 'NSR:StopPlace:6473'
const GRONLAND = 'NSR:StopPlace:6488'
const config = [
{ fromName: 'Kolsås', fromId: KOLSAS, toName: 'Tøyen', toId: TOYEN, lines: ['RUT:Line:3'] },
{ fromName: 'Grønland', fromId: GRONLAND, toName: 'Kolsås', toId: KOLSAS, lines: ['RUT:Line:3'] },
{ fromName: 'Tøyen', fromId: TOYEN, toName: 'Grorud', toId: GRORUD, lines: ['RUT:Line:5'] },
{ fromName: 'Tøyen', fromId: TOYEN, toName: 'Brynseng', toId: BRYNSENG, lines: ['RUT:Line:2', 'RUT:Line:3', 'RUT:Line:4'] },
{ fromName: 'Grorud', fromId: GRORUD, toName: 'Tøyen', toId: TOYEN, lines: ['RUT:Line:5'] }
];
const callAjax = (postContent, callback) => {
fetch('https://api.entur.io/journey-planner/v2/graphql', {
method: 'POST',
mode: 'cors',
headers: {
'ET-Client-Name': 'RekkTbanen',
'Content-Type': 'application/json'
},
body: JSON.stringify(postContent)
})
.then(response => response.json().then(jsonData => callback(jsonData)))
.catch(error => showErrorMessage("Feil ved kall til entur API"));
}
const showErrorMessage = (message) => {
document.getElementById('departure-loader').style.display = 'none';
var rowsEl = document.getElementById('departure-rows');
var noDeparturesFoundEl = document.createElement('div');
noDeparturesFoundEl.innerHTML = message;
rowsEl.appendChild(noDeparturesFoundEl);
}
const toDoubleDigit = (n) => {
return n > 9 ? '' + n: '0' + n;
}
const getDeparturesFromStop = (fromStop, toStop, lines) => {
const callback = (returnData) => {
var jsonReturn = returnData.data.trip.tripPatterns;
var rowsEl = document.getElementById('departure-rows');
if (jsonReturn[0].legs.length === 0 || !jsonReturn[0].legs[0].fromEstimatedCall) {
showErrorMessage('Fant ingen avganger');
} else {
for (var i = 0; i < jsonReturn.length; i++) {
var timeDiff = (new Date(jsonReturn[i].legs[0].fromEstimatedCall.expectedDepartureTime) - new Date()) / 1000;
var readableTime = Math.floor(timeDiff / 60) + '.' + toDoubleDigit(Math.floor(timeDiff % 60));
var tripEl = document.createElement('div');
tripEl.classList.add('departure-entry');
tripEl.innerHTML = readableTime;
rowsEl.appendChild(tripEl);
}
}
document.getElementById('departure-loader').style.display = 'none';
document.getElementById('refresh-button').style.display = 'inline-block';
}
var query = `{
trip(
from: {
place: "${fromStop}"
},
to: {
place: "${toStop}"
},
numTripPatterns: 5,
whiteListed: { lines: ["${lines.join("\",\"")}"] }
) {
tripPatterns {
legs {
fromEstimatedCall {
realtime
aimedDepartureTime
expectedDepartureTime
actualDepartureTime
}
}
}
}
}`;
var postContent = {
query: query,
variables: null,
operationName: null
}
callAjax(postContent, callback);
};
const setSelectedStop = (element) => {
showLoadingSpinner();
var selectedStop = document.getElementsByClassName('fa-check')[0];
if (selectedStop) {
selectedStop.classList.remove("fa-check"); // remove class icon
}
element.querySelector('.selection-icon').classList.add('fa-check');
};
const showLoadingSpinner = () => {
document.getElementById('refresh-button').style.display = 'none';
document.getElementById('departure-rows').innerHTML = ''; // clear old data;
document.getElementById('departure-loader').style.display = 'inline-block';
}
const createConfigElements = () => {
let root = document.getElementById('stop-selector');
for (let i = 0; i < config.length; i++) {
let stopEl = document.createElement('div');
stopEl.classList.add('stop-selector-item');
let selectionEl = document.createElement('i');
selectionEl.className = 'fa selection-icon';
let fromEl = document.createElement('span');
fromEl.innerHTML = config[i].fromName;
let arrowEl = document.createElement('i');
arrowEl.className = 'fa fa-arrow-right';
let toEl = document.createElement('span');
toEl.innerHTML = config[i].toName;
stopEl.appendChild(selectionEl);
stopEl.appendChild(fromEl);
stopEl.appendChild(arrowEl);
stopEl.appendChild(toEl);
stopEl.addEventListener('click', (event) => {
getDeparturesFromStop(config[i].fromId, config[i].toId, config[i].lines);
setSelectedStop(event.currentTarget);
});
root.appendChild(stopEl)
}
}
window.onload = () => {
createConfigElements();
document.getElementById('refresh-button').addEventListener('click', () => {
document.getElementsByClassName('fa-check')[0].click();
});
}