Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/public/js/kilo.js

222 lignes
8,3 Kio
JavaScript
Brut Vue normale Historique

2017-07-16 00:17:44 +02:00
var Kilo = function (params) {
// self reference
var self = this
2017-07-18 21:46:00 +02:00
// public variables
2017-07-18 21:55:40 +02:00
// Boolean defining if we are in sukebei
this.sukebei = (params.sukebei !== undefined) ? params.sukebei : false
2017-07-18 21:55:40 +02:00
// Boolean defining if a user is trusted
2017-07-16 00:17:44 +02:00
this.userTrusted = (params.userTrusted !== undefined) ? params.userTrusted : false
2017-07-18 21:55:40 +02:00
// Boolean defining if a user is logged
2017-07-16 00:17:44 +02:00
this.isMember = (params.isMember !== undefined) ? params.isMember : false
2017-07-18 21:55:40 +02:00
// Boolean enabling the AJAX load of torrents
this.listContext = (params.listContext !== undefined) ? params.listContext : false
2017-07-18 21:55:40 +02:00
// Variable defining the <select> of languages
2017-07-16 00:17:44 +02:00
this.langSelect = (params.langSelect !== undefined) ? params.langSelect : 'languages'
2017-07-18 21:55:40 +02:00
// Variable defining the language of the user
2017-07-16 00:17:44 +02:00
this.locale = (params.locale !== undefined) ? params.locale : ''
2017-07-18 21:55:40 +02:00
// Format of the date in the torrent listing
2017-07-16 00:17:44 +02:00
this.formatDate = {
year: 'numeric',
month: 'short',
day: 'numeric'
}
2017-07-18 21:55:40 +02:00
// Array of categories (loaded from the select html tag categories)
2017-07-16 00:17:44 +02:00
this.categories = []
2017-07-18 21:55:40 +02:00
// if no locale provided as a parameter, fallback to the language set by html tag
2017-07-16 00:17:44 +02:00
if (this.locale == '' && document.getElementsByTagName('html')[0].getAttribute('lang') !== null) {
this.locale = document.getElementsByTagName('html')[0].getAttribute('lang')
}
2017-07-18 21:46:00 +02:00
// Private variables
2017-07-23 04:50:36 +02:00
var Keywords_flags = [
["vostfr", "vosfr", "[ita]", "[eng]", " eng ", "[english]", "[english sub]", "engsub", "[jp]", "[jpn]", "[japanese]", "[jav]"],
["fr", "fr", "it", "en", "en", "en", "en", "en", "ja", "ja", "ja", "ja"]
2017-07-18 21:46:00 +02:00
]
var Keywords_categories = [
2017-07-23 04:50:36 +02:00
[
["[jav]", "[h-games]"],
[7, 3]
],
[
[""],
[0]
]
2017-07-18 21:55:40 +02:00
]
2017-07-18 21:46:00 +02:00
2017-07-16 00:17:44 +02:00
// Parsing categories
2017-07-23 04:50:36 +02:00
document.querySelectorAll(".form-torrent-category option").forEach(function (el) {
2017-07-16 00:17:44 +02:00
var subcat
if (self.sukebei) {
subcat = el.value.replace("_", "")
} else {
subcat = el.value.split("_")[1]
}
subcat = (subcat === undefined) ? 0 : subcat
self.categories.push(subcat)
})
2017-07-16 00:17:44 +02:00
this.render = function () {
console.log(this.locale)
// Displaying the block and set the locale timestamp
document.getElementsByClassName('torrent-preview-table')[0].style.display = 'block'
document.getElementsByClassName('table-torrent-date')[0].innerText = new Date(Date.now()).toISOString()
2017-07-23 04:50:36 +02:00
2017-07-16 00:17:44 +02:00
// Adding listener events
for (var langIndex = 0; langIndex < document.getElementsByName(this.langSelect).length; langIndex++) {
document.getElementsByName(this.langSelect)[langIndex].addEventListener('change', updateTorrentLang)
}
var formCategory = document.getElementsByClassName('form-torrent-category')[0]
formCategory.addEventListener('change', updatePreviewCategory)
var formName = document.getElementsByClassName('form-torrent-name')[0]
formName.addEventListener('keyup', updatePreviewTorrentName)
var formRemake = document.getElementsByClassName('form-torrent-remake')[0]
formRemake.addEventListener('change', updatePreviewRemake)
var formHidden = document.getElementsByClassName('form-torrent-hidden')[0]
if (this.isMember) {
formHidden.addEventListener('change', updateHidden)
}
2017-07-14 21:34:25 +02:00
2017-07-16 00:17:44 +02:00
// Setting default values
this.setRemake(formRemake.checked)
if (this.isMember) {
this.setHidden(formHidden.checked)
}
this.setName(formName.value)
this.setCategory(formCategory.selectedIndex)
updateTorrentLang()
2017-07-23 04:50:36 +02:00
//Adding the torrent under and above the previewed one.
if (this.listContext) {
2017-07-20 22:00:42 +02:00
Query.Get('/api/search?limit=2', function (data) {
torrents = data.torrents
var torrentHTML = []
var l = torrents.length
for (var i = 0; i < l; i++) {
torrentHTML.push(Templates.Render('torrents.item', torrents[i]))
}
document.getElementById("torrentListResults").innerHTML = torrentHTML[0] + document.getElementById("torrent-info-tr").outerHTML + torrentHTML[1]
})
}
2017-07-16 00:17:44 +02:00
}
2017-07-18 21:46:00 +02:00
2017-07-16 06:43:09 +02:00
// Helpers function for events and render
2017-07-18 21:55:40 +02:00
// set the class remake with b a boolean
2017-07-16 00:17:44 +02:00
this.setRemake = function (b) {
if (b) {
document.getElementById("torrent-info-tr").classList.add('remake')
2017-07-16 00:17:44 +02:00
} else {
document.getElementById("torrent-info-tr").classList.remove('remake')
2017-07-16 00:17:44 +02:00
}
}
2017-07-18 21:55:40 +02:00
// set the class hidden with b a boolean
2017-07-16 00:17:44 +02:00
this.setHidden = function (b) {
if (!b) {
document.getElementById("torrent-info-tr").classList.remove('trusted')
2017-07-16 00:17:44 +02:00
} else if (this.userTrusted) {
document.getElementById("torrent-info-tr").classList.add('trusted')
2017-07-16 00:17:44 +02:00
}
}
2017-07-18 21:55:40 +02:00
// set the name of the torrent according to value string
2017-07-16 00:17:44 +02:00
this.setName = function (value) {
document.getElementsByClassName('table-torrent-name')[0].innerText = value
}
2017-07-18 21:55:40 +02:00
// set the category of the torrent according to index int
2017-07-16 00:17:44 +02:00
this.setCategory = function (index) {
var tableCategory = document.getElementsByClassName('table-torrent-category')[0]
tableCategory.className = 'nyaa-cat table-torrent-category ' + (this.sukebei ? 'sukebei' : 'nyaa') + '-cat-' + this.categories[index]
tableCategory.title = document.getElementsByClassName('form-torrent-category')[0].querySelectorAll("option")[index].textContent
}
2017-07-23 04:50:36 +02:00
//
this.addKeywordFlags = function (value) {
2017-07-18 21:46:00 +02:00
var torrentLowerCaseName = value.toLowerCase()
var updateLang = false
2017-07-23 04:50:36 +02:00
for (var KeywordIndex = 0; KeywordIndex < Keywords_flags[0].length; KeywordIndex++)
if (torrentLowerCaseName.includes(Keywords_flags[0][KeywordIndex])) {
document.getElementById("upload-lang-" + Keywords_flags[1][KeywordIndex]).checked = true
updateLang = true
2017-07-18 21:46:00 +02:00
}
2017-07-23 04:50:36 +02:00
if (updateLang) updateTorrentLang()
2017-07-18 21:46:00 +02:00
}
2017-07-18 21:55:40 +02:00
//
2017-07-23 04:50:36 +02:00
this.addKeywordCategories = function (value) {
if (document.getElementsByClassName('form-torrent-category')[0].selectedIndex != 0)
2017-07-18 21:55:40 +02:00
return
2017-07-23 04:50:36 +02:00
2017-07-18 21:46:00 +02:00
var torrentLowerCaseName = value.toLowerCase(),
IsOnSukebei = params.sukebei ? 0 : 1;
2017-07-23 04:50:36 +02:00
for (var KeywordIndex = 0; KeywordIndex < Keywords_categories[IsOnSukebei][0].length; KeywordIndex++)
if (torrentLowerCaseName.includes(Keywords_categories[IsOnSukebei][0][KeywordIndex])) {
2017-07-18 21:46:00 +02:00
document.getElementsByClassName('form-torrent-category')[0].selectedIndex = Keywords_categories[IsOnSukebei][1][KeywordIndex];
this.setCategory(document.getElementsByClassName('form-torrent-category')[0].selectedIndex)
2017-07-18 21:55:40 +02:00
break
2017-07-23 04:50:36 +02:00
}
2017-07-18 21:46:00 +02:00
}
2017-07-18 21:55:40 +02:00
// Helper to prevent the functions on keyup/keydown to slow the user typing
2017-07-18 21:46:00 +02:00
this.debounce = function (func, wait, immediate) {
var timeout
2017-07-23 04:50:36 +02:00
return function () {
var context = this,
args = arguments
var later = function () {
2017-07-18 21:55:40 +02:00
timeout = null
2017-07-18 21:46:00 +02:00
if (!immediate) func.apply(context, args)
}
var callNow = immediate && !timeout
clearTimeout(timeout)
timeout = setTimeout(later, wait)
if (callNow) func.apply(context, args)
}
}
2017-07-16 06:43:09 +02:00
// Event handlers
2017-07-18 21:55:40 +02:00
// Event on remake checkbox
2017-07-16 00:17:44 +02:00
var updatePreviewRemake = function (e) {
var el = e.target
self.setRemake(el.checked)
}
2017-07-18 21:55:40 +02:00
// Event on torrent name keyup
2017-07-16 00:17:44 +02:00
var updatePreviewTorrentName = function (e) {
var el = e.target
self.setName(el.value)
2017-07-23 04:50:36 +02:00
self.debounce(function (value) {
2017-07-18 21:46:00 +02:00
self.addKeywordFlags(value)
self.addKeywordCategories(value)
}, 300)(el.value)
2017-07-16 00:17:44 +02:00
}
2017-07-18 21:55:40 +02:00
// Event on hidden checkbox
2017-07-16 00:17:44 +02:00
var updateHidden = function (e) {
var el = e.target
self.setHidden(el.checked)
}
2017-07-18 21:55:40 +02:00
// Event on cateogry change
2017-07-16 00:17:44 +02:00
var updatePreviewCategory = function (e) {
var el = e.target
self.setCategory(el.selectedIndex)
}
2017-07-18 21:55:40 +02:00
// Event on languages checkbox
2017-07-16 00:17:44 +02:00
var updateTorrentLang = function () {
var langCount = 0
var langValue = 'other'
var langTitle = ''
2017-07-16 00:17:44 +02:00
for (var langIndex = 0; langIndex < document.getElementsByName('languages').length; langIndex++) {
if (document.getElementsByName('languages')[langIndex].checked) {
langTitle = langTitle + document.getElementsByClassName('upload-lang-languagename')[langIndex].innerText + ','
2017-07-16 00:17:44 +02:00
if (++langCount > 1) {
langValue = 'multiple'
continue
}
langValue = document.getElementsByName('languages')[langIndex].value
}
}
var langCat = langValue !== 'other' ? (langValue > 1 ? 'multiple' : langValue) : 'other'
document.getElementsByClassName('table-torrent-flag')[0].className = 'table-torrent-flag flag flag-' + Templates.FlagCode(langCat)
2017-07-16 00:17:44 +02:00
document.getElementsByClassName('table-torrent-flag')[0].title = langTitle
}
}