Files
fnx_web/res/include/script/homepage.js

401 lines
12 KiB
JavaScript
Raw Normal View History

2020-01-28 12:51:21 +01:00
function UploadProgressBar(uploadManager, queueDiv, file){
2020-02-04 19:37:46 +01:00
this.uploadManager = uploadManager
this.file = file
this.name = file.name
2020-01-27 16:56:16 +01:00
2020-02-04 19:37:46 +01:00
this.uploadDiv = document.createElement("a")
this.uploadDiv.classList.add("file_button")
this.uploadDiv.style.opacity = "0"
this.uploadDiv.innerText = "Queued\n" + this.file.name
queueDiv.appendChild(this.uploadDiv)
2020-01-27 16:56:16 +01:00
// Start uploading the file
2020-01-28 12:51:21 +01:00
this.uploadManager.addFile(
this.file,
this.name,
2020-02-04 19:37:46 +01:00
(progress) => { this.onProgress(progress) },
(id) => { this.onFinished(id) },
(val, msg) => { this.onFailure(val, msg) }
)
2020-01-20 12:43:43 +01:00
2020-01-27 16:56:16 +01:00
// Browsers don't render the transition if the opacity is set and
// updated in the same frame. So we have to wait a frame (or more)
// before changing the opacity to make sure the transition triggers
2020-02-04 19:37:46 +01:00
window.setTimeout(() => {this.uploadDiv.style.opacity = "1"}, 100)
2020-01-27 16:56:16 +01:00
}
2020-01-20 12:43:43 +01:00
2020-01-27 16:56:16 +01:00
UploadProgressBar.prototype.onProgress = function(progress){
this.uploadDiv.innerText = "Uploading... " + Math.round(progress*1000)/10 + "%\n" + this.name
this.uploadDiv.style.background = 'linear-gradient('
+'to right, '
2020-03-10 18:04:30 +01:00
+'var(--layer_3_color) 0%, '
2020-01-27 16:56:16 +01:00
+'var(--highlight_color) '+ ((progress*100)) +'%, '
2020-03-10 18:04:30 +01:00
+'var(--layer_3_color) '+ ((progress*100)+1) +'%)'
2020-01-27 16:56:16 +01:00
}
UploadProgressBar.prototype.onFinished = function(id){
2020-02-04 19:37:46 +01:00
console.log("Upload finished: "+this.file.name+" "+id)
2020-01-27 16:56:16 +01:00
2020-03-10 18:04:30 +01:00
this.uploadDiv.style.background = 'var(--layer_3_color)'
2020-01-27 16:56:16 +01:00
this.uploadDiv.href = '/u/'+id
this.uploadDiv.target= "_blank"
let fileImg = document.createElement("img")
fileImg.src = apiEndpoint+'/file/'+id+'/thumbnail'
fileImg.alt = this.file.name
let linkSpan = document.createElement("span")
2020-03-10 18:04:30 +01:00
linkSpan.classList = "file_button_title"
2020-01-27 16:56:16 +01:00
linkSpan.innerText = domainURL()+"/u/"+id
this.uploadDiv.innerHTML = "" // Remove uploading progress
this.uploadDiv.appendChild(fileImg)
this.uploadDiv.appendChild(document.createTextNode(this.file.name))
this.uploadDiv.appendChild(document.createElement("br"))
this.uploadDiv.appendChild(linkSpan)
}
UploadProgressBar.prototype.onFailure = function(val, msg) {
2020-01-31 21:02:37 +01:00
if (val === "") {
2020-02-04 19:37:46 +01:00
val = "Could not connect to server"
2020-01-31 21:02:37 +01:00
}
2020-01-27 16:56:16 +01:00
this.uploadDiv.innerHTML = "" // Remove uploading progress
this.uploadDiv.style.background = 'var(--danger_color)'
2020-01-31 21:02:37 +01:00
this.uploadDiv.style.color = 'var(--highlight_text_color)'
this.uploadDiv.appendChild(document.createTextNode("Upload failed: "))
this.uploadDiv.appendChild(document.createTextNode(msg+" ("+val+")"))
2020-01-27 16:56:16 +01:00
this.uploadDiv.appendChild(document.createElement("br"))
2020-01-31 21:02:37 +01:00
this.uploadDiv.appendChild(document.createTextNode(this.file.name))
2020-02-04 19:37:46 +01:00
console.log(msg)
2020-01-20 12:43:43 +01:00
}
2020-01-27 16:56:16 +01:00
2020-02-04 19:37:46 +01:00
let uploader = null
let shareTitle = ""
let shareLink = ""
2020-01-20 12:43:43 +01:00
function handleUploads(files) {
if (uploader === null){
2020-02-11 13:58:19 +01:00
uploader = new UploadManager(apiEndpoint+"/file", uploadsFinished)
2020-01-20 12:43:43 +01:00
}
2020-01-27 16:56:16 +01:00
if (files.length === 0) {
2020-02-04 19:37:46 +01:00
return
2020-01-27 16:56:16 +01:00
}
2020-01-20 12:43:43 +01:00
for (let i = 0; i < files.length; i++) {
new UploadProgressBar(
uploader,
document.getElementById("uploads_queue"),
files.item(i)
2020-02-04 19:37:46 +01:00
)
2020-01-20 12:43:43 +01:00
}
2020-02-04 19:37:46 +01:00
hideShareButtons()
2020-01-20 12:43:43 +01:00
}
function uploadsFinished() {
shareLink = ""
shareTitle = ""
// Get the finished uploads from the uploader
2020-02-04 19:37:46 +01:00
let uploadLog = uploader.finishedUploads()
2020-01-20 12:43:43 +01:00
if (uploadLog.length === 1) {
2020-02-04 19:37:46 +01:00
shareTitle = "Download "+uploadLog[0].fileName+" here"
shareLink = domainURL()+"/u/"+uploadLog[0].fileID
2020-01-20 12:43:43 +01:00
2020-02-04 19:37:46 +01:00
showShareButtons()
2020-01-20 12:43:43 +01:00
} else if (uploadLog.length > 1) {
2020-02-04 19:37:46 +01:00
let title = uploadLog.length + " files"
2020-01-20 12:43:43 +01:00
createList(
title, true,
).then(resp => {
2020-02-04 19:37:46 +01:00
console.log("Automatic list ID "+resp.id)
shareTitle = "View "+title+" here"
shareLink = domainURL()+"/l/"+resp.id
2020-01-20 12:43:43 +01:00
2020-02-04 19:37:46 +01:00
showShareButtons()
2020-01-20 12:43:43 +01:00
}).catch(err => {
2020-02-04 19:37:46 +01:00
alert("Failed to generate link. Please check your internet connection and try again.\nError: "+err)
})
2020-01-20 12:43:43 +01:00
}
}
function createList(title, anonymous) {
2020-02-04 19:37:46 +01:00
let uploads = uploader.finishedUploads()
let files = Array()
2020-01-20 12:43:43 +01:00
for (let i = 0; i < uploads.length; i++) {
2020-02-04 19:37:46 +01:00
files.push({ "id": uploads[i].fileID })
2020-01-20 12:43:43 +01:00
}
return fetch(
apiEndpoint+"/list",
{
method: "POST",
headers: {"Content-Type": "application/json; charset=UTF-8"},
body: JSON.stringify({
"title": title,
"anonymous": anonymous,
"files": files
})
}
).then(resp => {
if (!resp.ok) {
2020-02-04 19:37:46 +01:00
return Promise.reject("HTTP error: "+resp.status)
2020-01-20 12:43:43 +01:00
}
2020-02-04 19:37:46 +01:00
return resp.json()
2020-01-20 12:43:43 +01:00
})
}
function hideShareButtons() {
2020-02-04 19:37:46 +01:00
document.getElementById("instruction_3_after").style.display = "none"
2020-01-20 12:43:43 +01:00
}
function showShareButtons() {
2020-02-04 19:37:46 +01:00
document.getElementById("instruction_3_after").style.display = ""
2020-01-20 12:43:43 +01:00
if (window.navigator && window.navigator.share) {
2020-02-04 19:37:46 +01:00
document.getElementById("social_buttons").style.display = "none"
2020-01-20 12:43:43 +01:00
} else {
2020-02-04 19:37:46 +01:00
document.getElementById("navigator_share_button").style.display = "none"
2020-01-20 12:43:43 +01:00
}
}
function copyLink() {
if(copyText(shareLink)) {
2020-02-04 19:37:46 +01:00
console.log('Text copied')
document.querySelector("#btn_copy_link>span").textContent = "Copied!"
document.getElementById("btn_copy_link").classList.add("button_highlight")
2020-01-20 12:43:43 +01:00
} else {
2020-02-04 19:37:46 +01:00
console.log('Copying not supported')
alert("Your browser does not support copying text.")
2020-01-20 12:43:43 +01:00
}
}
/*
* Upload Handlers
*/
// Relay click event to hidden file field
document.getElementById("upload_file_button").onclick = function() {
2020-02-04 19:37:46 +01:00
document.getElementById("file_input_field").click()
2020-01-20 12:43:43 +01:00
}
document.getElementById("file_input_field").onchange = function(evt){
// Start uploading the files async
2020-02-04 19:37:46 +01:00
window.setTimeout(handleUploads(evt.target.files), 1)
2020-01-20 12:43:43 +01:00
// This resets the file input field
2020-02-04 19:37:46 +01:00
document.getElementById("file_input_field").nodeValue = ""
2020-01-20 12:43:43 +01:00
}
document.getElementById("upload_text_button").onclick = function() {
2020-02-04 19:37:46 +01:00
window.location.href = '/t/'
2020-01-20 12:43:43 +01:00
}
/*
* Drag 'n Drop upload handlers
*/
2020-02-04 19:37:46 +01:00
document.ondragover = function(e) {
e.preventDefault()
e.stopPropagation()
}
document.ondragenter = function(e) {
e.preventDefault()
e.stopPropagation()
}
2020-01-20 12:43:43 +01:00
document.addEventListener('drop', function(e){
if (e.dataTransfer && e.dataTransfer.files.length > 0) {
e.preventDefault()
e.stopPropagation()
// Run async to not freeze the page
2020-02-04 19:37:46 +01:00
window.setTimeout(handleUploads(e.dataTransfer.files), 1)
2020-01-20 12:43:43 +01:00
}
})
/*
* Share buttons
*/
document.getElementById("btn_social_share").addEventListener("click", function() {
window.navigator.share({
title: "Pixeldrain",
text: shareTitle,
url: shareLink
2020-02-04 19:37:46 +01:00
})
})
2020-01-20 12:43:43 +01:00
document.getElementById("btn_copy_link").addEventListener("click", function() {
2020-02-04 19:37:46 +01:00
copyLink()
})
2020-01-20 12:43:43 +01:00
document.getElementById("btn_open_link").addEventListener("click", function() {
2020-02-04 19:37:46 +01:00
window.open(shareLink, '_blank')
})
2020-01-20 12:43:43 +01:00
document.getElementById("btn_social_email").addEventListener("click", function() {
2020-02-04 19:37:46 +01:00
window.open('mailto:please@set.address?subject=File%20on%20pixeldrain&body=' + shareLink)
})
2020-01-20 12:43:43 +01:00
document.getElementById("btn_social_twitter").addEventListener("click", function() {
2020-02-04 19:37:46 +01:00
window.open('https://twitter.com/share?url=' + shareLink)
})
2020-01-20 12:43:43 +01:00
document.getElementById("btn_social_facebook").addEventListener("click", function() {
2020-02-04 19:37:46 +01:00
window.open('http://www.facebook.com/sharer.php?u=' + shareLink)
})
2020-01-20 12:43:43 +01:00
document.getElementById("btn_social_reddit").addEventListener("click", function() {
2020-02-04 19:37:46 +01:00
window.open('https://www.reddit.com/submit?url=' + shareLink)
})
2020-01-20 12:43:43 +01:00
document.getElementById("btc_social_tumblr").addEventListener("click", function() {
2020-02-04 19:37:46 +01:00
window.open('http://www.tumblr.com/share/link?url=' + shareLink)
})
2020-01-20 12:43:43 +01:00
/*
* Link copy buttons
*/
2020-02-04 19:37:46 +01:00
function renderListButton(apiURL, id, title, subtitle) {
let btn = document.createElement("a")
btn.classList = "file_button"
btn.href = "/l/"+id
btn.target = "_blank"
let thumbnail = document.createElement("img")
thumbnail.src = apiURL+"/list/"+id+"/thumbnail?width=80&height=80"
thumbnail.alt = title
let titleSpan = document.createElement("span")
titleSpan.classList = "file_button_title"
titleSpan.innerText = title
let br = document.createElement("br")
let subtitleSpan = document.createElement("span")
subtitleSpan.classList = "file_button_subtitle"
subtitleSpan.innerText = subtitle
btn.appendChild(thumbnail)
btn.appendChild(titleSpan)
btn.appendChild(br)
btn.appendChild(subtitleSpan)
return btn
}
2020-01-20 12:43:43 +01:00
// Create list button
document.getElementById("btn_create_list").addEventListener("click", function(evt) {
let title = prompt(
"You are creating a list containing " + uploader.finishedUploads().length + " files.\n"
+ "What do you want to call it?", "My New Album"
2020-02-04 19:37:46 +01:00
)
2020-01-20 12:43:43 +01:00
if(title === null){
2020-02-04 19:37:46 +01:00
return
2020-01-20 12:43:43 +01:00
}
createList(title, false).then(resp => {
document.getElementById("created_lists").appendChild(
renderListButton(
apiEndpoint,
resp.id,
domainURL()+'/l/'+resp.id,
"List creation finished!",
)
2020-02-04 19:37:46 +01:00
)
window.open('/l/'+resp.id, '_blank')
2020-01-20 12:43:43 +01:00
}).catch(err => {
let div = document.createElement("div")
2020-02-04 19:37:46 +01:00
div.className = "file_button"
2020-01-20 12:43:43 +01:00
div.innerHTML = "List creation failed<br/>"
+ "The server responded with:<br/>"
2020-02-04 19:37:46 +01:00
+ err
document.getElementById("created_lists").append(div)
})
})
2020-01-20 12:43:43 +01:00
2020-02-04 19:37:46 +01:00
let btnCopyLinks = document.getElementById("btn_copy_links")
2020-01-20 12:43:43 +01:00
btnCopyLinks.addEventListener("click", function(){
2020-02-04 19:37:46 +01:00
let text = ""
let uploads = uploader.finishedUploads()
2020-01-20 12:43:43 +01:00
// Add the text to the textarea
for (let i = 0; i < uploads.length; i++) {
// Example: https://pixeldrain.com/u/abcd1234: Some_file.png
2020-02-04 19:37:46 +01:00
text += domainURL()+"/u/"+uploads[i].fileID+" "+uploads[i].fileName+"\n"
2020-01-20 12:43:43 +01:00
}
if (shareLink.includes("/l/")) {
2020-02-04 19:37:46 +01:00
text += "\n"+shareLink+" All "+uploads.length+" files\n"
2020-01-20 12:43:43 +01:00
}
// Copy the selected text
if(copyText(text)){
2020-02-04 19:37:46 +01:00
btnCopyLinks.classList.add("button_highlight")
2020-01-20 12:43:43 +01:00
btnCopyLinks.innerHTML = "Links copied to clipboard!"
}else{
2020-02-04 19:37:46 +01:00
btnCopyLinks.classList.add("button_red")
2020-01-20 12:43:43 +01:00
btnCopyLinks.innerHTML = "Copying links failed"
}
2020-02-04 19:37:46 +01:00
})
2020-01-20 12:43:43 +01:00
2020-02-04 19:37:46 +01:00
let btnCopyBBCode = document.getElementById("btn_copy_bbcode")
2020-01-20 12:43:43 +01:00
btnCopyBBCode.addEventListener("click", function(){
2020-02-04 19:37:46 +01:00
let text = ""
let uploads = uploader.finishedUploads()
2020-01-20 12:43:43 +01:00
// Add the text to the textarea
for (let i = 0; i < uploads.length; i++) {
// Example: [url=https://pixeldrain.com/u/abcd1234]Some_file.png[/url]
2020-02-04 19:37:46 +01:00
text += "[url="+domainURL()+"/u/"+uploads[i].fileID+"]"+uploads[i].fileName+"[/url]\n"
2020-01-20 12:43:43 +01:00
}
if (shareLink.includes("/l/")) {
2020-02-04 19:37:46 +01:00
text += "\n[url="+shareLink+"]All "+uploads.length+" files[/url]\n"
2020-01-20 12:43:43 +01:00
}
// Copy the selected text
if(copyText(text)){
2020-02-04 19:37:46 +01:00
btnCopyBBCode.classList.add("button_highlight")
2020-01-20 12:43:43 +01:00
btnCopyBBCode.innerHTML = "BBCode copied to clipboard!"
}else{
2020-02-04 19:37:46 +01:00
btnCopyBBCode.classList.add("button_red")
2020-01-20 12:43:43 +01:00
btnCopyBBCode.innerHTML = "Copying links failed"
}
2020-02-04 19:37:46 +01:00
})
2020-01-20 12:43:43 +01:00
2020-02-04 19:37:46 +01:00
let btnCopyMarkdown = document.getElementById("btn_copy_markdown")
2020-01-20 19:55:51 +01:00
btnCopyMarkdown.addEventListener("click", function(){
2020-02-04 19:37:46 +01:00
let text = ""
let uploads = uploader.finishedUploads()
2020-01-20 19:55:51 +01:00
// Add the text to the textarea
for (let i = 0; i < uploads.length; i++) {
// Example: * [Some_file.png](https://pixeldrain.com/u/abcd1234)
2020-02-04 19:37:46 +01:00
if (uploads.length > 1) { text += " * " }
text += "["+uploads[i].fileName+"]("+domainURL()+"/u/"+uploads[i].fileID+")\n"
2020-01-20 19:55:51 +01:00
}
if (shareLink.includes("/l/")) {
2020-02-04 19:37:46 +01:00
text += " * [All "+uploads.length+" files]("+shareLink+")\n"
2020-01-20 19:55:51 +01:00
}
// Copy the selected text
if(copyText(text)){
2020-02-04 19:37:46 +01:00
btnCopyMarkdown.classList.add("button_highlight")
2020-01-20 19:55:51 +01:00
btnCopyMarkdown.innerHTML = "Markdown copied to clipboard!"
}else{
2020-02-04 19:37:46 +01:00
btnCopyMarkdown.classList.add("button_red")
2020-01-20 19:55:51 +01:00
btnCopyMarkdown.innerHTML = "Copying links failed"
}
2020-02-04 19:37:46 +01:00
})
2020-01-20 19:55:51 +01:00
2020-01-20 12:43:43 +01:00
/*
* Keyboard shortcuts
*/
document.addEventListener("keydown", function(event){
2020-04-14 16:00:24 +02:00
if (event.ctrlKey || event.altKey || event.metaKey) {
2020-01-20 12:43:43 +01:00
return // prevent custom shortcuts from interfering with system shortcuts
}
2020-01-27 16:56:16 +01:00
if (event.keyCode === 67) { // c
2020-01-20 12:43:43 +01:00
// Copy links to clipboard
2020-02-04 19:37:46 +01:00
document.getElementById("btn_copy_link").click()
2020-01-27 16:56:16 +01:00
} else if (event.keyCode === 85) { // u
2020-01-20 12:43:43 +01:00
// Click the upload button
2020-02-04 19:37:46 +01:00
document.getElementById("file_input_field").click()
2020-01-27 16:56:16 +01:00
} else if (event.keyCode === 84) { // t
2020-01-20 12:43:43 +01:00
// Click the text button
2020-02-04 19:37:46 +01:00
document.getElementById("upload_text_button").click()
2020-01-20 12:43:43 +01:00
}
2020-01-27 16:56:16 +01:00
console.log(event.keyCode)
2020-02-04 19:37:46 +01:00
})