This is a partial code of my JavaScript app.
The openDoc
function's task is:
call newDoc
if use uploads a file through fileInput
.
Or, when user drag and drop a file in the document
.
I want to review my logic and code.
function stopDefault(event) {
event.stopPropagation();
event.preventDefault();
}
function openDoc(event) {
var files = event.target.files || event.dataTransfer.files,
file = files[0],
reader = new FileReader();
if (file) { // prevent TypeError
stopDefault(event); // prevent default file drop behavior
reader.readAsText(file);
}
reader.addEventListener("load", function(event) {
newDoc(event.target.result, file.name);
});
}
fileInput.addEventListener("change", openDoc);
document.addEventListener("dragover", stopDefault);
document.addEventListener("drop", openDoc);
If you're interested, checkout the full code - https://dl.dropboxusercontent.com/u/92126558/app.js