Files
fnx_web/res/static/script/Toolbar.js

344 lines
9.7 KiB
JavaScript
Raw Normal View History

2018-11-06 22:19:57 +01:00
/*
2017-11-10 12:39:55 +01:00
* Time for a more Java-like approach.
2018-11-06 22:19:57 +01:00
*
2017-11-10 12:39:55 +01:00
* Feel free to use this of course
2018-11-06 22:19:57 +01:00
*
2017-11-10 12:39:55 +01:00
* Made by Fornax
*/
2019-02-20 22:46:35 +01:00
/* global Viewer */
2017-11-10 12:39:55 +01:00
var Toolbar = {
visible: true,
toggle: function () {
if (this.visible) {
if (Sharebar.visible) {
Sharebar.toggle();
}
2019-05-13 21:05:29 +02:00
document.getElementById("toolbar").style.left = "-9em";
2019-02-20 22:46:35 +01:00
document.getElementById("filepreview").style.left = "0px";
2017-11-10 12:39:55 +01:00
this.visible = false;
} else {
2019-02-20 22:46:35 +01:00
document.getElementById("toolbar").style.left = "0px";
2019-05-13 21:05:29 +02:00
document.getElementById("filepreview").style.left = "8em";
2017-11-10 12:39:55 +01:00
this.visible = true;
}
},
download: function () {
2019-03-27 23:57:53 +01:00
var triggerDL = function(){
document.getElementById("download_frame").src = "/api/file/" + Viewer.currentFile + "?download";
}
if (captchaKey === "a"){
// If the server doesn't support captcha there's no use in checking
// availability
triggerDL();
return;
}
$.getJSON(
apiEndpoint + "/file/" + Viewer.currentFile + "/availability"
).done(function(data){
if(data.success === true){
// Downloading is allowed, start the download
triggerDL();
}
}).fail(function(data){
if(data.responseJSON.success === false) {
var popupDiv = document.getElementById("captcha_popup");
2019-03-28 10:47:27 +01:00
var popupTitle = document.getElementById("captcha_popup_title");
var popupContent = document.getElementById("captcha_popup_content");
var popupCaptcha = document.getElementById("captcha_popup_captcha");
2019-03-27 23:57:53 +01:00
if(data.responseJSON.value === "file_rate_limited_captcha_required") {
2019-03-28 10:47:27 +01:00
popupTitle.innerText = "Rate limiting enabled!";
popupContent.innerText = "This file is using a suspicious "+
"amount of bandwidth relative to its popularity. To "+
"continue downloading this file you will have to "+
"prove that you're a human first.";
2019-03-27 23:57:53 +01:00
}else if(data.responseJSON.value === "virus_detected_captcha_required"){
2019-03-28 10:47:27 +01:00
popupTitle.innerText = "Malware warning!";
popupContent.innerText = "According to our scanning "+
2019-03-31 21:06:52 +02:00
"systems this file may contain a virus of type '"+
data.responseJSON.extra+"'. You can continue "+
2019-03-28 10:47:27 +01:00
"downloading this file at your own risk, but you will "+
"have to prove that you're a human first.";
2019-03-27 23:57:53 +01:00
}
2019-03-28 10:47:27 +01:00
// Load the recaptcha script with a load function
$.getScript("https://www.google.com/recaptcha/api.js?onload=loadCaptcha&render=explicit");
2019-03-27 23:57:53 +01:00
popupDiv.style.opacity = "1";
popupDiv.style.visibility = "visible";
}else{
// No JSON, try download anyway
triggerDL();
}
});
2017-11-10 12:39:55 +01:00
},
downloadList: function(){
if(!Viewer.isList){
return;
}
2019-02-20 22:46:35 +01:00
document.getElementById("download_frame").src = "/api/list/" + Viewer.listId + "/zip";
2017-11-10 12:39:55 +01:00
},
copyUrl: function () {
2019-02-20 22:46:35 +01:00
if(copyText(window.location.href)) {
2017-11-10 12:39:55 +01:00
console.log('Text copied');
$("#btnCopy>span").text("Copied!");
2019-02-20 22:46:35 +01:00
document.getElementById("btnCopy").classList.add("button_highlight")
} else {
2017-11-10 12:39:55 +01:00
console.log('Copying not supported');
$("#btnCopy>span").text("Error!");
alert("Your browser does not support copying text.");
}
2018-11-06 22:19:57 +01:00
2017-11-10 12:39:55 +01:00
// Return to normal
2018-01-07 21:42:19 +01:00
setTimeout(function(){
$("#btnCopy>span").text("Copy");
2019-02-20 22:46:35 +01:00
document.getElementById("btnCopy").classList.remove("button_highlight")
2019-02-17 22:44:37 +01:00
}, 60000);
2017-11-10 12:39:55 +01:00
},
2019-03-26 20:53:19 +01:00
setStats: function(views, downloads){
document.getElementById("stat_views").innerText = views
document.getElementById("stat_downloads").innerText = Math.round(downloads*10)/10;
2019-02-20 22:46:35 +01:00
}
};
2019-06-04 20:14:25 +02:00
2019-02-20 22:46:35 +01:00
var Sharebar = {
visible: false,
toggle: function(){
2019-06-04 20:14:25 +02:00
if (navigator.share) {
navigator.share({
title: Viewer.title,
text: "Get this file from Pixeldrain!",
url: window.location.href
});
return;
}
2019-02-20 22:46:35 +01:00
if (!Toolbar.visible){
Toolbar.toggle();
}
if(this.visible){
2019-05-13 21:05:29 +02:00
document.getElementById("sharebar").style.left = "-8em";
2019-02-20 22:46:35 +01:00
document.getElementById("btnShare").classList.remove("button_highlight")
this.visible = false;
}else{
2019-05-13 20:33:31 +02:00
document.getElementById("sharebar").style.left = "8em";
2019-02-20 22:46:35 +01:00
document.getElementById("btnShare").classList.add("button_highlight")
this.visible = true;
}
}
};
function copyText(text) {
// Create a textarea to copy the text from
var ta = document.createElement("textarea");
ta.setAttribute("readonly", "readonly")
ta.style.position = "absolute";
ta.style.left = "-9999px";
ta.value = text; // Put the text in the textarea
// Add the textarea to the DOM so it can be seleted by the user
document.body.appendChild(ta);
ta.select(); // Select the contents of the textarea
var success = document.execCommand("copy"); // Copy the selected text
document.body.removeChild(ta); // Remove the textarea
return success;
}
2019-03-28 10:47:27 +01:00
function loadCaptcha(){
grecaptcha.render("captcha_popup_captcha", {
sitekey: captchaKey,
theme: "dark",
callback: function(token){
document.getElementById("download_frame").src = "/api/file/" + Viewer.currentFile +
"?download&recaptcha_response="+token;
2019-03-31 21:06:52 +02:00
2019-03-28 10:47:27 +01:00
setTimeout(function(){
var popupDiv = document.getElementById("captcha_popup");
popupDiv.style.opacity = "0";
popupDiv.style.visibility = "hidden";
}, 1000)
}
});
}
2019-02-20 22:46:35 +01:00
var DetailsWindow = {
visible: false,
2019-07-07 01:03:44 +02:00
fileID: "",
2019-07-07 12:09:13 +02:00
graph: 0,
2019-07-06 18:41:16 +02:00
popupDiv: document.getElementById("details_popup"),
2019-02-20 22:46:35 +01:00
detailsButton: document.getElementById("btnDetails"),
toggle: function () {
if (this.visible) {
this.popupDiv.style.opacity = "0"
this.popupDiv.style.visibility = "hidden"
this.detailsButton.classList.remove("button_highlight")
this.visible = false;
} else {
this.popupDiv.style.opacity = "1"
this.popupDiv.style.visibility = "visible"
this.detailsButton.classList.add("button_highlight")
this.visible = true;
2019-07-07 12:09:13 +02:00
2019-07-07 22:34:20 +02:00
// This is a workaround for a chrome bug which makes it so hidden
// windows can't be scrolled after they are shown
this.popupDiv.focus();
2019-07-07 12:09:13 +02:00
if (this.graph === 0) {
this.renderGraph();
}
this.updateGraph(this.fileID);
2019-02-20 22:46:35 +01:00
}
},
setDetails: function (file) {
2019-07-07 12:09:13 +02:00
var that = this;
2019-02-20 22:46:35 +01:00
if (Viewer.isList) {
// Lists give incomplete file information, so we have to request
// more details in the background. File descriptions only exist in
// lists, so for that we use the data provided in the page source
$.ajax({
dataType: "json",
url: apiEndpoint + "/file/" + file.id + "/info",
success: function(data){
2019-07-07 12:09:13 +02:00
that.fileID = data.id;
2019-02-20 22:46:35 +01:00
$("#info_file_details").html(
"<table>"
+ "<tr><td>Name<td><td>" + escapeHTML(data.name) + "</td></tr>"
+ "<tr><td>Url<td><td><a href=\"/u/" + data.id + "\">/u/" + data.id + "</a></td></tr>"
+ "<tr><td>Mime Type<td><td>" + escapeHTML(data.mime_type) + "</td></tr>"
2019-03-26 20:53:19 +01:00
+ "<tr><td>ID<td><td>" + data.id + "</td></tr>"
2019-02-20 22:46:35 +01:00
+ "<tr><td>Size<td><td class=\"bytecounter\">" + data.size + "</td></tr>"
2019-03-26 20:53:19 +01:00
+ "<tr><td>Bandwidth<td><td class=\"bytecounter\">" + data.bandwidth_used + "</td></tr>"
2019-02-20 22:46:35 +01:00
+ "<tr><td>Upload Date<td><td>" + data.date_upload + "</td></tr>"
+ "<tr><td>Description<td><td>" + escapeHTML(file.description) + "</td></tr>"
+ "</table>"
);
2019-03-26 20:53:19 +01:00
Toolbar.setStats(data.views, data.bandwidth_used/data.size);
2019-07-07 12:09:13 +02:00
if(that.visible) {
that.updateGraph(that.fileID);
2019-07-07 01:03:44 +02:00
}
2019-02-20 22:46:35 +01:00
}
});
} else {
2019-07-07 01:03:44 +02:00
this.fileID = file.id;
2019-02-20 22:46:35 +01:00
$("#info_file_details").html(
"<table>"
+ "<tr><td>Name<td><td>" + escapeHTML(file.name) + "</td></tr>"
+ "<tr><td>Mime Type<td><td>" + escapeHTML(file.mime_type) + "</td></tr>"
+ "<tr><td>ID<td><td>" + file.id + "</td></tr>"
+ "<tr><td>Size<td><td class=\"bytecounter\">" + file.size + "</td></tr>"
2019-03-26 20:53:19 +01:00
+ "<tr><td>Bandwidth<td><td class=\"bytecounter\">" + file.bandwidth_used + "</td></tr>"
2019-02-20 22:46:35 +01:00
+ "<tr><td>Upload Date<td><td>" + file.date_upload + "</td></tr>"
+ "</table>"
);
2019-03-26 20:53:19 +01:00
Toolbar.setStats(file.views, file.bandwidth_used/file.size);
2019-07-07 01:03:44 +02:00
if(this.visible) {
2019-07-07 12:09:13 +02:00
this.updateGraph(file.id);
2019-07-07 01:03:44 +02:00
}
2019-02-20 22:46:35 +01:00
}
2019-07-07 01:03:44 +02:00
},
2019-07-07 12:09:13 +02:00
updateGraph: function(fileID) {
var that = this;
console.log("updating graph "+fileID);
2019-07-07 01:03:44 +02:00
$.get(apiEndpoint+"/file/" + fileID + "/timeseries", function(response){
console.log(response);
if (response.success) {
2019-07-07 12:09:13 +02:00
that.graph.data.labels = response.labels;
that.graph.data.datasets[0].data = response.downloads;
that.graph.data.datasets[1].data = response.views;
that.graph.update();
}
});
},
renderGraph: function() {
console.log("rendering graph");
Chart.defaults.global.defaultFontColor = "#b3b3b3";
Chart.defaults.global.defaultFontSize = 16;
Chart.defaults.global.defaultFontFamily = "Ubuntu";
this.graph = new Chart(
document.getElementById('bandwidth_chart'),
{
type: 'line',
data: {
datasets: [
{
label: "Downloads",
2019-07-07 12:18:26 +02:00
backgroundColor: "rgba(100, 255, 100, .1)",
2019-07-07 12:09:13 +02:00
borderColor: "rgba(100, 255, 100, .8)",
borderWidth: 2,
2019-07-07 12:18:26 +02:00
fill: true,
2019-07-07 12:09:13 +02:00
yAxisID: "y_bandwidth",
}, {
label: "Views",
2019-07-07 12:18:26 +02:00
backgroundColor: "rgba(128, 128, 255, .1)",
2019-07-07 12:09:13 +02:00
borderColor: "rgba(128, 128, 255, .8)",
borderWidth: 2,
2019-07-07 12:18:26 +02:00
fill: true,
2019-07-07 12:09:13 +02:00
yAxisID: "y_views",
}
]
},
options: {
stacked: false,
aspectRatio: 2.5,
tooltips: {
mode: "index",
intersect: false,
axis: "x"
},
scales: {
yAxes: [
{
type: "linear",
display: true,
position: "left",
id: "y_bandwidth",
scaleLabel: {
display: true,
labelString: "Downloads"
2019-07-07 22:34:20 +02:00
},
gridLines: {
color: "rgba(100, 255, 100, .1)"
2019-07-07 10:11:18 +02:00
}
2019-07-07 12:09:13 +02:00
}, {
type: "linear",
display: true,
position: "right",
id: "y_views",
scaleLabel: {
display: true,
labelString: "Views"
},
gridLines: {
2019-07-07 22:34:20 +02:00
color: "rgba(128, 128, 255, .2)"
}
}
],
xAxes: [
{
gridLines: {
display: false
2019-07-07 01:03:44 +02:00
}
}
2019-07-07 12:09:13 +02:00
]
},
elements: {
point: {
radius: 0
2019-07-07 01:03:44 +02:00
}
}
2019-07-07 12:09:13 +02:00
}
2019-07-07 01:03:44 +02:00
}
2019-07-07 12:09:13 +02:00
);
2017-11-10 12:39:55 +01:00
}
};