window.onload = function () {
$('#subcategory').prop('disabled', true);
$('#category').change(function() {
fillSubcategory(this.value)
});
customizeUploadButton()
}
function sortCategories(categories) {
var sortedCategories = []
for(var key in categories) {
sortedCategories.push(categories[key])
}
sortedCategories.sort()
return sortedCategories
}
function fillDetectedFilelist(file) {
var reader = new FileReader();
fileList = document.querySelectorAll(".detectedFiles")[0]
fileList.querySelector(".label").innerHTML = ""
fileList.querySelector(".content").innerHTML = ""
reader.onload = function() {
autodetectSuccess = false
b = new bencode()
try {
torrentObject = b.decode(reader.result)
} catch(err) {
}
if(torrentObject.info.name) {
document.querySelectorAll("input.name")[0].value = torrentObject.info.name
}
sizeGroup = document.querySelectorAll(".sizeGroup")[0]
if(torrentObject.info.pieces.length && torrentObject.info["piece length"]) {
sizeGroup.style.display = ""
size = (torrentObject.info.pieces.length / 20) * torrentObject.info["piece length"]
size = getNextUnit(size)
document.querySelectorAll(".detectedSize")[0].innerHTML = ((Math.round(size[0] * 100)) / 100) + " " + size[1]
autodetectSuccess = true
} else {
sizeGroup.style.display = "none"
}
trackerGroup = document.querySelectorAll(".trackerGroup")[0]
if(torrentObject.announce) {
trackerGroup.style.display = ""
document.querySelectorAll(".detectedTracker")[0].innerHTML = torrentObject.announce
autodetectSuccess = true
} else {
trackerGroup.style.display = "none"
}
filesGroup = document.querySelectorAll(".filesGroup")[0]
if(torrentObject["info"]["files"] != undefined && torrentObject["info"]["files"].length > 0) {
// render the file-tree if multiple files are present
autodetectSuccess = true
for(var fileIndex = 0; fileIndex < torrentObject.info.files.length; fileIndex++){
path = torrentObject["info"]["files"][fileIndex]["path"]
renderFile(getRoot(), path.reverse())
}
} else {
// assume the torrent is only one file named "name"
autodetectSuccess = true
renderFile(getRoot(), [torrentObject["info"]["name"]])
}
var detectInfosGroup = document.querySelectorAll(".detectedInfosGroup")[0]
if(autodetectSuccess) {
detectInfosGroup.style.display = "block"
} else {
detectInfosGroup.style.display = "none"
}
}
reader.readAsArrayBuffer(file)
}
function renderFile(root, path) {
if(path.length == 0){
return 0
} else {
nextElement = path.pop()
folderName = ""
for(var i = 0; i < nextElement.length; i++) {
code = nextElement.charCodeAt(i)
if((code >= 97 && code <= 122) || (code >= 65 && code <= 90)) {
folderName = folderName + nextElement[i]
}
}
type = path.length > 0 ? "dir" : "file"
newRoot = getOrCreate(root, folderName, nextElement, type)
renderFile(newRoot, path)
}
}
function getOrCreate(root, foldername, displayname, type) {
var obj = root.querySelector("div." + type + "." + foldername)
if(obj == undefined || obj.length == 0){
element = document.createElement("div")
element.classList.add(type)
element.classList.add(foldername)
label = document.createElement("div")
label.classList.add("label")
label.innerHTML = displayname
element.appendChild(label)
if(type == "dir") {
label.classList.add("closed")
label.onclick = function() {
if(this.classList.contains("closed")) {
this.classList.remove("closed")
this.classList.add("opened")
} else if(this.classList.contains("opened")) {
this.classList.remove("opened")
this.classList.add("closed")
}
content = this.parentElement.querySelector(".content")
content.hidden = !content.hidden
}
content = document.createElement("div")
content.classList.add("content")
content.hidden = true
element.appendChild(content)
}
root = root.querySelector(".content")
root.appendChild(element)
return element
} else {
return obj
}
}
function getRoot() {
rootElement = document.querySelectorAll(".detectedFiles.fileRoot")[0]
return rootElement
}
function fillSubcategory(value) {
var subSelect = $('#subcategory')
var selText = $(':first-child', subSelect).text()
subSelect.empty();
subSelect.append($('