forked from wylovan/qti30Upgrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
site.js
141 lines (131 loc) · 4.32 KB
/
site.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
/*
* site.js
*/
import * as fflate from 'https://cdn.skypack.dev/fflate?min';
import {attachBehaviors} from './rn.js';
//console.log(SaxonJS.getProcessorInfo());
//console.log('fflate', fflate);
const domParser = new DOMParser(),
xmlSerializer = new XMLSerializer(),
inputFiles = document.getElementById('inputFiles'),
inputShowInPage = document.getElementById('inputShowInPage'),
inputCreateZip = document.getElementById('inputCreateZip');
let results = document.getElementById('results'),
xslJson = null;
fetch('qti2xTo30.sef.json')
.then(res => res.json())
.then(data => {
xslJson = data;
console.log('Stylesheet read from SEF.')
})
.catch(err => {
console.log('SEF not available. Getting XSL...');
const loc = window.location;
SaxonJS.getResource({
location: `${loc.protocol}//${loc.host}${loc.pathname.replace(/^(.*\/).*$/, '$1')}qti2xTo30.xsl`,
type: 'xml'
})
.then(doc => {
xslJson = SaxonJS.compile(doc);
console.log('Stylesheet compiled from XSL.')
})
.catch(err => {
console.log('Error getting XSL resource.', err);
alert('No XSL available!')
});
});
function transform(txt) {
return SaxonJS.transform({
stylesheetInternal: xslJson,
sourceText: txt,
destination: 'serialized'
}, 'async')
.then (output => {
return output.principalResult;
})
.catch(err => {
console.log('ERROR:', err);
return txt;
});
}
function showInPage(fileName, content) {
let result = document.createElement('div'),
h2 = document.createElement('h2'),
pre = document.createElement('pre');
h2.append(fileName);
if (content.code) {
result.classList.add('transformerror');
pre.append(content.code + ': ' + content.message);
}
else
pre.append(content);
result.append(h2, pre);
results.append(result);
}
function zipFiles(values) {
const zipOut = [];
const zipFile = new fflate.Zip();
zipFile.ondata = (err, dat, final) => {
if (err) throw err;
zipOut.push(dat);
if (final) {
const blob = new Blob(zipOut, { type: 'application/zip' });
var fileName = 'qti-' + new Date().toISOString() + '.zip';
var tempEl = document.createElement("a");
document.body.appendChild(tempEl);
tempEl.style = "display: none";
var url = window.URL.createObjectURL(blob);
tempEl.href = url;
tempEl.download = fileName;
tempEl.click();
window.URL.revokeObjectURL(url);
}
}
values.forEach((v, i) => {
if (v !== undefined) {
const val = fflate.strToU8(v.result),
defl = new fflate.AsyncZipDeflate(v.filename, { level: 9 });
zipFile.add(defl);
defl.push(val, true);
}
});
console.log('END');
zipFile.end();
}
function processFiles() {
results.innerHTML = '';
let promises = [];
for (var i = 0; i < inputFiles.files.length; i++) {
let file = inputFiles.files[i];
console.log('Processing file: ', file.name);
promises.push(
file.text()
.then(txt => {
return transform(txt);
})
.then(transformed => {
if (inputShowInPage.checked)
showInPage(file.name, transformed);
return {
filename: file.name,
result: transformed
};
})
.catch(err => {
console.log('ERROR: ', err);
if (inputShowInPage.checked)
showInPage(file.name, err);
})
);
}
if (inputCreateZip.checked)
Promise.all(promises).then(zipFiles);
}
attachBehaviors({
'form#uploadForm': function(idx, elm) {
elm.addEventListener('submit', function(evt) {
evt.preventDefault();
processFiles();
});
}
});