This repository has been archived by the owner on Aug 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
run-client.js
159 lines (138 loc) · 4.05 KB
/
run-client.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
var unit = 'mi';
// from https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date
function pad(n) {
return n < 10 ? '0' + n : n;
}
function formatDuration(duration) {
var hours = Math.floor(duration/3600);
var minutes = Math.floor(duration/60)-hours*60;
var seconds = Math.round(duration % 60);
if (hours > 0)
return pad(hours) + ':' + pad(minutes) + ':' + pad(seconds);
else
return pad(minutes) + ':' + pad(seconds);
}
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
function formatDate(date) {
return months[date.getMonth()]+" "+date.getDate()+", "+date.getFullYear();
}
function formatTime(date) {
return date.getHours() + ":" + pad(date.getMinutes());
}
function formatDistance(meters) {
if (unit == 'mi')
return parseFloat(meters/1609.344).toFixed(2) + ' mi';
else
return parseFloat(meters/1000).toFixed(2) + ' km';
}
function setCookie(name, value, days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) {
return c.substring(nameEQ.length, c.length);
}
}
return null;
}
function deleteCookie(name) {
setCookie(name,"",-1);
}
function setUnit(newUnit) {
unit = newUnit;
setCookie('unit', unit, 10000);
/*
var distanceSpans = $('.distance');
for (var i = 0; i < runs.length; i++) {
distanceSpans[i].innerHTML = formatDistance(runs[i].distance, 2);
}
*/
}
function toggleUnits() {
if (unit == 'km') {
setUnit('mi');
} else {
setUnit('km');
}
document.location.reload(true);
}
function downloadGPX(runID) {
window.location = '/export-gpx/'+runID;
d3.select('a#download-'+runID).classed('btn-info', false);
d3.select('a#download-'+runID).select('i').classed('icon-white', false);
}
function makeDownloadButton(run) {
var html = '<div class="downloadbtn">';
if (run.hasGPSData == 'yes') {
html += '<a id="download-'+run.runID+'" class="btn btn-small';
html += (run.exported=='no')?' btn-info':'';
html += '" href="javascript:downloadGPX(\''+run.runID+'\');">Download GPX';
if (run.hasHRData == 'yes') {
html += '+<i class="icon-heart '+((run.exported=='no')?'icon-white':'')+'"></i>';
}
html += '</a></div>';
/*
} else if (run.hasGPSData == 'no') {
html += '<p><small>(no GPS data)</small></p></div>';
} else {
html += '<p><small>(error checking for GPS data)</small></p></div>'
}
*/
} else {
html += '</div>';
}
return html;
}
function makeRunList() {
// not very elegant, but solves the problem that runs are otherwise inserted out-of-order
$('div#run-list').html('');
d3.select('#run-list').selectAll('.run')
.data(runs, function(d) {return d.runID; })
.enter().append('div')
.attr('id', function(run) { return run.runID; })
.classed('run', true)
.html(function(run) { return makeDownloadButton(run)
+ '<p><b>'+formatDate(run.startTime) + '</b> at ' + formatTime(run.startTime) + ', '
+ formatDistance(run.distance) + ', '
+ formatDuration(run.duration) + ' ';
});
}
function updateRunList() {
makeRunList();
setTimeout(function() {
d3.json('/updateRunList', function(data) {
data.newRuns.forEach(function(run) {
run.startTime = new Date(run.startTime);
});
runs = runs.concat(data.newRuns);
runs.sort(function(a, b) { return b.startTime - a.startTime; });
if (data.done) {
makeRunList();
$('div#checking').hide('fast');
} else {
updateRunList();
}
});
}, 1000);
}
function initializeRuns() {
var cookieUnit = getCookie('unit');
if (cookieUnit) {
unit = cookieUnit;
}
runs.forEach(function(run) {
run.startTime = new Date(run.startTime);
});
updateRunList();
}