Last active
February 6, 2019 15:02
-
-
Save tomtheisen/ac80f447622b5d4d2695228506fe9952 to your computer and use it in GitHub Desktop.
Vendor Convert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Vendor Convert</title> | |
<meta charset=utf-8> | |
<style> | |
body { | |
font-size: 200%; | |
font-family: sans-serif; | |
color: firebrick; | |
background: burlywood; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>Thoracic Harvest Vendor Conversion</h1> | |
<ol> | |
<li> | |
Choose a file<br> | |
<input type=file id=file> | |
</li> | |
<li hidden> | |
Detected vendor: | |
<span id=detected-vendor></span> | |
</li> | |
<li hidden> | |
<form id=convert> | |
New target vendor: <br> | |
<input id=new-vendor required> | |
<input type=submit value=Convert> | |
</form> | |
</li> | |
<li hidden> | |
<a id=result>Result</a> | |
</li> | |
</ol> | |
<script src=index.js></script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fileInput = document.getElementById("file") as HTMLInputElement; | |
const items = document.getElementsByTagName("li"); | |
const convertForm = document.getElementById("convert") as HTMLFormElement; | |
const detectedVendor = document.getElementById("detected-vendor") as HTMLSpanElement; | |
let contents: string[], vendor: string, lineBreak: string; | |
function detectVendor(lines: string[]): string { | |
return "NIE"; | |
} | |
fileInput.value = ""; | |
fileInput.addEventListener("change", () => { | |
if (fileInput.files) { | |
const reader = new FileReader; | |
reader.addEventListener("loadend", ev => { | |
let whole = reader.result as string; | |
let match = whole.match(/\r?\n/); | |
if (!match) return | |
lineBreak = match[0]; | |
contents = whole.split(lineBreak); | |
if (contents.length > 1 && contents[contents.length - 1] === "") contents.pop(); | |
detectedVendor.innerHTML = vendor = detectVendor(contents); | |
fileInput.disabled = true; | |
items[2].hidden = items[1].hidden = false; | |
}); | |
reader.readAsText(fileInput.files[0]); | |
} | |
}, { once: true }); | |
convertForm.addEventListener("submit", ev => { | |
ev.preventDefault(); | |
let newVendor = (document.getElementById("new-vendor") as HTMLInputElement).value; | |
let blob = new Blob(contents.map(line => { | |
return "NIE: " + line + lineBreak; | |
})); | |
items[3].hidden = false; | |
let result = document.getElementById("result") as HTMLAnchorElement; | |
result.href = URL.createObjectURL(blob); | |
result.download = fileInput.value.split(/[\\/]/g).pop() || ""; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment