2017-07-16 00:17:44 +02:00
var Kilo = function ( params ) {
// self reference
var self = this
// variables
this . sukebei = ( params . sukebei !== undefined ) ? params . sukebei : 0
this . userTrusted = ( params . userTrusted !== undefined ) ? params . userTrusted : false
this . isMember = ( params . isMember !== undefined ) ? params . isMember : false
2017-07-17 14:20:18 +02:00
this . listContext = ( params . listContext !== undefined ) ? params . listContext : false
2017-07-16 00:17:44 +02:00
this . langSelect = ( params . langSelect !== undefined ) ? params . langSelect : 'languages'
this . locale = ( params . locale !== undefined ) ? params . locale : ''
this . formatDate = {
year : 'numeric' ,
month : 'short' ,
day : 'numeric'
}
this . categories = [ ]
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-16 00:17:44 +02:00
// Parsing categories
document . querySelectorAll ( ".form-torrent-category option" ) . forEach ( function ( el ) {
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-16 12:51:00 +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
//Adding the torrent under and above the previewed one.
if ( this . listContext ) {
Query . Get ( '/api/search?limit=2' , function ( 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 ( "torrentListResults" ) . innerHTML + torrentHTML [ 1 ] ;
} )
}
2017-07-16 00:17:44 +02:00
}
2017-07-16 06:43:09 +02:00
// Helpers function for events and render
2017-07-16 00:17:44 +02:00
this . setRemake = function ( b ) {
if ( b ) {
document . getElementsByName ( 'torrent-info tr' ) [ 0 ] . classList . add ( 'remake' )
} else {
document . getElementsByName ( 'torrent-info tr' ) [ 0 ] . classList . remove ( 'remake' )
}
}
this . setHidden = function ( b ) {
if ( ! b ) {
document . getElementsByName ( 'torrent-info tr' ) [ 0 ] . classList . remove ( 'trusted' )
} else if ( this . userTrusted ) {
document . getElementsByName ( 'torrent-info tr' ) [ 0 ] . classList . add ( 'trusted' )
}
}
this . setName = function ( value ) {
document . getElementsByClassName ( 'table-torrent-name' ) [ 0 ] . innerText = value
}
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-15 21:41:57 +02:00
2017-07-16 06:43:09 +02:00
// Event handlers
2017-07-16 00:17:44 +02:00
var updatePreviewRemake = function ( e ) {
var el = e . target
self . setRemake ( el . checked )
}
2017-07-17 22:05:49 +02:00
2017-07-16 00:17:44 +02:00
var updatePreviewTorrentName = function ( e ) {
var el = e . target
self . setName ( el . value )
2017-07-17 22:05:49 +02:00
addKeywordFlags ( el . value ) ;
2017-07-16 00:17:44 +02:00
}
2017-07-17 22:05:49 +02:00
var addKeywordFlags = debounce ( function ( e ) {
var Keywords _flags = [
[ "vostfr" , "vosfr" , "[ita]" , "[eng]" , " eng " , "[english]" , "[jp]" , "[jpn]" ] ,
2017-07-18 13:03:53 +02:00
[ "fr" , "fr" , "it" , "en" , "en" , "en" , "ja" , "ja" ] ] ;
2017-07-17 22:05:49 +02:00
var torrentLowerCaseName = e . toLowerCase ( ) ,
updateLang = false ;
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 ;
}
if ( updateLang ) updateTorrentLang ( ) ;
} , 300 ) ;
2017-07-16 00:17:44 +02:00
var updateHidden = function ( e ) {
var el = e . target
self . setHidden ( el . checked )
}
var updatePreviewCategory = function ( e ) {
var el = e . target
self . setCategory ( el . selectedIndex )
}
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 ) {
langTitle = langTitle + document . getElementsByName ( 'upload-lang-languagename' ) [ langIndex ] . innerText + ','
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-' + langCat
document . getElementsByClassName ( 'table-torrent-flag' ) [ 0 ] . title = langTitle
}
2017-07-17 22:05:49 +02:00
}
function debounce ( func , wait , immediate ) {
var timeout ;
return function ( ) {
var context = this , args = arguments ;
var later = function ( ) {
timeout = null ;
if ( ! immediate ) func . apply ( context , args ) ;
} ;
var callNow = immediate && ! timeout ;
clearTimeout ( timeout ) ;
timeout = setTimeout ( later , wait ) ;
if ( callNow ) func . apply ( context , args ) ;
} ;
} ;