2021-04-22 14:58:37 +00:00
|
|
|
import Component from '@glimmer/component';
|
2018-09-25 16:28:26 +00:00
|
|
|
import { set } from '@ember/object';
|
2021-04-22 14:58:37 +00:00
|
|
|
import { action } from '@ember/object';
|
|
|
|
import { tracked } from '@glimmer/tracking';
|
|
|
|
import { guidFor } from '@ember/object/internals';
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2021-04-22 14:58:37 +00:00
|
|
|
/**
|
|
|
|
* @module TextFile
|
|
|
|
* `TextFile` components are file upload components where you can either toggle to upload a file or enter text.
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* <TextFile
|
|
|
|
* @inputOnly={{true}}
|
|
|
|
* @helpText="help text"
|
|
|
|
* @file={{object}}
|
|
|
|
* @onChange={{action "someOnChangeFunction"}}
|
|
|
|
* @label={{"string"}}
|
|
|
|
* />
|
|
|
|
*
|
|
|
|
* @param [inputOnly] {bool} - When true, only the file input will be rendered
|
|
|
|
* @param [helpText] {string} - Text underneath label.
|
|
|
|
* @param file {object} - * Object in the shape of:
|
|
|
|
* {
|
|
|
|
* value: 'file contents here',
|
|
|
|
* fileName: 'nameOfFile.txt',
|
|
|
|
* enterAsText: boolean ability to enter as text
|
|
|
|
* }
|
|
|
|
* @param [onChange=Function.prototype] {Function|action} - A function to call when the value of the input changes.
|
|
|
|
* @param [label=null] {string} - Text to use as the label for the file input. If null, a default will be rendered.
|
|
|
|
*/
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2021-04-22 14:58:37 +00:00
|
|
|
export default class TextFile extends Component {
|
|
|
|
fileHelpText = 'Select a file from your computer';
|
|
|
|
textareaHelpText = 'Enter the value as text';
|
|
|
|
elementId = guidFor(this);
|
|
|
|
index = '';
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2021-04-22 14:58:37 +00:00
|
|
|
@tracked file = null;
|
|
|
|
@tracked showValue = false;
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2021-04-22 14:58:37 +00:00
|
|
|
get inputOnly() {
|
|
|
|
return this.args.inputOnly || false;
|
|
|
|
}
|
|
|
|
get label() {
|
|
|
|
return this.args.label || null;
|
|
|
|
}
|
2018-04-03 14:16:57 +00:00
|
|
|
|
|
|
|
readFile(file) {
|
|
|
|
const reader = new FileReader();
|
|
|
|
reader.onload = () => this.setFile(reader.result, file.name);
|
|
|
|
reader.readAsText(file);
|
2021-04-22 14:58:37 +00:00
|
|
|
}
|
2018-04-03 14:16:57 +00:00
|
|
|
|
|
|
|
setFile(contents, filename) {
|
2021-04-22 14:58:37 +00:00
|
|
|
this.args.onChange(this.index, { value: contents, fileName: filename });
|
|
|
|
}
|
2018-04-03 14:16:57 +00:00
|
|
|
|
2021-04-22 14:58:37 +00:00
|
|
|
@action
|
|
|
|
pickedFile(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
const { files } = e.target;
|
|
|
|
if (!files.length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (let i = 0, len = files.length; i < len; i++) {
|
|
|
|
this.readFile(files[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@action
|
|
|
|
updateData(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
let file = this.args.file;
|
|
|
|
set(file, 'value', e.target.value);
|
|
|
|
this.args.onChange(this.index, file);
|
|
|
|
}
|
|
|
|
@action
|
|
|
|
clearFile() {
|
|
|
|
this.args.onChange(this.index, { value: '' });
|
|
|
|
}
|
|
|
|
@action
|
|
|
|
toggleMask() {
|
|
|
|
this.showValue = !this.showValue;
|
|
|
|
}
|
|
|
|
}
|