-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
145 lines (137 loc) · 4.41 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
<!--
© 2013 OKFN - CC0
This is an example of how to use Prefill in your applications
-->
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="build/bundle.js"></script>
<title>A demo of Prefill.js</title>
</head>
<body>
<div class="form" align="center">
<header>
<h1>Fill out 2 Belgian railway stations</h1>
</header>
<form>
<label>Load a profile: </label>
<input type="button" id="commuter" value="Commuter"/>
<br/>
<input type="button" id="clear" value="Clear history"/>
<br/>
<label>From</label><br/><input type="text" id="from"></input><br/>
<input type="button" id="switch" value="↓↑"/><br/>
<label>To</label><br/><input type="text" id="to"></input><br/>
<input type="button" id="submit" value="Go!"/>
</form>
</div>
<div id="result" align="center">
</div>
<script>
$("#commuter").click(function(){
$.ajax("examples/commuter.json", {
success:function(data){
console.log(data);
localStorage["planner_history"] = JSON.stringify(data);
window.alert("The commuter commutes from Ghent to Brussels everyday. In the weekend, (s)he does random trips.");
window.location.reload(true);
}
});
});
$("#clear").click(function(){
localStorage.removeItem("planner_history");
});
$("#switch").click(function(){
var temp = $("#from").val();
$("#from").val($("#to").val());
$("#to").val(temp);
});
var fetchResults = function(){
$("#result").html("Loading...");
var from = $("#from").val();
var to = $("#to").val();
$.ajax("http://api.irail.be/connections/", {
data : {
"format" : "json",
"from" : from,
"to" : to
},
success : function(data){
$("#result").html("<table><tr><th>#</th><th>Direction</th><th>duration</th></tr>");
for(var index = 0; index < data.connection.length ; index ++){
connection = data.connection[index];
$("#result").append("<tr><td>" + index + "</td><td>" + connection.departure.direction.name + "</td><td>" + connection.duration/60 +" min</td></tr>");
}
$("#result").append("</table>");
}
});
getLocation(function(location){
var now = new Date();
var history = [];
if(typeof localStorage["planner_history"] !== "undefined"){
history = JSON.parse(localStorage["planner_history"]);
}
history[history.length] = {
datetime : now.toISOString(),
location : location,
from : from,
to : to
};
localStorage["planner_history"] = JSON.stringify(history);
console.log(localStorage["planner_history"]);
});
};
$("#submit").click(fetchResults);
/**
* on load, this function will prefill the to and the from field
*/
$(document).ready(function(){
getLocation(function(location){
var now = new Date();
var p = new Prefill();
//get the history from local storage
var history = [];
if(typeof localStorage["planner_history"] !== "undefined"){
history = JSON.parse(localStorage["planner_history"]);
//prepare the prefill
p.prepare(history,function(){
console.log(location);
result = p.guess(now,location.longitude, location.latitude);
$('#to').val(result.to);
$('#from').val(result.from);
});
fetchResults();
}else{
console.log("No good learning collection found");
}
});
});
// Get the HTML5 location
var getLocation = function(callback){
if (navigator.geolocation){
navigator.geolocation.getCurrentPosition(function(p){
var result = {
"longitude": p.coords.longitude,
"latitude" : p.coords.latitude
};
callback(result);
}, function(){
console.log("Could not retrieve current location.");
callback({
longitude : 0,
latitude : 0
});
});
}
else{
console.info("Geolocation is not supported by this browser.");
callback({
longitude : 0,
latitude : 0
});
}
}
</script>
</body>
</html>