TorrentIndexer/static/js/create.js

165 lines
6.7 KiB
JavaScript
Raw Normal View History

2015-02-14 00:17:40 +01:00
window.onload = function () {
$('.dropdown li > a').click(function(e){
setDropdownButtonText(this.innerHTML, this.parentElement.parentElement.parentElement) //set the display-text to the selected value
fillDropdownByValue(this.innerHTML, $(".dropdown")["1"])
});
customizeUploadButton()
}
//function fillDropdownByValue(value, dropdownToFill) {
// dropdownToFill.getElementsByClassName("text")[0].innerHTML = "Subcategory"
// var dropdownToFillUl = dropdownToFill.getElementsByClassName("dropdown-menu")[0]
// dropdownToFillUl.innerHTML = ""
//
// subCategory = getDescriptorByLocalString("english", value)
// catList = document.getElementById("subcategory_list")
// catList.innerHTML = ""
// categories = global_categories[subCategory].sort()
// for(var key in categories) {
// var newEntry = document.createElement("li")
// var newEntryLink = document.createElement("a")
// newEntry.setAttribute("role", "presentation")
// newEntryLink.setAttribute("role", "menuitem")
// newEntryLink.tabIndex = -1
// newEntryLink.href = "#"
// newEntryLink.innerHTML = getLocalString("english", categories[key])
// newEntryLink.onclick = function(){ setDropdownButtonText(this.innerHTML, this.parentElement.parentElement.parentElement) }
// newEntry.appendChild(newEntryLink)
// dropdownToFillUl.appendChild(newEntry)
// }
//}
2015-02-14 00:17:40 +01:00
function setDropdownButtonText(text, dropdownButton) {
var dropdownTextSpan = dropdownButton.getElementsByClassName("text")[0]
dropdownTextSpan.innerHTML = text
}
function sortCategories(categories) {
var sortedCategories = []
for(var key in categories) {
sortedCategories.push(categories[key])
2015-02-14 00:17:40 +01:00
}
sortedCategories.sort()
return sortedCategories
}
// Fill a defined dropdown with values.
// These values will be generated out of the categories.json
2015-02-14 00:17:40 +01:00
function fillDropdownByValue(value, dropdownToFill) {
dropdownToFill.getElementsByTagName("button")[0].disabled = false
dropdownToFill.getElementsByClassName("text")[0].innerHTML = "Subcategorie"
var dropdownToFillUl = dropdownToFill.getElementsByClassName("dropdown-menu")[0]
dropdownToFillUl.innerHTML = ""
valueDescriptor = getDescriptorByLocalString("english", value)
subcategories = global_categories[valueDescriptor]
subcategories = sortCategories(subcategories)
for(subcategoryIndex in subcategories) {
subcategoryLocalString = getLocalString("english", subcategories[subcategoryIndex])
var newEntry = document.createElement("li")
var newEntryLink = document.createElement("a")
newEntry.setAttribute("role", "presentation")
newEntryLink.setAttribute("role", "menuitem")
newEntryLink.tabIndex = -1
newEntryLink.href = "#"
newEntryLink.innerHTML = subcategoryLocalString
newEntryLink.onclick = function(){ setDropdownButtonText(this.innerHTML, this.parentElement.parentElement.parentElement) }
newEntry.appendChild(newEntryLink)
dropdownToFillUl.appendChild(newEntry)
2015-02-14 00:17:40 +01:00
}
}
// Hides the default browser-upload-form and replaces it by an button
2015-02-14 00:17:40 +01:00
function customizeUploadButton() {
$("input.file").before('<button id="button-file" type="button" class="btn btn-default"><span class="text">Upload...</span><span class="glyphicon glyphicon-open-file" aria-hidden="true"></span></button>');
$("input.file").hide();
2015-02-14 00:17:40 +01:00
$('body').on('click', '#button-file', function() {
$("input.file").trigger('click');
2015-02-14 00:17:40 +01:00
});
}
// This sets the Uploadbutton to the filename of the uploaded file
2015-02-14 00:17:40 +01:00
function setButtonToFilename(event) {
$("input[name='torrentFile']").each(function() {
2015-02-14 00:17:40 +01:00
var fileName = $(this).val().split('/').pop().split('\\').pop();
targetInput = event["target"]
button = targetInput.previousSibling.getElementsByClassName("text")[0]
button.innerHTML = chunk(fileName, 40)
2015-02-14 00:17:40 +01:00
});
}
// Sets the text of a given dropdown-button to a given value
2015-02-14 00:17:40 +01:00
function setDropdownButtonText(text, dropdownButton) {
var dropdownTextSpan = dropdownButton.getElementsByClassName("text")[0]
dropdownHiddenValue = dropdownButton.getElementsByTagName("input")[0]
dropdownHiddenValue.setAttribute("value", getDescriptorByLocalString("english", text))
2015-02-14 00:17:40 +01:00
dropdownTextSpan.innerHTML = text
}
function validateForm() {
valid = true
file = document.querySelectorAll("input.file")[0]
category = document.querySelectorAll("input.category")[0]
subcategory = document.querySelectorAll("input.subcategory")[0]
torrentname = document.querySelectorAll("input.name")[0]
description = document.querySelectorAll("textarea.description")[0]
if(file.value.length <= 0) {
valid = false
file.parentElement.parentElement.classList.add("has-error")
file.parentElement.parentElement.classList.remove("has-success")
} else {
file.parentElement.parentElement.classList.add("has-success")
file.parentElement.parentElement.classList.remove("has-error")
}
if(category.value.length <= 0) {
valid = false
category.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.classList.add("has-error")
category.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.classList.remove("has-success")
} else {
category.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.classList.add("has-success")
category.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.classList.remove("has-error")
}
if(subcategory.value.length <= 0) {
valid = false
category.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.classList.add("has-error")
category.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.classList.remove("has-success")
} else {
category.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.classList.add("has-success")
category.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.classList.remove("has-error")
}
if(torrentname.value.length <= 0) {
valid = false
torrentname.parentElement.parentElement.classList.add("has-error")
torrentname.parentElement.parentElement.classList.remove("has-success")
} else {
torrentname.parentElement.parentElement.classList.add("has-success")
torrentname.parentElement.parentElement.classList.remove("has-error")
}
if(description.value.length <= 0) {
valid = false
description.parentElement.parentElement.classList.add("has-error")
description.parentElement.parentElement.classList.remove("has-success")
} else {
description.parentElement.parentElement.classList.add("has-success")
description.parentElement.parentElement.classList.remove("has-error")
}
return valid;
}
function chunk(string, n) {
var ret = "";
for(var i=0, len=string.length; i < len; i += n) {
if(i==0) {
ret = string.substr(i, n)
} else {
ret += "<br/>" + string.substr(i, n)
}
}
return ret
};