This repository has been archived by the owner on Jun 28, 2022. It is now read-only.
/
importSched.js
48 lines (45 loc) · 1.55 KB
/
importSched.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
function readCalFile() {
var files = $('#schedInput')[0].files;
if (!files || !files.length) {
return;
}
var file = files[0];
var reader = new FileReader();
// If we use onloadend, we need to check the readyState.
reader.onloadend = function (evt) {
if (evt.target.readyState === FileReader.DONE) {
// DONE == 2
var secArr = parseCalFile(evt.target.result);
if (secArr.length) {
$('#importSubmit').prop('disabled', false);
} else {
$('#importSubmit').prop('disabled', true);
}
$('#secsToImport').empty();
for (var i = 0; i < secArr.length; i++) {
$('#secsToImport').append('<input type="checkbox" name="' + FormatID(secArr[i]).join('-') + '" checked><span style="display:inline">' + FormatID(secArr[i]).join(' ') + '</span><br>');
}
return secArr;
} else {
$('#importSubmit').prop('disabled', true);
return [];
}
};
var blob = file.slice(0, file.size - 1);
reader.readAsBinaryString(blob);
}
function parseCalFile(rawCal) {
function FilterFunc(line) {
if (line.split(':')[0] === 'SUMMARY') {
return 1;
}
}
function MapFunc(line) {
return line.split('\r')[0].replace(/ /g, '').split(':')[1];
}
var secs = rawCal.split('\n').filter(FilterFunc).map(MapFunc);
var uniq = secs.filter(function (elem, pos) {
return secs.indexOf(elem) === pos;
});
return uniq;
}