@ -1,28 +1,98 @@
var yo = require ( 'yo-yo' )
var csjs = require ( 'csjs-inject' )
var css = csjs `
. modal {
display : none ; /* Hidden by default */
position : fixed ; /* Stay in place */
z - index : 1000 ; /* Sit on top of everything including the dragbar */
left : 0 ;
top : 0 ;
width : 100 % ; /* Full width */
height : 100 % ; /* Full height */
overflow : auto ; /* Enable scroll if needed */
background - color : rgb ( 0 , 0 , 0 ) ; /* Fallback color */
background - color : rgba ( 0 , 0 , 0 , 0.4 ) ; /* Black w/ opacity */
}
. modalHeader {
padding : 2 px 16 px ;
background - color : orange ;
color : white ;
}
. modalBody {
padding : 1.5 em ;
line - height : 1.5 em ;
}
. modalFooter {
padding : 10 px 30 px ;
background - color : orange ;
color : white ;
text - align : right ;
font - weight : 700 ;
cursor : pointer ;
}
. modalContent {
position : relative ;
background - color : # fefefe ;
margin : auto ;
padding : 0 ;
line - height : 18 px ;
font - size : 13 px ;
border : 1 px solid # 888 ;
width : 50 % ;
box - shadow : 0 4 px 8 px 0 rgba ( 0 , 0 , 0 , 0.2 ) , 0 6 px 20 px 0 rgba ( 0 , 0 , 0 , 0.19 ) ;
- webkit - animation - name : animatetop ;
- webkit - animation - duration : 0.4 s ;
animation - name : animatetop ;
animation - duration : 0.4 s
}
. modalFooterOk {
cursor : pointer ;
}
. modalFooterCancel {
cursor : pointer ;
}
@ - webkit - keyframes animatetop {
from { top : - 300 px ; opacity : 0 }
to { top : 0 ; opacity : 1 }
}
@ keyframes animatetop {
from { top : - 300 px ; opacity : 0 }
to { top : 0 ; opacity : 1 }
}
`
module . exports = ( title , content , ok , cancel ) => {
module . exports = ( title , content , ok , cancel ) => {
var container = document . querySelector ( ` . ${ css . modal } ` )
if ( ! container ) {
document . querySelector ( 'body' ) . appendChild ( html ( ) )
container = document . querySelector ( ` . ${ css . modal } ` )
}
var okDiv = document . getElementById ( 'modal-footer-ok' )
var okDiv = document . getElementById ( 'modal-footer-ok' )
var cancelDiv = document . getElementById ( 'modal-footer-cancel' )
okDiv . innerHTML = ( ok && ok . label !== undefined ) ? ok . label : 'OK'
okDiv . innerHTML = ( ok && ok . label !== undefined ) ? ok . label : 'OK'
var cancelDiv = document . getElementById ( 'modal-footer-cancel' )
cancelDiv . innerHTML = ( cancel && cancel . label !== undefined ) ? cancel . label : 'Cancel'
cancelDiv . innerHTML = ( cancel && cancel . label !== undefined ) ? cancel . label : 'Cancel'
var modal = document . querySelector ( '.modal-body' )
var modal = document . querySelector ( ` . ${ css . modalBody } ` )
var modaltitle = document . querySelector ( '.modal-header h2' )
var modalT itle = document . querySelector ( ` . ${ css . modalHeader } h2 ` )
modaltitle . innerHTML = ' - '
modalT itle . innerHTML = ''
if ( title ) modaltitle . innerHTML = title
if ( title ) modalT itle . innerHTML = title
modal . innerHTML = ''
modal . innerHTML = ''
if ( content ) modal . appendChild ( content )
if ( content ) modal . appendChild ( content )
var container = document . querySelector ( '.modal' )
container . style . display = container . style . display === 'block' ? hide ( ) : show ( )
container . style . display = container . style . display === 'block' ? hide ( ) : show ( )
function okListenner ( ) {
function okListener ( ) {
hide ( )
hide ( )
if ( ok && ok . fn ) ok . fn ( )
if ( ok && ok . fn ) ok . fn ( )
removeEventListener ( )
removeEventListener ( )
}
}
function cancelListenner ( ) {
function cancelListener ( ) {
hide ( )
hide ( )
if ( cancel && cancel . fn ) cancel . fn ( )
if ( cancel && cancel . fn ) cancel . fn ( )
removeEventListener ( )
removeEventListener ( )
@ -37,10 +107,26 @@ module.exports = (title, content, ok, cancel) => {
}
}
function removeEventListener ( ) {
function removeEventListener ( ) {
okDiv . removeEventListener ( 'click' , okListenn er )
okDiv . removeEventListener ( 'click' , okListener )
cancelDiv . removeEventListener ( 'click' , cancelListenn er )
cancelDiv . removeEventListener ( 'click' , cancelListener )
}
}
okDiv . addEventListener ( 'click' , okListenner )
okDiv . addEventListener ( 'click' , okListener )
cancelDiv . addEventListener ( 'click' , cancelListenner )
cancelDiv . addEventListener ( 'click' , cancelListener )
}
function html ( ) {
return yo ` <div id="modal-dialog" class=" ${ css . modal } " onclick="clickModal()" >
< div class = "${css['modalContent']}" >
< div class = "${css['modalHeader']}" >
< h2 > < / h 2 >
< / d i v >
< div class = "${css['modalBody']}" > -
< / d i v >
< div class = "${css['modalFooter']}" >
< span id = "modal-footer-ok" class = "modalFooterOk" > OK < / s p a n > < s p a n i d = " m o d a l - f o o t e r - c a n c e l " c l a s s = " m o d a l F o o t e r C a n c e l " > C a n c e l < / s p a n >
< / d i v >
< / d i v >
< / d i v >
< / d i v > `
}
}