1
0
Fork 0
BangleApps/apps/gipy/interface.html

86 lines
2.6 KiB
HTML
Raw Normal View History

2022-11-07 13:03:15 +00:00
<html>
<head>
<link rel="stylesheet" href="../../css/spectre.min.css">
</head>
<body>
<p>Please select a gpx file to be converted to gpc and loaded.</p>
<input type="file" is="gpx_file" id="fileInput" accept=".gpx">
<script src="../../core/lib/interface.js"></script>
2022-11-08 08:06:58 +00:00
<script>
function onInit() {
}
</script>
2022-11-07 13:03:15 +00:00
<script type="module">
2022-11-08 17:00:19 +00:00
// Get the JS needed to upload a file
function getUploadFileCode(fileName, contents) {
var js = [];
if ("string" != typeof contents)
throw new Error("Expecting a string for contents");
if (fileName.length==0 || fileName.length>28)
throw new Error("Invalid filename length");
var fn = JSON.stringify(fileName);
for (var i=0;i<contents.length;i+=CHUNKSIZE) {
var part = contents.substr(i,CHUNKSIZE);
js.push(`require("Storage").write(${fn},atob(${JSON.stringify(btoa(part))}),${i}${(i==0)?","+contents.length:""})`);
}
return js.join("\n");
}
// Upload a file
function uploadFile(fileName, contents, callback) {
let js = "\x10"+getUploadFileCode(fileName, contents).replace(/\n/g,"\n\x10");
let fun = "\x10(function() {" + js + ";\n\x10Bluetooth.print(\"OK\");})()\n";
Puck.write(fun, callback);
}
2022-11-08 07:37:09 +00:00
2022-11-07 13:03:15 +00:00
import init, { convert_gpx_strings } from "./pkg/gpconv.js";
console.log("imported wasm");
document.getElementById('fileInput').addEventListener('change', function selectedFileChanged() {
if (this.files.length === 0) {
console.log('No file selected.');
return;
}
let gpx_filename = this.files[0].name;
let gpc_filename = gpx_filename.slice(0, gpx_filename.length-4) + ".gpc";
while (gpc_filename.length > 28) {
let new_name = prompt("enter a shorter destination filename than '" + gpc_filename + "' (28 chars max)", gpc_filename);
if (new_name != null && new_name.slice(new_name.length-4) == ".gpc") {
gpc_filename = new_name;
} else {
return;
}
}
const reader = new FileReader();
reader.onload = function fileReadCompleted() {
console.log("reading file completed");
init().then(() => {
let gpc_file = convert_gpx_strings(reader.result);
2022-11-08 17:00:19 +00:00
let gpc_string = String.fromCharCode.apply(String, gpc_file));
2022-11-07 13:03:15 +00:00
console.log("uploading");
2022-11-08 17:00:19 +00:00
uploadFile(gpc_filename, gpc_string, ret => {
console.log("uploadFile", ret);
if (ret == "OK")
clean();
});
2022-11-07 13:03:15 +00:00
});
}
reader.readAsText(this.files[0]);
});
</script>
</body>
</html>