Seyi Adebajo 1c02e757ab for TypeScript breaking change with keyof operator now supporting number and symbol type, use extract to specify string type
upgrades various dependencies. renames mirage .js files to .ts. fixes linting issues. rebuilds lock file

removes redux deps

upgrades to latest

Revert "upgrades to latest"

This reverts commit 45d2d45b0a28db3217863b2ac9492c5bfe67bf0a.

 Committer: Seyi Adebajo <sadebajo@linkedin.com>

reoders tsconfig keys
2018-08-05 14:24:09 -07:00

60 lines
1.8 KiB
TypeScript

import Component from '@ember/component';
import { get, computed } from '@ember/object';
import ComputedProperty from '@ember/object/computed';
import { action } from '@ember-decorators/object';
export default class JsonUpload extends Component {
classNames = ['nacho-uploader'];
/**
* External action to receive json text resource
* @memberof JsonUpload
*/
receiveJsonFile: (textBlob: string) => void;
/**
* Handles the change event on the json-upload DOM element
* @param {FileList} files extracts a FileList object from the event object
* @return {void | null}
*/
change({ target }: Event) {
const { files } = <HTMLInputElement>target;
return files && files.length ? this.sendFileAsText(Array.from(files).shift()) : null;
}
/**
* Caches a unique id for this instances HTML Element for file upload
* @type {ComputedProperty<string>}
* @memberof JsonUpload
*/
uploadInputId: ComputedProperty<string> = computed(function() {
return `${get(this, 'elementId')}-uploader`;
});
/**
* Reads a file as a text string and passes the output to the closure action
* @param {File} [fileBlob]
* @memberof JsonUpload
*/
sendFileAsText(fileBlob?: File) {
const closureAction = get(this, 'receiveJsonFile');
const reader = new FileReader();
if (typeof closureAction === 'function' && fileBlob) {
reader.onload = ({ target }: ProgressEvent & { target: FileReader }) =>
target && closureAction(String(target.result));
reader.readAsText(fileBlob);
}
}
/**
* Proxies the user's click interaction on the styled upload button to the file input element
* @returns {JQuery<HTMLElement>}
* @memberof JsonUpload
*/
@action
onUpload(): JQuery<HTMLElement> {
return this.$(`#${get(this, 'uploadInputId')}`).click();
}
}