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 = {
// 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 : [ ] ,
unique _id : 1 ,
error _count : 0 ,
progress _count : 0 ,
progress _max : 0 ,
pause : false ,
2017-06-02 15:42:53 +02:00
enabled : false ,
2017-06-02 04:51:44 +02:00
// Init method
Create : function ( ) {
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 ;
for ( var i = 0 ; i < btn _actions . length ; i ++ ) {
btn _actions [ i ] . disabled = true ;
switch ( btn _actions [ i ] . id ) {
case this . delete _btn :
btn _actions [ i ] . addEventListener ( "click" , this . Delete )
break ;
case this . lock _delete _btn :
btn _actions [ i ] . addEventListener ( "click" , this . LockDelete )
break ;
case this . edit _btn :
btn _actions [ i ] . addEventListener ( "click" , this . Edit )
break ;
default :
break ;
}
}
for ( var i = 0 ; i < this . checkboxes . length ; i ++ ) {
checkbox = this . checkboxes [ i ] ;
checkbox . addEventListener ( "change" , this . checkboxEventHandler )
}
sh _btn . addEventListener ( "click" , function ( e ) {
var divActions = this . nextElementSibling ;
if ( divActions . style . display == "inline" ) {
2017-06-02 15:42:53 +02:00
TorrentsMod . enabled = false ;
} else {
TorrentsMod . enabled = true ;
2017-06-02 04:51:44 +02:00
}
2017-06-02 15:42:53 +02:00
divActions . style . display = ( TorrentsMod . enabled ) ? "inline" : "none" ;
2017-06-02 04:51:44 +02:00
var td _cbs = document . getElementsByClassName ( "tr-cb" )
for ( var i = 0 ; i < td _cbs . length ; i ++ ) {
td _cb = td _cbs [ i ] ;
2017-06-02 15:42:53 +02:00
td _cb . style . display = ( TorrentsMod . enabled ) ? "table-cell" : "none" ;
2017-06-02 04:51:44 +02:00
}
var toggleText = this . dataset . toggleText ;
this . dataset . toggleText = this . innerText ;
this . innerText = toggleText ;
} ) ;
} ,
// generate a unique id for a query
getId : function ( ) {
return this . unique _id ++ ;
} ,
// UI Methods
selectAll : function ( bool ) {
var l = TorrentsMod . checkboxes . length ;
for ( var i = 0 ; i < l ; i ++ ) {
TorrentsMod . checkboxes [ i ] . checked = bool ;
TorrentsMod . checkboxEventHandlerFunc ( TorrentsMod . checkboxes [ i ] ) ;
}
} ,
disableBtnActions : function ( ) {
var btn _actions = document . getElementsByClassName ( this . btn _class _action )
for ( var i = 0 ; i < btn _actions . length ; i ++ ) {
btn _actions [ i ] . disabled = true ;
}
} ,
enableBtnActions : function ( ) {
var btn _actions = document . getElementsByClassName ( this . btn _class _action )
for ( var i = 0 ; i < btn _actions . length ; i ++ ) {
btn _actions [ i ] . disabled = false ;
}
} ,
enableBtnSubmit : function ( ) {
var btn _submit = document . getElementsByClassName ( this . btn _class _submit )
btn _submit [ 0 ] . disabled = false ;
} ,
enableApplyChangesBtn : function ( ) {
var btn _apply _changes = document . getElementById ( "confirm_changes" ) ;
btn _apply _changes . disabled = false ;
} ,
disableBtnSubmit : function ( ) {
var btn _submit = document . getElementsByClassName ( this . btn _class _submit )
btn _submit [ 0 ] . disabled = true ;
} ,
disableApplyChangesBtn : function ( ) {
var btn _apply _changes = document . getElementById ( "confirm_changes" ) ;
btn _apply _changes . disabled = true ;
} ,
removeDivFromList : function ( i ) {
var queueAction = this . queued [ i ] ;
var parentDiv = document . getElementById ( queueAction . unique _id ) . parentNode ;
parentDiv . removeChild ( document . getElementById ( queueAction . unique _id ) ) ;
} ,
removeFromParent : function ( el ) {
var parentDiv = el . parentNode ;
parentDiv . removeChild ( el ) ;
} ,
generatingModal : function ( ) {
listLength = this . queued . length ;
var div = { "edit" : "" , "delete" : "" } ;
for ( var i = 0 ; i < listLength ; i ++ ) {
var listHTML = "" ;
for ( key in this . queued [ i ] . selection ) {
var selection = this . queued [ i ] . selection [ key ] ;
selection . key = i ;
listHTML += Templates . Render ( "torrents." + this . queued [ i ] . action + ".item" , selection ) ;
}
this . queued [ i ] . list = listHTML ;
this . queued [ i ] . key = i ;
div [ this . queued [ i ] . action ] += Templates . Render ( "torrents." + this . queued [ i ] . action + ".block" , this . queued [ i ] ) ;
}
this . progress _count = 0 ;
this . progress _max = listLength ;
document . querySelector ( ".modal .edit_changes" ) . innerHTML = div [ "edit" ] ;
document . querySelector ( ".modal .delete_changes" ) . innerHTML = div [ "delete" ] ;
} ,
toggleList : function ( el ) {
el . parentNode . nextSibling . style . display = ( el . parentNode . nextSibling . style . display != "block" ) ? "block" : "none"
} ,
addToLog : function ( type , msg ) {
var logDiv = document . querySelector ( ".modal .logs_mess" ) ;
if ( logDiv . style . display == "none" ) logDiv . style . display = "block"
logDiv . innerHTML += Templates . Render ( "torrents.logs." + type , msg ) ;
} ,
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 ;
} ,
resetModal : function ( ) {
var logDiv = document . querySelector ( ".modal .logs_mess" ) ;
logDiv . style . display = "none" ;
document . querySelector ( "#" + this . progress _bar _id ) . style . display = "none" ;
logDiv . innerHTML = "" ;
document . querySelector ( ".modal .edit_changes" ) . innerHTML = "" ;
document . querySelector ( ".modal .delete_changes" ) . innerHTML = "" ;
this . enableApplyChangesBtn ( ) ;
} ,
statusToClassName : function ( status ) {
var className = [ "" , "normal" , "remake" , "trusted" , "aplus" , "" ]
return className [ status ] ;
} ,
// Selection Management Methods
addToSelection : function ( torrent ) {
this . selected [ torrent . id ] = torrent ;
} ,
removeFromSelection : function ( torrent ) {
delete this . selected [ torrent . id ] ;
for ( t in this . selected ) {
return
}
this . selected = [ ] ;
} ,
// Query Queue Management Methods
AddToQueue : function ( QueueAction ) {
QueueAction . unique _id = this . getId ( ) ; // used for DOM interaction
this . queued . push ( QueueAction ) ;
this . enableBtnSubmit ( )
} ,
RemoveFromQueueAfterItems : function ( i ) {
for ( t in this . queued [ i ] . selection ) {
this . RemoveItemFromQueue ( i , t ) ;
}
} ,
RemoveFromQueue : function ( i ) {
this . progress _max = ( this . progress _max - 1 >= 0 ) ? this . progress _max - 1 : 0 ;
this . RemoveFromQueueAction ( i )
return false ;
} ,
RemoveFromQueueAction : function ( i ) {
this . removeFromParent ( document . getElementById ( "list_" + this . queued [ i ] . unique _id ) ) ;
this . queued . splice ( i , 1 ) ;
if ( this . queued . length == 0 ) {
this . disableBtnSubmit ( ) ;
if ( this . progress _max > 0 ) {
this . disableApplyChangesBtn ( ) ;
} else {
Modal . CloseActive ( ) ;
}
}
} ,
formatSelectionToQuery : function ( selection ) {
var format = "" ;
for ( s in selection ) {
format += "&torrent_id=" + selection [ s ] . id
}
return ( format != "" ) ? format . substr ( 1 ) : ""
} ,
RemoveItemFromQueue : function ( i , id ) {
this . removeFromParent ( document . getElementById ( "list_item_" + id ) ) ;
delete this . queued [ i ] . selection [ id ] ;
document . getElementById ( "torrent_cb_" + id ) . checked = false ;
document . getElementById ( "torrent_" + id ) . style . display = "" ;
var test = 0 ;
for ( t in this . queued [ i ] . selection ) {
test ++
break ;
}
if ( test == 0 ) this . RemoveFromQueue ( i ) ;
return false ;
} ,
newQueryAttempt : function ( queryUrl , queryPost , callback ) {
Query . Post ( queryUrl , queryPost , function ( response ) {
if ( ( response . length == 0 ) || ( ! response . ok ) ) { // Query has failed
var errorMsg = response . errors . join ( "<br>" ) ;
TorrentsMod . addToLog ( "error" , errorMsg ) ;
TorrentsMod . error _count ++ ;
if ( TorrentsMod . error _count < 2 ) {
2017-06-02 15:42:53 +02:00
TorrentsMod . addToLog ( "success" , T . r ( "try_new_attempt" ) ) ;
2017-06-02 04:51:44 +02:00
TorrentsMod . newQueryAttempt ( queryUrl , queryPost , callback )
} else {
2017-06-02 15:42:53 +02:00
TorrentsMod . addToLog ( "error" , T . r ( "query_is_broken" , queryUrl , queryPost ) ) ;
2017-06-02 04:51:44 +02:00
if ( callback != undefined ) {
TorrentsMod . addToLog ( "error" , "Passing to the next query..." ) ;
callback ( response ) // So we can query only one item
}
}
} else {
2017-06-02 15:42:53 +02:00
var succesMsg = ( response . infos != null ) ? response . infos . join ( "<br>" ) : T . r ( "query_executed_success" ) ;
2017-06-02 04:51:44 +02:00
TorrentsMod . addToLog ( "success" , succesMsg )
if ( callback != undefined ) callback ( response ) // So we can query only one item
}
} ) ;
} ,
QueryQueue : function ( i , callback ) {
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" ) {
queryPost = "action=" + QueueAction . action ;
queryPost += "&withreport=" + QueueAction . withReport ;
queryPost += "&status=" + ( ( QueueAction . status != undefined ) ? QueueAction . status : "" ) ;
queryPost += "&" + QueueAction . queryPost ; // we add torrent id
} else if ( QueueAction . action == "edit" ) {
queryPost = "action=multiple" ;
queryPost += "&status=" + QueueAction . status ;
queryPost += "&owner=" + QueueAction . owner ;
queryPost += "&category=" + QueueAction . category ;
queryPost += "&" + QueueAction . queryPost ; // we add torrent id
}
TorrentsMod . newQueryAttempt ( queryUrl , queryPost , callback )
} else {
2017-06-02 15:42:53 +02:00
TorrentsMod . addToLog ( "success" , T . r ( "all_operations_done" ) )
2017-06-02 04:51:44 +02:00
if ( TorrentsMod . refreshTimeout > 0 ) {
2017-06-02 15:42:53 +02:00
TorrentsMod . addToLog ( "success" , T . r ( "refreshing_in" , TorrentsMod . refreshTimeout / 1000 ) )
2017-06-02 04:51:44 +02:00
setTimeout ( function ( ) {
window . location . reload ( )
} , TorrentsMod . refreshTimeout ) ;
}
}
} ,
QueryLoop : function ( ) {
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 ) ;
}
} ,
// Event Handlers
checkboxEventHandler : function ( e ) {
var el = e . target ;
TorrentsMod . checkboxEventHandlerFunc ( el ) ;
} ,
checkboxEventHandlerFunc : function ( el ) {
var name = el . dataset . name ;
var id = el . value ;
if ( el . checked ) TorrentsMod . addToSelection ( { name : name , id : id } ) ;
else TorrentsMod . removeFromSelection ( { name : name , id : id } ) ;
if ( TorrentsMod . selected . length > 0 ) TorrentsMod . enableBtnActions ( ) ;
else TorrentsMod . disableBtnActions ( ) ;
} ,
// Action Methods
DeleteHandler : function ( locked ) {
2017-06-02 15:42:53 +02:00
var withReport = confirm ( T . r ( "delete_reports_with_torrents" ) )
2017-06-02 04:51:44 +02:00
var selection = TorrentsMod . selected ;
if ( locked )
TorrentsMod . AddToQueue ( { action : "delete" ,
withReport : withReport ,
selection : selection ,
queryPost : "" ,
2017-06-02 15:42:53 +02:00
infos : T . r ( "with_lock" ) + ( ( withReport ) ? T . r ( "and_reports" ) : "" ) ,
2017-06-02 04:51:44 +02:00
status : "5" } ) ;
else TorrentsMod . AddToQueue ( {
action : "delete" ,
withReport : withReport ,
selection : selection ,
2017-06-02 15:42:53 +02:00
infos : ( withReport ) ? T . r ( "with_reports" ) : "" ,
2017-06-02 04:51:44 +02:00
queryPost : "" } ) ;
for ( i in selection ) document . getElementById ( "torrent_" + i ) . style . display = "none" ;
TorrentsMod . selected = [ ]
TorrentsMod . disableBtnActions ( ) ;
} ,
Delete : function ( e ) {
TorrentsMod . DeleteHandler ( false ) ;
e . preventDefault ( ) ;
} ,
LockDelete : function ( e ) {
TorrentsMod . DeleteHandler ( true ) ;
e . preventDefault ( ) ;
} ,
Edit : function ( e ) {
var selection = TorrentsMod . selected ;
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 ;
var infos = "" ;
2017-06-02 15:42:53 +02:00
infos += ( status != "" ) ? T . r ( "status_js" , status ) : "" ;
infos += ( owner _id != "" ) ? T . r ( "owner_id_js" , owner _id ) : "" ;
infos += ( category != "" ) ? T . r ( "category_js" , category ) : "" ;
2017-06-02 04:51:44 +02:00
TorrentsMod . AddToQueue ( {
action : "edit" ,
selection : selection ,
queryPost : "" , // We don't format now, we wait until the query is sent
2017-06-02 15:42:53 +02:00
infos : ( infos != "" ) ? T . r ( "with_st" , infos ) : T . r ( "no_changes" ) ,
2017-06-02 04:51:44 +02:00
status : status ,
category : category ,
owner : owner _id } ) ;
if ( status != "" ) {
for ( i in selection ) document . getElementById ( "torrent_" + i ) . className = "torrent-info " + TorrentsMod . statusToClassName ( status ) ;
}
TorrentsMod . selected = [ ]
TorrentsMod . disableBtnActions ( ) ;
e . preventDefault ( ) ;
} ,
ApplyChanges : function ( ) {
this . pause = false ;
this . QueryLoop ( ) ;
}
} ;
// Load torrentMods when DOM is ready
document . addEventListener ( "DOMContentLoaded" , function ( ) {
TorrentsMod . checkboxes = document . querySelectorAll ( "input[name='torrent_id']" ) ;
TorrentsMod . Create ( ) ;
2017-06-04 07:43:41 +02:00
} ) ;
// @license-end