dees-catalog/ts_web/elements/dees-input-fileupload.ts

170 lines
4.0 KiB
TypeScript
Raw Normal View History

2021-05-05 20:55:49 +00:00
import {
customElement,
DeesElement,
TemplateResult,
property,
html,
css,
unsafeCSS,
cssManager,
} from '@designestate/dees-element';
2021-08-19 22:25:14 +00:00
import * as domtools from '@designestate/dees-domtools';
2021-05-05 20:55:49 +00:00
declare global {
interface HTMLElementTagNameMap {
'dees-input-fileupload': DeesInputFileupload;
}
}
@customElement('dees-input-fileupload')
export class DeesInputFileupload extends DeesElement {
public static demo = () => html`<dees-input-fileupload></dees-input-fileupload>`;
2021-08-19 22:25:14 +00:00
// INSTANCE
public changeSubject = new domtools.rxjs.Subject();
2021-05-05 20:55:49 +00:00
@property({
type: String,
})
public label: string = null;
@property({
type: String,
})
public key: string;
@property({
attribute: false,
})
public value: File[] = [];
@property()
public state: 'idle' | 'dragOver' | 'dropped' | 'uploading' | 'completed' = 'idle';
2021-08-25 11:51:55 +00:00
@property({
type: Boolean,
})
public required: boolean = false;
2021-08-26 19:30:35 +00:00
@property({
2022-12-06 12:11:06 +00:00
type: Boolean,
2021-08-26 19:30:35 +00:00
})
public disabled: boolean = false;
2022-12-06 12:11:06 +00:00
@property({
type: String,
})
public buttonText: string = 'Upload File...';
2021-08-25 11:51:55 +00:00
constructor() {
2022-12-06 12:11:06 +00:00
super();
2021-08-25 11:51:55 +00:00
}
2021-05-05 20:55:49 +00:00
public static styles = [
cssManager.defaultStyles,
css`
:host {
position: relative;
display: grid;
margin: 10px 0px;
margin-bottom: 24px;
}
.maincontainer {
color: ${cssManager.bdTheme('#333', '#ccc')};
}
.label {
font-size: 14px;
margin-bottom: 5px;
}
.uploadButton {
position: relative;
cursor: pointer;
2022-12-06 12:11:06 +00:00
padding: 8px;
max-width: 600px;
2021-05-05 20:55:49 +00:00
background: ${cssManager.bdTheme('#fafafa', '#333333')};
border-radius: 3px;
text-align: center;
}
.uploadButton::after {
2022-12-06 12:11:06 +00:00
top: 2px;
right: 2px;
left: 2px;
bottom: 2px;
transform: scale3d(0.98, 0.9, 1);
2021-05-05 20:55:49 +00:00
position: absolute;
content: '';
display: block;
2022-12-06 12:11:06 +00:00
border: 2px dashed rgba(255, 255, 255, 0);
2021-05-05 20:55:49 +00:00
transition: all 0.2s;
}
.uploadButton.dragOver::after {
transform: scale3d(1, 1, 1);
2022-12-06 12:11:06 +00:00
border: 2px dashed rgba(255, 255, 255, 0.3);
}
.uploadCandidate {
text-align: left;
border-bottom: 1px dashed #444;
color: #fff;
padding: 8px;
font-family: 'Roboto Mono';
}
.uploadCandidate:last-child {
margin-bottom: 8px;
2021-05-05 20:55:49 +00:00
}
`,
];
public render(): TemplateResult {
return html`
<div class="maincontainer">
${this.label ? html`<div class="label">${this.label}</div>` : null}
2022-12-06 12:11:06 +00:00
<div class="uploadButton ${this.state === 'dragOver' ? 'dragOver' : ''}">
${this.value.map((fileArg) => html` <div class="uploadCandidate">${fileArg.name} | ${fileArg.size}</div> `)}
${this.buttonText}
</div>
2021-05-05 20:55:49 +00:00
</div>
`;
}
public async updateValue(eventArg: Event) {
const target: any = eventArg.target;
this.value = target.value;
2021-08-19 22:25:14 +00:00
this.changeSubject.next(this);
2021-05-05 20:55:49 +00:00
}
public firstUpdated() {
const dropArea = this.shadowRoot.querySelector('.uploadButton');
const handlerFunction = (eventArg: DragEvent) => {
eventArg.preventDefault();
2022-12-06 12:11:06 +00:00
switch (eventArg.type) {
2021-05-05 20:55:49 +00:00
case 'dragover':
this.state = 'dragOver';
2022-12-06 12:11:06 +00:00
this.buttonText = 'release to upload file...';
2021-05-05 20:55:49 +00:00
break;
case 'dragleave':
this.state = 'idle';
2022-12-06 12:11:06 +00:00
this.buttonText = 'Upload File...';
2021-05-05 20:55:49 +00:00
break;
2022-12-06 12:11:06 +00:00
case 'drop':
this.state = 'idle';
this.buttonText = 'Upload more files...';
2021-05-05 20:55:49 +00:00
}
console.log(eventArg);
for (const file of Array.from(eventArg.dataTransfer.files)) {
this.value.push(file);
2022-12-06 12:11:06 +00:00
this.requestUpdate();
}
2021-05-05 20:55:49 +00:00
console.log(`Got ${this.value.length} files!`);
};
dropArea.addEventListener('dragenter', handlerFunction, false);
dropArea.addEventListener('dragleave', handlerFunction, false);
dropArea.addEventListener('dragover', handlerFunction, false);
dropArea.addEventListener('drop', handlerFunction, false);
}
}