-
Notifications
You must be signed in to change notification settings - Fork 0
/
fodte.js
177 lines (137 loc) · 4.75 KB
/
fodte.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
let parser = new DOMParser();
// doc = parser.parseFromString(odt, "application/xml");
let first = true;
// on cherche s'il y a un fichier teacher sinon on prend le premier comme base ?
// correction calcul score ?
let headers = {
ids: [],
names: []
}
let datas = [];
let ignores = {
'radio': {
'formx:group-name': [],
'form:name': []
}
};
/**
*
* @param {string} odt content.xml
* @param {string} filename
*/
function handleOdt(odt, filename) {
doc = parser.parseFromString(odt, "application/xml");
if (first) {
handleFirst();
}
first = false;
let data = [filename];
headers.ids.forEach(function (id) {
let el = doc.querySelector('form>*[*|id="' + id + '"]'); // get the input in form part
if (el && Forms[el.tagName]) {
data.push(Forms[el.tagName].getValue(el));
}
});
datas.push(data);
updateButton();
}
function handleFirst() {
let inputs = doc.getElementsByTagName('draw:control'); // get inputs in the text part
Array.from(inputs).forEach(function (e) {
let id = e.getAttribute('draw:control');
let el = id.indexOf('"') === -1 && doc.querySelector('form>*[*|id="' + id + '"]'); // get the input in form part
if (el && Forms[el.tagName] && !Forms[el.tagName].ignore(el)) {
headers.ids.push(id);
headers.names.push(Forms[el.tagName].getName(el));
}
});
}
function exportToCsv() {
let e = datas;
let n = headers.names.slice(); // create a copy
n.unshift('Filename') // add "Filename" add the start of headers
e.unshift(n); // put headers at the start of data needing export
// log(CSV.serialize(e));
downloadText('export.csv', CSV.serialize(e))
}
function updateButton() {
let n = datas.length;
if (n) {
document.querySelector('.status button .imported').textContent = n > 1 ? '(' + n + ' fichiers importés)' : '(' + n + ' fichier importé)';
}
}
function downloadText(filename, txt) {
// Lance le téléchargement d'un fichier texte
let el = document.createElement('a');
el.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(txt));
el.setAttribute('download', filename);
el.style.display = 'none';
document.body.appendChild(el);
el.click();
document.body.removeChild(el);
}
async function handleFiles(files) {
let teacherIndex = files.findIndex(x => x.name === '_teacher.odt');
if (teacherIndex !== -1) {
files.unshift(files.splice(teacherIndex, 1)[0]);
}
for (var i = 0; i < files.length; i++) {
if (files[i].type === 'application/vnd.oasis.opendocument.text' || files[i].name.substr(-4) == '.odt') {
try {
let zip = await JSZip.loadAsync(files[i]);
let odt = await zip.file('content.xml').async('string');
handleOdt(odt, files[i].name);
} catch (e) {
alert("Erreur avec le fichier " + files[i].name);
}
} else if (files[i].type === 'application/x-zip-compressed') {
let zip = await JSZip.loadAsync(files[i]);
for (let [name, file] of Object.entries(zip.files)) { // Loop throught each files of the zip
let blobFile = await file.async('blob');
files.push(new File([blobFile], name));
}
}
}
}
function log(text) {
let p = document.createElement('pre');
p.textContent = text;
document.body.appendChild(p);
}
document.addEventListener('drop', async function (e) {
e.preventDefault();
document.body.classList.remove('hover');
document.querySelector('.status button').classList.add('disabled');
let files = Array.from(e.dataTransfer.files); // required because it will be lost during async
await handleFiles(files);
updateButton();
if (datas.length) {
document.querySelector('.status button').classList.remove('disabled');
}
});
document.addEventListener('DOMContentLoaded', function (event) {
document.querySelector('.status button').addEventListener('click', function (e) {
if (datas.length) {
exportToCsv();
}
});
});
document.addEventListener('dragover', function (e) {
e.preventDefault();
});
document.addEventListener('dragenter', function (e) {
document.body.classList.add('hover');
});
document.addEventListener('dragend', function (e) {
document.body.classList.remove('hover');
});
document.addEventListener('DOMContentLoaded', function () {
document.body.addEventListener('dragleave', function (e) {
document.body.classList.remove('hover');
});
});
window.addEventListener('load', function () {
if (location.protocol !== 'file:') {
navigator.serviceWorker.register('sw.js');
}
});