transfer.sh/transfersh-web/scripts/main.js

186 lines
6.4 KiB
JavaScript
Raw Normal View History

2014-10-20 15:08:28 +02:00
$(document).ready(function() {
2014-10-16 20:01:43 +02:00
// Smooth scrolling
2014-10-20 15:08:28 +02:00
$('a[href*=#]:not([href=#])').click(function() {
2014-10-16 20:01:43 +02:00
if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $('[name=' + this.hash.slice(1) + ']');
if (target.length) {
$('html,body').animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
});
});
2014-10-20 15:08:28 +02:00
(function() {
2014-10-16 20:01:43 +02:00
var files = Array()
var queue = Array()
2014-10-16 20:01:43 +02:00
$(window).bind('beforeunload', function(){
if (queue.length==0)
return;
return 'There are still ' + queue.length + 'files being uploaded.';
});
2014-10-20 15:08:28 +02:00
function upload(file) {
2014-10-20 15:08:28 +02:00
$('.browse').addClass('uploading');
2014-10-16 20:01:43 +02:00
var li = $('<li style="clear:both;"/>');
2014-10-20 11:15:41 +02:00
2014-10-24 15:09:32 +02:00
li.append($('<div><div class="upload-progress"><span></span><div class="bar" style="width:0%;">####################################################</div></div><p>Uploading... ' + file.name + '</p></div>'));
2014-10-16 20:01:43 +02:00
$(li).appendTo($('.queue'));
var xhr = new XMLHttpRequest();
2014-10-20 15:08:28 +02:00
xhr.upload.addEventListener("progress", function(e) {
2014-10-16 20:01:43 +02:00
var pc = parseInt((e.loaded / e.total * 100));
2014-10-24 15:09:32 +02:00
$('.upload-progress', $(li)).show();
2014-10-16 20:01:43 +02:00
$('.upload-progress .bar', $(li)).css('width', pc + "%");
2014-10-24 15:09:32 +02:00
$('.upload-progress span ').empty().append(pc + "%");
$(window).bind('beforeunload', function(){
return 'File are still uploading';
});
2014-10-16 20:01:43 +02:00
}, false);
2014-10-20 15:08:28 +02:00
xhr.onreadystatechange = function(e) {
2014-10-16 20:01:43 +02:00
if (xhr.readyState == 4) {
2014-10-24 15:09:32 +02:00
/* $('.upload-progress', $(li)).hide();*/
2014-10-20 11:15:41 +02:00
$('#web').addClass('uploading');
2014-10-16 20:01:43 +02:00
// progress.className = (xhr.status == 200 ? "success" : "failure");
if (xhr.status == 200) {
2014-10-16 21:25:04 +02:00
$(li).html('<a target="_blank" href="' + xhr.responseText + '">' + xhr.responseText + '</a>');
2014-10-16 20:01:43 +02:00
} else {
2014-10-16 21:25:04 +02:00
$(li).html('<span>Error (' + xhr.status + ') during upload of file ' + file.name + '</span>');
2014-10-16 20:01:43 +02:00
}
var index = queue.indexOf(xhr);
if (index > -1) {
queue.splice(index, 1);
}
files.push(URI(xhr.responseText.replace("\n", "")).path());
2014-10-16 20:01:43 +02:00
$(".download-zip").attr("href", URI("(" + files.join(",") + ").zip").absoluteTo(location.href).toString());
$(".download-tar").attr("href", URI("(" + files.join(",") + ").tar.gz").absoluteTo(location.href).toString());
2014-10-20 15:08:28 +02:00
$(".all-files").addClass('show');
2014-10-16 20:01:43 +02:00
}
};
2014-10-16 20:01:43 +02:00
// should queue all uploads.
queue.push(xhr);
2014-10-16 20:01:43 +02:00
// start upload
xhr.open("PUT", '/' + file.name, true);
xhr.setRequestHeader("X_FILENAME", file.name);
xhr.send(file);
};
2014-10-20 15:08:28 +02:00
$(document).bind("dragenter", function(event) {
2014-10-16 20:01:43 +02:00
event.preventDefault();
2014-10-20 15:08:28 +02:00
}).bind("dragover", function(event) {
2014-10-16 20:01:43 +02:00
event.preventDefault();
// show drop indicator
2014-10-24 17:58:10 +02:00
$('#terminal').addClass('dragged');
2014-10-20 15:08:28 +02:00
$('#web').addClass('dragged');
}).bind("dragleave", function(event) {
2014-10-24 17:58:10 +02:00
$('#terminal').removeClass('dragged');
$('#web').removeClass('dragged');
2014-10-20 15:08:28 +02:00
}).bind("drop dragdrop", function(event) {
2014-10-16 20:01:43 +02:00
var files = event.originalEvent.target.files || event.originalEvent.dataTransfer.files;
2014-10-20 15:08:28 +02:00
$.each(files, function(index, file) {
2014-10-16 20:01:43 +02:00
upload(file);
});
event.stopPropagation();
event.preventDefault();
});
2014-10-20 15:08:28 +02:00
$('a.browse').on('click', function(event) {
2014-10-16 20:01:43 +02:00
$("input[type=file]").click();
return (false);
});
2014-10-20 15:08:28 +02:00
$('input[type=file]').on('change', function(event) {
$.each(this.files, function(index, file) {
2014-10-16 20:01:43 +02:00
if (file instanceof Blob) {
upload(file);
}
});
});
// clipboard
if (window.location.href.indexOf("download") > -1 ) {
(function() {
var copylinkbtn = document.getElementById("copy-link-btn"),
copylink = document.getElementById("copy-link-wrapper"),
overlay = document.getElementById("overlay");
var url = "http://url"
copylinkbtn.addEventListener("click", function() {
var error = document.getElementsByClassName('error');
while (error[0]) {
error[0].parentNode.removeChild(error[0]);
}
2014-11-06 17:12:59 +01:00
document.body.className += ' active';
2014-11-06 17:12:59 +01:00
copylink.children[1].value = url;
copylink.children[1].focus();
copylink.children[1].select();
}, false);
overlay.addEventListener("click", function() {
document.body.className = '';
}, false);
copylink.children[1].addEventListener("keydown", function(e) {
var error = document.getElementsByClassName('error');
while (error[0]) {
error[0].parentNode.removeChild(error[0]);
}
setTimeout(function() {
if ((e.metaKey || e.ctrlKey) && e.keyCode === 67 && isTextSelected(copylink.children[2])) {
document.body.className = '';
} else if ((e.metaKey || e.ctrlKey) && e.keyCode === 67 && isTextSelected(copylink.children[2]) === false) {
var error = document.createElement('span');
error.className = 'error';
var errortext = document.createTextNode('The link was not copied, make sure the entire text is selected.');
error.appendChild(errortext);
copylink.appendChild(error);
}
}, 100);
function isTextSelected(input) {
if (typeof input.selectionStart == "number") {
return input.selectionStart == 0 && input.selectionEnd == input.value.length;
} else if (typeof document.selection != "undefined") {
input.focus();
return document.selection.createRange().text == input.value;
}
}
}, false);
})();
};
})();