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
// Get the modal
var Modal = {
2017-07-16 06:43:09 +02:00
active : 0 ,
2017-08-01 13:54:01 +02:00
// Initialise a modal or multiple ones
// takes as parameter an object params:
// @param params Object{ elements: NodeList|Node, button: ID(eg. #something)|Class(eg. .something), before: callback, after: callback, close: callback }
2017-07-16 06:43:09 +02:00
Init : function ( params ) {
var elements = params . elements
var button = ( params . button != undefined ) ? params . button : false
2017-08-01 13:54:01 +02:00
if ( elements . innerHTML == undefined ) {
2017-07-16 06:43:09 +02:00
var nbEl = elements . length
2017-07-23 04:50:36 +02:00
for ( var i = 0 ; i < nbEl ; i ++ ) {
2017-07-16 06:43:09 +02:00
var modal = elements [ i ] ;
this . addModal ( modal , button , i , params . before , params . after , params . close )
}
2017-08-01 13:54:01 +02:00
} else {
this . addModal ( modal , button , i , params . before , params . after , params . close )
2017-06-02 04:51:44 +02:00
}
2017-07-16 06:43:09 +02:00
} ,
2017-08-01 13:54:01 +02:00
// addModal prepare a modal (called by Init so you don't have use it)
// @param modal Node
// @param btn ID(eg. #something)|Class(eg. .something)
// @param i If multiple btn, points out to which btn in the array apply the event
// @param before_callback callback called before opening a modal
// @param after_callback callback called after opening a modal
// @param close_callback callback called after closing a modal
2017-07-23 04:50:36 +02:00
addModal : function ( modal , btn , i , before _callback , after _callback , close _callback ) {
2017-07-16 06:43:09 +02:00
var isBtnArray = false ;
// Get the button that opens the modal
if ( ! btn ) {
2017-07-23 04:50:36 +02:00
btn = document . getElementById ( "modal_btn_" + modal . id )
2017-08-01 23:38:13 +02:00
} else if ( typeof ( btn ) == "string" && btn . match ( /^#/ ) ) {
2017-07-16 06:43:09 +02:00
btn = document . getElementById ( btn . substr ( 1 ) ) ;
2017-08-01 23:38:13 +02:00
} else if ( typeof ( btn ) == "string" && btn . match ( /^\./ ) ) {
2017-07-16 06:43:09 +02:00
btn = document . getElementsByClassName ( btn . substr ( 1 ) ) ;
isBtnArray = true ;
2017-08-01 13:54:01 +02:00
} else if ( btn instanceof Array ) {
btn = btn . map ( function ( val , index ) {
if ( val . match ( /^#/ ) ) {
return document . getElementById ( val . substr ( 1 ) ) ;
} else if ( val . match ( /^\./ ) ) {
return document . getElementsByClassName ( val . substr ( 1 ) ) [ index ] ;
}
return document . querySelector ( val )
} )
isBtnArray = true ;
2017-07-16 06:43:09 +02:00
} else {
2017-08-01 13:54:01 +02:00
console . error ( "Couldn't find the button, please provide either a #id, a .classname or an array of #id" )
2017-07-16 06:43:09 +02:00
return
}
if ( ( isBtnArray ) && ( i > 0 ) && ( btn . length > 0 ) && ( btn . length > i ) ) {
2017-07-23 04:50:36 +02:00
btn [ i ] . addEventListener ( "click" , function ( e ) {
2017-07-16 06:43:09 +02:00
if ( before _callback != undefined ) before _callback ( )
modal . style . display = "block" ;
Modal . active = modal ;
if ( after _callback != undefined ) after _callback ( )
e . preventDefault ( ) ;
} ) ;
} else {
btn = ( isBtnArray ) ? btn [ 0 ] : btn ;
// When the user clicks on the button, open the modal
2017-07-23 04:50:36 +02:00
btn . addEventListener ( "click" , function ( e ) {
2017-07-16 06:43:09 +02:00
if ( before _callback != undefined ) before _callback ( )
modal . style . display = "block" ;
Modal . active = modal ;
if ( after _callback != undefined ) after _callback ( )
e . preventDefault ( ) ;
} ) ;
}
// Get the <span> element that closes the modal
2017-07-23 04:50:36 +02:00
var span = document . querySelectorAll ( "#" + modal . id + " .close" ) [ 0 ]
2017-07-16 06:43:09 +02:00
// When the user clicks on <span> (x), close the modal
2017-07-23 04:50:36 +02:00
span . addEventListener ( "click" , function ( e ) {
2017-07-16 06:43:09 +02:00
modal . style . display = "none" ;
Modal . active = 0 ;
if ( close _callback != undefined ) close _callback ( )
e . preventDefault ( ) ;
} ) ;
// When the user clicks anywhere outside of the modal, close it
2017-07-23 04:50:36 +02:00
window . addEventListener ( "click" , function ( event ) {
2017-07-16 06:43:09 +02:00
if ( event . target == modal ) {
modal . style . display = "none" ;
Modal . active = 0 ;
if ( close _callback != undefined ) close _callback ( )
}
} ) ;
} ,
2017-08-01 13:54:01 +02:00
// CloseActive closes the opened modal, if any
2017-07-23 04:50:36 +02:00
CloseActive : function ( ) {
2017-07-16 06:43:09 +02:00
if ( this . active != 0 ) {
2017-07-23 04:50:36 +02:00
this . active . style . display = "none" ;
2017-07-16 06:43:09 +02:00
this . active = 0 ;
}
} ,
2017-08-01 13:54:01 +02:00
// GetActive return the opened modal div
2017-07-23 04:50:36 +02:00
GetActive : function ( ) {
2017-07-16 06:43:09 +02:00
return this . active ;
} ,
2017-08-01 13:54:01 +02:00
// Open opens a modal and closes the active one if any
2017-07-23 04:50:36 +02:00
Open : function ( q ) {
2017-08-01 13:54:01 +02:00
var activeModal = this . GetActive ( )
if ( activeModal != 0 ) {
this . CloseActive ( )
}
2017-07-16 06:43:09 +02:00
var modal = document . querySelector ( q ) ;
if ( modal != undefined ) {
2017-07-23 04:50:36 +02:00
modal . style . display = "none" ;
2017-07-16 06:43:09 +02:00
this . active = modal ;
}
}
2017-06-04 07:43:41 +02:00
} ;
// @license-end