Skip to content

Instantly share code, notes, and snippets.

@marioloncarek
Created January 27, 2019 15:49
Show Gist options
  • Select an option

  • Save marioloncarek/15f8b5621d0ff40cb54514b9dc0ce030 to your computer and use it in GitHub Desktop.

Select an option

Save marioloncarek/15f8b5621d0ff40cb54514b9dc0ce030 to your computer and use it in GitHub Desktop.
class JSONReader {
constructor(completed = null) {
this.onCompleted = completed;
this.result = undefined;
this.input = document.createElement('input');
this.input.type = 'file';
this.input.accept = 'text/json|application/json';
this.input.addEventListener('change', this.onChange.bind(this), false);
this.input.style.display = 'none';
document.body.appendChild(this.input);
this.input.click();
}
destroy() {
this.input.removeEventListener('change', this.onChange.bind(this), false);
document.body.removeChild(this.input);
}
onChange(event) {
if (event.target.files.length > 0) {
this.readJSON(event.target.files[0]);
}
}
readJSON(file) {
const reader = new FileReader();
reader.onload = (event) => {
if (event.target.readyState === 2) {
this.result = JSON.parse(reader.result);
if (typeof this.onCompleted === 'function') {
this.onCompleted(this.result);
}
this.destroy();
}
};
reader.readAsText(file);
}
static read(callback = null) {
return new JSONReader(callback);
}
}
JSONReader.read((result) => {
console.log(result);
});
const reader = new JSONReader((result) => {
console.log(result);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment