Repository navigation
Expand file tree
/
Copy pathfrontend.js
More file actions
101 lines (88 loc) · 3.08 KB
/
Copy pathfrontend.js
File metadata and controls
101 lines (88 loc) · 3.08 KB
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
function addInput() {
var newInput = document.createElement('input');
newInput.setAttribute('placeholder', 'My other Friend\'s Location')
var container = document.createElement('div');
container.appendChild(newInput);
var before = document.getElementById('add-field').parentNode;
document.getElementById('form').insertBefore(container, before);
}
function delInput() {
var formChildren = document.getElementById('form').children;
var lastInputIndex = -1;
for(var i = 0; i < formChildren.length; i++) {
if(formChildren[i].children.length >= 1 &&
formChildren[i].children[0].tagName === 'INPUT') {
lastInputIndex = i;
}
}
if(lastInputIndex >= 0) {
formChildren[lastInputIndex].remove();
}
}
function calculate(stations, callback) {
var myHeaders = new Headers();
myHeaders.set('Content-Type', 'application/json');
var myInit = {
method : 'POST',
headers : myHeaders,
mode : 'cors',
body : JSON.stringify(stations)
};
fetch(new Request('/api/v1/meetup', myInit)).then(res => {
if(res.status == 400 || res.status == 200) {
return res.blob();
} else {
alert("Error in communication with the web server");
}
}).then(blob => {
var reader = new FileReader();
reader.addEventListener('loadend', () => {
var result = JSON.parse(reader.result);
callback(result);
});
reader.readAsText(blob);
});
}
function showMeeting() {
var stations = []
var formChildren = document.getElementById('form').children;
for(var i = 0; i < formChildren.length; i++) {
if(formChildren[i].children.length >= 1 &&
formChildren[i].children[0].tagName === 'INPUT' &&
formChildren[i].children[0].value !== '') {
stations = stations.concat([formChildren[i].children[0].value]);
}
}
if(stations.length >= 2) {
calculate(stations, result => {
if(result.error !== undefined) {
alert("Error: " + result.error);
} else {
var form = document.getElementById("form");
var linkBox = document.getElementById("links");
var resultDiv = document.getElementById('result');
if(resultDiv === null) {
resultDiv = document.createElement('div');
resultDiv.id = "result";
resultDiv.classList.add("text");
form.insertBefore(resultDiv, linkBox);
}
var routes = ""
for(var i = 0; i < stations.length; i++) {
var route = result.routes[stations[i]]
route[0] = `<em>${route[0]}</em>`;
route[route.length - 1] = `<strong>${route[route.length - 1]}</strong>`;
routes += `<li>${route.reverse().join(" ← ")}</li>`;
}
resultDiv.innerHTML = "<p>Your Meeting Point is <strong>" + result.meetup + "</strong></p>"
+ "<div id=\"routes\"><h2>Routes</h2><ul>" + routes + "</ul></div>";
}
});
}
}
function init() {
document.getElementById('add-field').onclick = addInput;
document.getElementById('del-field').onclick = delInput;
document.getElementById('calc').onclick = showMeeting;
}
window.addEventListener("DOMContentLoaded", init);