-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
165 lines (140 loc) · 6.19 KB
/
index.html
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
<!DOCTYPE html>
<html>
<header>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
</header>
<body>
<h2 class="text-center">TGVMAX REQUESTS</h2>
<p class="text-center">Ce projet utilise les ressources de la sncf (actualisées toutes les 24h, vers 14h). </p>
<form id="form" name="name_form" action=" " class="form-group text-center">
<input type="origine" name="origine">
<input type="destination" name="destination">
<input type="submit" value="TCHOU TCHOU !" class="btn btn-primary">
</form>
<p id="realhits" class="text-center"></p>
<div id="container" class="container center">
<div id="row" class="row">
</div>
</div>
<script type="text/javascript">
var rows = 400;
class trajet
{
constructor(heure_depart,heure_arrivee,date,origine,destination){
this.heure_depart = " " + heure_depart + " ";
this.heure_arrivee = " " + heure_arrivee + " ";
this.date = " " + date + " ";
this.origine = " " + origine + " ";
this.destination = " " + destination + " ";
}
render(){
var div = document.createElement("div");
var heure_depart = document.createElement("heure_depart");
var heure_arrivee = document.createElement("heure_arrivee");
var date = document.createElement("date");
var origine = document.createElement("origine");
var destination = document.createElement("destination");
var button = document.createElement("a");
var text_heure_depart = document.createTextNode( this.heure_depart );
var text_heure_arrivee = document.createTextNode( this.heure_arrivee );
var text_date = document.createTextNode( this.date );
var text_origine = document.createTextNode( this.origine );
var text_destination = document.createTextNode( this.destination );
var text_button = document.createTextNode('Reserver');
heure_depart.append(text_heure_depart);
heure_arrivee.append(text_heure_arrivee);
date.append(text_date);
origine.append(text_origine);
destination.append(text_destination);
button.append(text_button);
heure_depart.setAttribute("class","col-sm-2 align-self-center");
heure_arrivee.setAttribute("class","col-sm-2 align-self-center");
date.setAttribute("class","col-sm-2 align-self-center");
origine.setAttribute("class","col-sm-2 align-self-center");
destination.setAttribute("class","col-sm-2 align-self-center");
button.setAttribute("class","col-sm-2 align-self-center btn btn-link");
button.setAttribute("type","button");
button.setAttribute("href","http://trainline.fr");
button.setAttribute("target","_blank");
div.append(heure_depart);
div.append(heure_arrivee);
div.append(date);
div.append(origine);
div.append(destination);
div.append(button);
div.setAttribute("class","row");
var currentDiv = document.getElementById("container");
currentDiv.append(div);
}
}
function render_train(origine,destination){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var count_OUI = 0;
var myObj = JSON.parse(this.responseText);
var currentDiv = document.getElementById("container");
currentDiv.innerHTML = "";
myObj.records.sort(function(a,b){
if( new Date(a.fields.date) > new Date(b.fields.date)){
return 1;
}
else if (new Date(a.fields.date) < new Date(b.fields.date)){
return -1;
}
else{
if( parseInt(a.fields.heure_depart.replace(':','.')) >
parseInt(b.fields.heure_depart.replace(':','.')))
return 1;
else if (parseInt( a.fields.heure_depart.replace(':','.') )<
parseInt( b.fields.heure_depart.replace(':','.'))){
return -1;
}
else {
return 1;
}
}
});
for(var i = 0; i < Math.min(myObj.nhits,rows) ; i++){
var trajet1 = new trajet(myObj.records[i].fields.heure_depart,
myObj.records[i].fields.heure_arrivee,
myObj.records[i].fields.date,
myObj.records[i].fields.origine,
myObj.records[i].fields.destination);
if(myObj.records[i].fields.od_happy_card == "OUI"){
trajet1.render();
count_OUI ++;
}
document.getElementById("realhits").innerHTML = "Train(s) disponibles: " + count_OUI + "/" +myObj.nhits ;
}
}
}; var base_api="https://ressources.data.sncf.com/api/records/1.0/search/?dataset=tgvmax&";
var query='q=origine%3A%22'+origine+'%22destination%3A%22'+destination+'%22';
var end_query="&rows="+rows+"&sort=date&facet=date&facet=origine&facet=destination";
xmlhttp.open("GET", base_api + query + end_query,true);
xmlhttp.send();
}
var destination="MARSEILLE";
var origine="PARIS";
document.forms.name_form.origine.setAttribute("value",origine);
document.forms.name_form.destination.setAttribute("value",destination);
render_train(origine,destination);
function render_train_form(event)
{
event.preventDefault();
var origine = document.forms.name_form.origine.value;
var destination = document.forms.name_form.destination.value;
render_train(origine,destination);
return true;
}
document.getElementById("form").addEventListener("submit",render_train_form);
</script>
</body>
</html>