-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
421 lines (382 loc) · 10.4 KB
/
Copy pathapp.js
File metadata and controls
421 lines (382 loc) · 10.4 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
var FRAME_T = 1.0/29.97; // TODO: read frame rate from video
var time_offset = 0.0;
var K_IN_MILE = 1.60934;
var DISP_EPS = 0.001;
var container = null; // initialized later
function fmt_time(t)
{
if (typeof t !== "number" || isNaN(t))
return "";
var ss, mm, hh;
var sign = "";
if (t < 0)
{
sign = "-";
t = -t;
}
ss = t % 60;
t -= ss;
ss = ss.toFixed(2);
t = Math.round(t);
t /= 60;
mm = t % 60;
t -= mm;
t /= 60;
hh = Math.round(t);
var res_arr = [];
if (hh)
res_arr.push(hh);
res_arr.push(mm);
res_arr.push(ss);
return sign + res_arr.map(function (t) {
if (t < 10) t = "0" + t; return t;}).join(":");
}
function ucfirst(s)
{
return s && s[0].toUpperCase() + s.slice(1);
}
function update_time()
{
$('#time-overlay').text(fmt_time(get_time() - time_offset));
}
// from https://stackoverflow.com/questions/1293147/javascript-code-to-parse-csv-data
function parse_csv(str) {
var arr = [];
var quote = false; // true means we're inside a quoted field
// iterate over each character, keep track of current row and column (of the returned array)
for (var col = 0, c = 0; c < str.length; c++) {
var cc = str[c], nc = str[c+1]; // current character, next character
arr[col] = arr[col] || ''; // create a new column (start with empty string) if necessary
// If the current character is a quotation mark, and we're inside a
// quoted field, and the next character is also a quotation mark,
// add a quotation mark to the current column and skip the next character
if (cc == '"' && quote && nc == '"') { arr[col] += cc; ++c; continue; }
// If it's just one quotation mark, begin/end quoted field
if (cc == '"') { quote = !quote; continue; }
// If it's a comma and we're not in a quoted field, move on to the next column
if (cc == ',' && !quote) { ++col; continue; }
// If it's a newline (CRLF) and we're not in a quoted field, skip the next character
// and move on to the next row and move to column 0 of that new row
if (cc == '\r' && nc == '\n' && !quote) { break; }
// If it's a newline (LF or CR) and we're not in a quoted field,
// move on to the next row and move to column 0 of that new row
if ((cc == '\n' || cc == '\r') && !quote) { break; }
// Otherwise, append the current character to the current column
arr[col] += cc;
}
return arr;
}
function arr_to_csv_row(arr)
{
return arr.map(function (col) {
if (typeof col !== "string")
return '""';
return '"' + col.replace("\\", "").replace(/\"/g, '""') + '"';
}).join(',') + "\r\n";
}
function get_time()
{
return document.getElementById('video').currentTime;
}
function set_time(t)
{
document.getElementById('video').currentTime = t;
}
function is_time(f)
{
return f == "time" || f == "pace";
}
function inc_time_pos(dt)
{
set_time(get_time() + dt);
}
function show_status(msg)
{
$('#status').text(msg);
}
function show_error(msg)
{
$('#error').text(msg);
}
function parse_time(t_str)
{
if (typeof t_str !== "string" && typeof t_str !== "number")
return NaN;
var parts = t_str.split(":");
var t = 0;
for (var i = 0; i < parts.length; i++)
{
var n = parseFloat(parts[i]);
if (isNaN(n))
return NaN;
t = t * 60 + n;
}
return t;
}
window.onload = function () {
Vue.component('vue-bootstrap-typeahead', VueBootstrapTypeahead);
container = new Vue({
el: '#container',
data: {
req_fields: ["name", "age", "gender"],
opt_fields: ["time", "pace"],
splits: null,
all_fields: null,
distance: null,
current_split: -1,
overlay_buttons: [
{id: "prev-frame", text: "<<" },
{id: "next-frame", text: ">>"},
{id: "start-time", text: "Start"},
{id: "record-time", text: "Time"},
],
file_inputs: [
{id: "video-file", prompt: "Load Video"},
{id: "participant-file", prompt: "Load Participants"}
],
participants: [],
current_participant: null
},
computed: {
split_inputs: function () {
var res = [ {pos: -1, txt: "Finish" }];
if (this.splits && this.splits.length)
res = res.concat(this.splits.map(function (el, i) {
return {pos: i, txt: el};
}));
return res;
},
distance_str: {
set: function (d_str) {
var vm = this;
d_str = d_str.trim().toLowerCase();
vm.distance = parseFloat(d_str);
if (d_str.endsWith("k"))
{
vm.distance /= K_IN_MILE;
}
console.log("Updating pace");
vm.update_pace();
},
get: function() {
var vm = this;
if (!vm.distance)
return "";
if (Math.abs(vm.distance - Math.round(vm.distance)) < DISP_EPS)
return vm.distance;
return Math.round(vm.distance * K_IN_MILE) + "K";
}
}
},
methods: {
handle_click: function (b) {
var cb_name = b.id.replace("-", "_");
this[cb_name]();
},
handle_file_select: function (f, event) {
var cb_name = "handle_" + f.id.replace("-", "_") + "_select";
this[cb_name](event);
},
handle_video_file_select: function (e)
{
if (e.target.files.length <= 0)
return;
var f = e.target.files[0];
$('#video').attr("src", URL.createObjectURL(f));
$('#video').bind("timeupdate", update_time);
},
handle_participant_file_select: function (e)
{
if (e.target.files.length <= 0)
return;
var f = e.target.files[0];
var reader = new FileReader();
reader.onload = function (read_e) {
container.init_participants(read_e.target.result);
}
reader.readAsText(f);
},
fmt_field: function(f, val)
{
return (is_time(f) && val) ? val.fmt_t : val;
},
handle_export: function () {
var vm = this;
var header = vm.all_fields.map(ucfirst).concat(vm.splits.map(
function (s) { return "Split " + s;}
));
var export_line = arr_to_csv_row(header);
var data = export_line;
for (var i = 0; i < vm.participants.length; i++)
{
var p = vm.participants[i];
var arr = vm.all_fields.map(function (f) {
return vm.fmt_field(f, p[f]);
});
arr = arr.concat(vm.splits.map(function (s, i) {
return vm.get_time(p.splits[i]);
}));
data += arr_to_csv_row(arr);
}
var el_a = document.createElement('a');
var data_b = new Blob([data]);
el_a.href = URL.createObjectURL(data_b, {type: "text/csv"});
el_a.download = "participants.csv";
document.body.append(el_a);
el_a.click();
el_a.remove();
URL.revokeObjectURL(el_a.href);
},
next_frame: function () {
inc_time_pos(FRAME_T);
},
prev_frame:function () {
inc_time_pos(-FRAME_T);
},
start_time: function () {
time_offset = get_time();
update_time();
},
goto_time: function (t_str) {
var t = parse_time(t_str);
if (isNaN(t))
return;
set_time(t + time_offset);
update_time();
},
record_time: function() {
var t = get_time() - time_offset;
var t_o = {t:t, fmt_t: fmt_time(t)};
var vm = this;
if (!vm.current_participant)
return;
if (vm.current_split == -1 /* Finish */)
vm.current_participant.time = t_o;
else
{
vm.current_participant.splits[vm.current_split] = t_o;
}
vm.update_pace_for_participant(vm.current_participant);
vm.sort_participants();
},
update_pace: function() {
var i;
var vm = this;
for (i = 0; i < vm.participants.length; i++)
{
vm.update_pace_for_participant(vm.participants[i]);
}
},
sort_participants: function () {
this.participants.sort(function (a,b) {
if (!a.time && b.time) return 1;
if (a.time && !b.time) return -1;
if (!a.time && !b.time) return 0;
return a.time.t - b.time.t;
});
},
update_field_map: function (field_map, header, fields, is_req) {
for (var i = 0; i < fields.length; i++)
{
var pos = header.indexOf(fields[i]);
if (is_req && !(pos >= 0))
{
show_error("Missing required field " + req_fields[i]);
return;
}
field_map[fields[i]] = pos;
}
},
update_pace_for_participant: function(p) {
var vm = this;
p.pace_to = [];
if (!vm.distance)
return;
p.pace = vm.get_time_obj(p.time.t/vm.distance);
var work_splits = vm.splits.slice();
work_splits.push(vm.distance);
for (var i = 0; i < work_splits.length; i++)
{
var d = work_splits[i];
var cur_t = i < p.splits.length ? p.splits[i].t : p.time.t;
var prev_t = 0,prev_d = 0;
if (i)
{
prev_d = work_splits[i-1];
prev_t = p.splits[i-1].t;
}
var dt = cur_t - prev_t;
var dd = parseFloat(d) - parseFloat(prev_d);
console.log("dd:", dd, "dt:", dt);
var pace_t = vm.get_time_obj(dd ? dt / dd : 0);
p.pace_to[d] = pace_t;
}
console.log("with splits paces:", p);
},
get_time_obj: function (t) {
if (typeof t !== "number")
return {t: null, fmt_t: ""};
return {t: t, fmt_t: fmt_time(t)};
},
get_time: function (t) {
return t ? t.fmt_t : "";
},
init_splits: function(field_map, header) {
var vm = this;
vm.splits = [];
field_map.splits = {};
for (var i = 0; i < header.length; i++)
{
var f = header[i];
if (!f.startsWith("split"))
continue;
var parts = f.split(/\s+/, 2);
vm.splits.push(parts[1]);
field_map.splits[parts[1]] = i;
}
},
init_participants: function (data) {
var vm = this;
var lines = data.split(/\n|\r\n/);
var header = parse_csv(lines[0].toLowerCase());
var field_map = {};
vm.all_fields = vm.req_fields.concat(vm.opt_fields);
vm.update_field_map(field_map, header, vm.req_fields, true);
vm.update_field_map(field_map, header, vm.opt_fields, false);
vm.init_splits(field_map, header);
vm.participants = [];
for (var i = 1; i < lines.length; i++)
{
var line_data = parse_csv(lines[i]);
if (!line_data || !line_data[field_map.name])
continue;
var o = {};
for (var j = 0; j < vm.all_fields.length; j++)
{
var f = vm.all_fields[j];
var v = line_data[field_map[f]];
if (is_time(f) && v)
{
var t = parse_time(v);
v = {t: t, fmt_t: fmt_time(t)};
}
o[f] = v;
}
vm.update_splits_for_participant(o, line_data, field_map);
vm.update_pace_for_participant(o);
//console.log("part:", o);
vm.participants.push(o);
}
},
update_splits_for_participant: function (p, line_data, field_map) {
var vm = this;
p.splits = vm.splits.map(function (s) {
return vm.get_time_obj(parse_time(line_data[field_map.splits[s]]));
});
},
set_current_participant: function (p) {
this.current_participant = p;
}
}
});
}