2017-06-04 07:43:41 +02:00
// @source https://github.com/NyaaPantsu/nyaa/tree/dev/public/js
// @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&dn=expat.txt Expat
2017-06-02 04:51:44 +02:00
var TorrentsMod = {
2017-07-16 06:43:09 +02:00
// Variables that can be modified to change the dom interactions
show _hide _button : "show_actions" ,
btn _class _action : "cb_action" ,
btn _class _submit : "cb_submit" ,
progress _bar _id : "progress_modtool" ,
status _input _name : "status_id" ,
owner _input _name : "owner_id" ,
category _input _name : "category_id" ,
delete _btn : "delete" ,
lock _delete _btn : "lock_delete" ,
edit _btn : "edit" ,
refreshTimeout : 3000 ,
// Internal variables used for processing the request
selected : [ ] ,
queued : [ ] ,
2017-07-23 04:50:36 +02:00
unique _id : 1 ,
error _count : 0 ,
2017-07-16 06:43:09 +02:00
progress _count : 0 ,
progress _max : 0 ,
pause : false ,
enabled : false ,
2017-06-02 04:51:44 +02:00
2017-07-16 06:43:09 +02:00
// Init method
2017-07-23 04:50:36 +02:00
Create : function ( ) {
2017-07-16 06:43:09 +02:00
var sh _btn = document . getElementById ( TorrentsMod . show _hide _button ) ;
var btn _actions = document . getElementsByClassName ( this . btn _class _action )
var btn _submit = document . getElementsByClassName ( this . btn _class _submit )
btn _submit [ 0 ] . disabled = true ;
2017-07-23 04:50:36 +02:00
for ( var i = 0 ; i < btn _actions . length ; i ++ ) {
2017-07-16 06:43:09 +02:00
btn _actions [ i ] . disabled = true ;
switch ( btn _actions [ i ] . id ) {
2017-07-17 15:10:08 +02:00
case this . delete _btn :
2017-07-16 06:43:09 +02:00
btn _actions [ i ] . addEventListener ( "click" , this . Delete )
break ;
2017-07-17 15:10:08 +02:00
case this . lock _delete _btn :
2017-07-16 06:43:09 +02:00
btn _actions [ i ] . addEventListener ( "click" , this . LockDelete )
break ;
2017-07-17 15:10:08 +02:00
case this . edit _btn :
2017-07-16 06:43:09 +02:00
btn _actions [ i ] . addEventListener ( "click" , this . Edit )
break ;
2017-07-17 15:10:08 +02:00
default :
2017-07-16 06:43:09 +02:00
break ;
}
}
2017-07-23 04:50:36 +02:00
for ( var i = 0 ; i < this . checkboxes . length ; i ++ ) {
2017-07-17 15:10:08 +02:00
var checkbox = this . checkboxes [ i ] ;
2017-07-16 06:43:09 +02:00
checkbox . addEventListener ( "change" , this . checkboxEventHandler )
}
2017-07-23 04:50:36 +02:00
sh _btn . addEventListener ( "click" , function ( e ) {
2017-07-16 06:43:09 +02:00
var divActions = this . nextElementSibling ;
if ( divActions . style . display == "inline" ) {
TorrentsMod . enabled = false ;
2017-07-17 22:05:24 +02:00
document . getElementsByClassName ( "results box mod-open" ) [ 0 ] . className = "results box" ;
2017-07-23 04:50:36 +02:00
} else {
TorrentsMod . enabled = true ;
document . getElementsByClassName ( "results box" ) [ 0 ] . className = "results box mod-open" ;
2017-07-16 06:43:09 +02:00
}
divActions . style . display = ( TorrentsMod . enabled ) ? "inline" : "none" ;
var td _cbs = document . getElementsByClassName ( "tr-cb" )
2017-07-23 04:50:36 +02:00
for ( var i = 0 ; i < td _cbs . length ; i ++ ) {
2017-07-16 06:43:09 +02:00
td _cb = td _cbs [ i ] ;
td _cb . style . display = ( TorrentsMod . enabled ) ? "table-cell" : "none" ;
}
var toggleText = this . dataset . toggleText ;
this . dataset . toggleText = this . innerText ;
this . innerText = toggleText ;
} ) ;
} ,
// generate a unique id for a query
2017-07-23 04:50:36 +02:00
getId : function ( ) {
2017-07-16 06:43:09 +02:00
return this . unique _id ++ ;
} ,
2017-06-02 04:51:44 +02:00
2017-07-16 06:43:09 +02:00
// UI Methods
2017-07-23 04:50:36 +02:00
selectAll : function ( bool ) {
2017-07-16 06:43:09 +02:00
var l = TorrentsMod . checkboxes . length ;
for ( var i = 0 ; i < l ; i ++ ) {
TorrentsMod . checkboxes [ i ] . checked = bool ;
TorrentsMod . checkboxEventHandlerFunc ( TorrentsMod . checkboxes [ i ] ) ;
}
} ,
2017-07-23 04:50:36 +02:00
disableBtnActions : function ( ) {
2017-07-16 06:43:09 +02:00
var btn _actions = document . getElementsByClassName ( this . btn _class _action )
2017-07-23 04:50:36 +02:00
for ( var i = 0 ; i < btn _actions . length ; i ++ ) {
2017-07-16 06:43:09 +02:00
btn _actions [ i ] . disabled = true ;
}
} ,
2017-07-23 04:50:36 +02:00
enableBtnActions : function ( ) {
2017-07-16 06:43:09 +02:00
var btn _actions = document . getElementsByClassName ( this . btn _class _action )
2017-07-23 04:50:36 +02:00
for ( var i = 0 ; i < btn _actions . length ; i ++ ) {
2017-07-16 06:43:09 +02:00
btn _actions [ i ] . disabled = false ;
}
} ,
2017-07-23 04:50:36 +02:00
enableBtnSubmit : function ( ) {
2017-07-16 06:43:09 +02:00
var btn _submit = document . getElementsByClassName ( this . btn _class _submit )
btn _submit [ 0 ] . disabled = false ;
} ,
2017-07-23 04:50:36 +02:00
enableApplyChangesBtn : function ( ) {
2017-07-16 06:43:09 +02:00
var btn _apply _changes = document . getElementById ( "confirm_changes" ) ;
2017-07-23 04:50:36 +02:00
btn _apply _changes . disabled = false ;
2017-07-16 06:43:09 +02:00
} ,
2017-07-23 04:50:36 +02:00
disableBtnSubmit : function ( ) {
2017-07-16 06:43:09 +02:00
var btn _submit = document . getElementsByClassName ( this . btn _class _submit )
btn _submit [ 0 ] . disabled = true ;
} ,
2017-07-23 04:50:36 +02:00
disableApplyChangesBtn : function ( ) {
2017-07-16 06:43:09 +02:00
var btn _apply _changes = document . getElementById ( "confirm_changes" ) ;
2017-07-23 04:50:36 +02:00
btn _apply _changes . disabled = true ;
2017-07-16 06:43:09 +02:00
} ,
2017-07-23 04:50:36 +02:00
removeDivFromList : function ( i ) {
2017-07-16 06:43:09 +02:00
var queueAction = this . queued [ i ] ;
var parentDiv = document . getElementById ( queueAction . unique _id ) . parentNode ;
parentDiv . removeChild ( document . getElementById ( queueAction . unique _id ) ) ;
} ,
2017-07-23 04:50:36 +02:00
removeFromParent : function ( el ) {
2017-07-16 06:43:09 +02:00
var parentDiv = el . parentNode ;
parentDiv . removeChild ( el ) ;
} ,
2017-07-23 04:50:36 +02:00
generatingModal : function ( ) {
2017-07-17 15:10:08 +02:00
var listLength = this . queued . length ;
2017-07-23 04:50:36 +02:00
var div = {
"edit" : "" ,
"delete" : ""
} ;
for ( var i = 0 ; i < listLength ; i ++ ) {
2017-07-16 06:43:09 +02:00
var listHTML = "" ;
2017-07-23 04:50:36 +02:00
for ( key in this . queued [ i ] . selection ) {
2017-07-16 06:43:09 +02:00
var selection = this . queued [ i ] . selection [ key ] ;
selection . key = i ;
2017-07-23 04:50:36 +02:00
listHTML += Templates . Render ( "torrents." + this . queued [ i ] . action + ".item" , selection ) ;
2017-07-16 06:43:09 +02:00
}
this . queued [ i ] . list = listHTML ;
this . queued [ i ] . key = i ;
2017-07-23 04:50:36 +02:00
div [ this . queued [ i ] . action ] += Templates . Render ( "torrents." + this . queued [ i ] . action + ".block" , this . queued [ i ] ) ;
2017-07-16 06:43:09 +02:00
}
this . progress _count = 0 ;
this . progress _max = listLength ;
document . querySelector ( ".modal .edit_changes" ) . innerHTML = div [ "edit" ] ;
document . querySelector ( ".modal .delete_changes" ) . innerHTML = div [ "delete" ] ;
} ,
2017-07-23 04:50:36 +02:00
toggleList : function ( el ) {
2017-07-16 06:43:09 +02:00
el . parentNode . nextSibling . style . display = ( el . parentNode . nextSibling . style . display != "block" ) ? "block" : "none"
} ,
2017-07-23 04:50:36 +02:00
addToLog : function ( type , msg ) {
2017-07-16 06:43:09 +02:00
var logDiv = document . querySelector ( ".modal .logs_mess" ) ;
if ( logDiv . style . display == "none" ) logDiv . style . display = "block"
2017-07-23 04:50:36 +02:00
logDiv . innerHTML += Templates . Render ( "torrents.logs." + type , msg ) ;
2017-07-16 06:43:09 +02:00
} ,
2017-07-23 04:50:36 +02:00
updateProgressBar : function ( ) {
document . querySelector ( "#" + this . progress _bar _id ) . style . display = "block" ;
var progress _green = document . querySelector ( "#" + this . progress _bar _id + " .progress-green" ) ;
var perc = this . progress _count / this . progress _max * 100 ;
progress _green . style . width = perc + "%" ;
progress _green . innerText = this . progress _count + "/" + this . progress _max ;
2017-07-16 06:43:09 +02:00
} ,
2017-07-23 04:50:36 +02:00
resetModal : function ( ) {
2017-07-16 06:43:09 +02:00
var logDiv = document . querySelector ( ".modal .logs_mess" ) ;
logDiv . style . display = "none" ;
2017-07-23 04:50:36 +02:00
document . querySelector ( "#" + this . progress _bar _id ) . style . display = "none" ;
2017-07-16 06:43:09 +02:00
logDiv . innerHTML = "" ;
document . querySelector ( ".modal .edit_changes" ) . innerHTML = "" ;
document . querySelector ( ".modal .delete_changes" ) . innerHTML = "" ;
this . enableApplyChangesBtn ( ) ;
} ,
2017-07-23 04:50:36 +02:00
statusToClassName : function ( status ) {
2017-07-16 06:43:09 +02:00
var className = [ "" , "normal" , "remake" , "trusted" , "aplus" , "" ]
return className [ status ] ;
} ,
2017-06-02 04:51:44 +02:00
2017-07-16 06:43:09 +02:00
// Selection Management Methods
addToSelection : function ( torrent ) {
this . selected [ torrent . id ] = torrent ;
} ,
2017-07-23 04:50:36 +02:00
removeFromSelection : function ( torrent ) {
2017-07-16 06:43:09 +02:00
delete this . selected [ torrent . id ] ;
2017-07-17 15:10:08 +02:00
for ( var t in this . selected ) {
2017-07-16 06:43:09 +02:00
return
}
this . selected = [ ] ;
} ,
2017-06-02 04:51:44 +02:00
2017-07-16 06:43:09 +02:00
// Query Queue Management Methods
2017-07-23 04:50:36 +02:00
AddToQueue : function ( QueueAction ) {
2017-07-16 06:43:09 +02:00
QueueAction . unique _id = this . getId ( ) ; // used for DOM interaction
this . queued . push ( QueueAction ) ;
this . enableBtnSubmit ( )
} ,
2017-07-23 04:50:36 +02:00
RemoveFromQueueAfterItems : function ( i ) {
2017-07-16 06:43:09 +02:00
for ( t in this . queued [ i ] . selection ) {
this . RemoveItemFromQueue ( i , t ) ;
}
} ,
2017-07-23 04:50:36 +02:00
RemoveFromQueue : function ( i ) {
this . progress _max = ( this . progress _max - 1 >= 0 ) ? this . progress _max - 1 : 0 ;
2017-07-16 06:43:09 +02:00
this . RemoveFromQueueAction ( i )
return false ;
} ,
2017-07-23 04:50:36 +02:00
RemoveFromQueueAction : function ( i ) {
this . removeFromParent ( document . getElementById ( "list_" + this . queued [ i ] . unique _id ) ) ;
2017-07-16 06:43:09 +02:00
this . queued . splice ( i , 1 ) ;
if ( this . queued . length == 0 ) {
this . disableBtnSubmit ( ) ;
2017-07-23 04:50:36 +02:00
if ( this . progress _max > 0 ) {
2017-07-16 06:43:09 +02:00
this . disableApplyChangesBtn ( ) ;
} else {
Modal . CloseActive ( ) ;
}
}
} ,
2017-07-23 04:50:36 +02:00
formatSelectionToQuery : function ( selection ) {
2017-07-16 06:43:09 +02:00
var format = "" ;
2017-07-17 15:10:08 +02:00
for ( var s in selection ) {
2017-07-23 04:50:36 +02:00
format += "&torrent_id=" + selection [ s ] . id
2017-07-16 06:43:09 +02:00
}
return ( format != "" ) ? format . substr ( 1 ) : ""
} ,
2017-07-23 04:50:36 +02:00
RemoveItemFromQueue : function ( i , id ) {
this . removeFromParent ( document . getElementById ( "list_item_" + id ) ) ;
2017-07-16 06:43:09 +02:00
delete this . queued [ i ] . selection [ id ] ;
2017-07-23 04:50:36 +02:00
document . getElementById ( "torrent_cb_" + id ) . checked = false ;
document . getElementById ( "torrent_" + id ) . style . display = "" ;
2017-07-16 06:43:09 +02:00
var test = 0 ;
for ( t in this . queued [ i ] . selection ) {
test ++
break ;
}
if ( test == 0 ) this . RemoveFromQueue ( i ) ;
return false ;
} ,
2017-07-23 04:50:36 +02:00
newQueryAttempt : function ( queryUrl , queryPost , callback ) {
2017-07-16 06:43:09 +02:00
Query . Post ( queryUrl , queryPost , function ( response ) {
2017-07-23 04:50:36 +02:00
if ( ( response . length == 0 ) || ( ! response . ok ) ) { // Query has failed
2017-07-16 06:43:09 +02:00
var errorMsg = response . errors . join ( "<br>" ) ;
TorrentsMod . addToLog ( "error" , errorMsg ) ;
TorrentsMod . error _count ++ ;
if ( TorrentsMod . error _count < 2 ) {
TorrentsMod . addToLog ( "success" , T . r ( "try_new_attempt" ) ) ;
TorrentsMod . newQueryAttempt ( queryUrl , queryPost , callback )
2017-06-02 04:51:44 +02:00
} else {
2017-07-16 06:43:09 +02:00
TorrentsMod . addToLog ( "error" , T . r ( "query_is_broken" , queryUrl , queryPost ) ) ;
if ( callback != undefined ) {
TorrentsMod . addToLog ( "error" , "Passing to the next query..." ) ;
callback ( response ) // So we can query only one item
}
2017-06-02 04:51:44 +02:00
}
2017-07-16 06:43:09 +02:00
} else {
var succesMsg = ( response . infos != null ) ? response . infos . join ( "<br>" ) : T . r ( "query_executed_success" ) ;
TorrentsMod . addToLog ( "success" , succesMsg )
if ( callback != undefined ) callback ( response ) // So we can query only one item
}
} ) ;
} ,
2017-07-23 04:50:36 +02:00
QueryQueue : function ( i , callback ) {
2017-07-16 06:43:09 +02:00
if ( this . queued . length > 0 ) {
var QueueAction = this . queued [ i ] ; // we clone it so we can delete it safely
this . RemoveFromQueueAction ( i ) ;
var queryPost = "" ;
var queryUrl = "/mod/api/torrents" ;
QueueAction . queryPost = TorrentsMod . formatSelectionToQuery ( QueueAction . selection )
if ( QueueAction . action == "delete" ) {
2017-07-23 04:50:36 +02:00
queryPost = "action=" + QueueAction . action ;
queryPost += "&withreport=" + QueueAction . withReport ;
queryPost += "&status=" + ( ( QueueAction . status != undefined ) ? QueueAction . status : "" ) ;
queryPost += "&" + QueueAction . queryPost ; // we add torrent id
2017-07-16 06:43:09 +02:00
} else if ( QueueAction . action == "edit" ) {
2017-07-23 04:50:36 +02:00
queryPost = "action=multiple" ;
queryPost += "&status=" + QueueAction . status ;
queryPost += "&owner=" + QueueAction . owner ;
queryPost += "&category=" + QueueAction . category ;
queryPost += "&" + QueueAction . queryPost ; // we add torrent id
2017-07-16 06:43:09 +02:00
}
TorrentsMod . newQueryAttempt ( queryUrl , queryPost , callback )
} else {
TorrentsMod . addToLog ( "success" , T . r ( "all_operations_done" ) )
if ( TorrentsMod . refreshTimeout > 0 ) {
2017-07-23 04:50:36 +02:00
TorrentsMod . addToLog ( "success" , T . r ( "refreshing_in" , TorrentsMod . refreshTimeout / 1000 ) )
setTimeout ( function ( ) {
2017-07-16 06:43:09 +02:00
window . location . reload ( )
} , TorrentsMod . refreshTimeout ) ;
}
}
} ,
2017-07-23 04:50:36 +02:00
QueryLoop : function ( ) {
2017-07-16 06:43:09 +02:00
if ( TorrentsMod . progress _count <= TorrentsMod . progress _max ) {
TorrentsMod . updateProgressBar ( )
TorrentsMod . progress _count ++ ;
if ( TorrentsMod . progress _count > TorrentsMod . progress _max ) TorrentsMod . progress _count = TorrentsMod . progress _max ;
TorrentsMod . QueryQueue ( 0 , TorrentsMod . QueryLoop ) ;
}
} ,
2017-06-02 04:51:44 +02:00
2017-07-16 06:43:09 +02:00
// Event Handlers
2017-07-23 04:50:36 +02:00
checkboxEventHandler : function ( e ) {
2017-07-16 06:43:09 +02:00
var el = e . target ;
TorrentsMod . checkboxEventHandlerFunc ( el ) ;
} ,
2017-07-23 04:50:36 +02:00
checkboxEventHandlerFunc : function ( el ) {
2017-07-16 06:43:09 +02:00
var name = el . dataset . name ;
var id = el . value ;
2017-07-23 04:50:36 +02:00
if ( el . checked ) TorrentsMod . addToSelection ( {
name : name ,
id : id
} ) ;
else TorrentsMod . removeFromSelection ( {
name : name ,
id : id
} ) ;
2017-07-16 06:43:09 +02:00
if ( TorrentsMod . selected . length > 0 ) TorrentsMod . enableBtnActions ( ) ;
else TorrentsMod . disableBtnActions ( ) ;
} ,
2017-06-02 04:51:44 +02:00
2017-07-16 06:43:09 +02:00
// Action Methods
2017-07-23 04:50:36 +02:00
DeleteHandler : function ( locked ) {
2017-07-16 06:43:09 +02:00
var withReport = confirm ( T . r ( "delete_reports_with_torrents" ) )
var selection = TorrentsMod . selected ;
if ( locked )
2017-07-23 04:50:36 +02:00
TorrentsMod . AddToQueue ( {
action : "delete" ,
2017-07-17 15:10:08 +02:00
withReport : withReport ,
selection : selection ,
queryPost : "" ,
infos : T . r ( "with_st" , T . r ( "lock" ) ) + ( ( withReport ) ? T . r ( "and_reports" ) : "" ) ,
2017-07-23 04:50:36 +02:00
status : "5"
} ) ;
2017-07-16 06:43:09 +02:00
else TorrentsMod . AddToQueue ( {
action : "delete" ,
withReport : withReport ,
selection : selection ,
2017-07-17 15:10:08 +02:00
infos : ( withReport ) ? T . r ( "with_st" , T . r ( "reports" ) ) : "" ,
2017-07-23 04:50:36 +02:00
queryPost : ""
} ) ;
for ( i in selection ) document . getElementById ( "torrent_" + i ) . style . display = "none" ;
2017-07-17 15:10:08 +02:00
TorrentsMod . selected = [ ]
TorrentsMod . disableBtnActions ( ) ;
} ,
2017-07-23 04:50:36 +02:00
Delete : function ( e ) {
2017-07-17 15:10:08 +02:00
TorrentsMod . DeleteHandler ( false ) ;
e . preventDefault ( ) ;
} ,
2017-07-23 04:50:36 +02:00
LockDelete : function ( e ) {
2017-07-17 15:10:08 +02:00
TorrentsMod . DeleteHandler ( true ) ;
e . preventDefault ( ) ;
} ,
2017-07-23 04:50:36 +02:00
Edit : function ( e ) {
2017-07-17 15:10:08 +02:00
var selection = TorrentsMod . selected ;
2017-07-23 04:50:36 +02:00
var status = document . querySelector ( ".modtools *[name='" + TorrentsMod . status _input _name + "']" ) . value ;
var owner _id = document . querySelector ( ".modtools *[name='" + TorrentsMod . owner _input _name + "']" ) . value ;
var category = document . querySelector ( ".modtools *[name='" + TorrentsMod . category _input _name + "']" ) . value ;
2017-07-17 15:10:08 +02:00
var infos = "" ;
infos += ( status != "" ) ? T . r ( "status_js" , status ) : "" ;
infos += ( owner _id != "" ) ? T . r ( "owner_id_js" , owner _id ) : "" ;
infos += ( category != "" ) ? T . r ( "category_js" , category ) : "" ;
TorrentsMod . AddToQueue ( {
action : "edit" ,
selection : selection ,
queryPost : "" , // We don't format now, we wait until the query is sent
2017-07-23 04:50:36 +02:00
infos : ( infos != "" ) ? T . r ( "with_st" , infos ) : T . r ( "no_changes" ) ,
2017-07-17 15:10:08 +02:00
status : status ,
category : category ,
2017-07-23 04:50:36 +02:00
owner : owner _id
} ) ;
2017-07-17 15:10:08 +02:00
if ( status != "" ) {
2017-07-23 04:50:36 +02:00
for ( i in selection ) document . getElementById ( "torrent_" + i ) . className = "torrent-info " + TorrentsMod . statusToClassName ( status ) ;
2017-07-17 15:10:08 +02:00
}
TorrentsMod . selected = [ ]
TorrentsMod . disableBtnActions ( ) ;
e . preventDefault ( ) ;
} ,
2017-07-23 04:50:36 +02:00
ApplyChanges : function ( ) {
2017-07-17 15:10:08 +02:00
this . pause = false ;
this . QueryLoop ( ) ;
}
} ;
2017-06-02 04:51:44 +02:00
2017-07-23 04:50:36 +02:00
// Load torrentMods when DOM is ready
document . addEventListener ( "DOMContentLoaded" , function ( ) {
TorrentsMod . checkboxes = document . querySelectorAll ( "input[name='torrent_id']" ) ;
2017-07-17 15:10:08 +02:00
TorrentsMod . Create ( ) ;
} ) ;
2017-07-23 04:50:36 +02:00
// @license-end