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
2017-08-01 13:54:01 +02:00
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
2017-07-17 14:20:18 +02:00
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-15 21:41:57 +02:00
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-15 21:41:57 +02:00
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 )
2017-07-16 14:41:24 +02:00
updateTorrentLang ( )
2017-07-17 14:20:18 +02:00
2017-07-23 04:50:36 +02:00
//Adding the torrent under and above the previewed one.
2017-07-17 14:20:18 +02:00
if ( this . listContext ) {
2017-07-20 22:00:42 +02:00
Query . Get ( '/api/search?limit=2' , function ( data ) {
torrents = data . torrents
2017-07-17 14:20:18 +02:00
var torrentHTML = [ ]
var l = torrents . length
for ( var i = 0 ; i < l ; i ++ ) {
torrentHTML . push ( Templates . Render ( 'torrents.item' , torrents [ i ] ) )
}
2017-09-11 21:59:17 +02:00
document . getElementById ( "torrentListResults" ) . innerHTML = torrentHTML [ 0 ] + document . getElementById ( "torrent-info-tr" ) . outerHTML + torrentHTML [ 1 ]
2017-07-17 14:20:18 +02:00
} )
}
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 ) {
2017-09-11 21:59:17 +02:00
document . getElementById ( "torrent-info-tr" ) . classList . add ( 'remake' )
2017-07-16 00:17:44 +02:00
} else {
2017-09-11 21:59:17 +02:00
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 ) {
2017-09-11 21:59:17 +02:00
document . getElementById ( "torrent-info-tr" ) . classList . remove ( 'trusted' )
2017-07-16 00:17:44 +02:00
} else if ( this . userTrusted ) {
2017-09-11 21:59:17 +02:00
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-15 21:41:57 +02:00
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-15 21:41:57 +02:00
2017-07-16 00:17:44 +02:00
for ( var langIndex = 0 ; langIndex < document . getElementsByName ( 'languages' ) . length ; langIndex ++ ) {
if ( document . getElementsByName ( 'languages' ) [ langIndex ] . checked ) {
2017-09-11 21:59:17 +02:00
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'
2017-07-21 01:47:36 +02:00
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
}
2017-07-17 22:05:49 +02:00
}