-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #478 from slovensko-digital/GO-129/fs_drafts_uploa…
…d_drag_and_drop_zone GO-129 Add drag&drop zone to upload FS drafts
- Loading branch information
Showing
4 changed files
with
75 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Controller } from "@hotwired/stimulus" | ||
|
||
export default class extends Controller { | ||
connect() { | ||
const dropzone = document.getElementById('dropzone'); | ||
const fileInput = document.getElementById('content[]'); | ||
const fileList = document.getElementById('fileList'); | ||
|
||
dropzone.addEventListener('dragover', (e) => { | ||
e.preventDefault(); | ||
dropzone.classList.add('border-blue-500', 'border-2'); | ||
}); | ||
|
||
dropzone.addEventListener('dragleave', () => { | ||
dropzone.classList.remove('border-blue-500', 'border-2'); | ||
}); | ||
|
||
dropzone.addEventListener('drop', (e) => { | ||
e.preventDefault(); | ||
dropzone.classList.remove('border-blue-500', 'border-2'); | ||
|
||
const files = e.dataTransfer.files; | ||
this.handleFiles(files); | ||
}); | ||
|
||
fileInput.addEventListener('change', (e) => { | ||
const files = e.target.files; | ||
this.handleFiles(files); | ||
}); | ||
} | ||
|
||
handleFiles(files) { | ||
const fileInput = document.getElementById('content[]'); | ||
fileInput.files = files; | ||
|
||
for (const file of files) { | ||
const listItem = document.createElement('div'); | ||
listItem.textContent = `${file.name} (${this.formatBytes(file.size)})`; | ||
fileList.appendChild(listItem); | ||
} | ||
} | ||
|
||
formatBytes(bytes) { | ||
if (bytes === 0) return '0 Bytes'; | ||
|
||
const k = 1024; | ||
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB']; | ||
const i = Math.floor(Math.log(bytes) / Math.log(k)); | ||
|
||
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters