Last active
December 23, 2015 04:49
-
-
Save paulkaplan/6583296 to your computer and use it in GitHub Desktop.
Make a potentially dangerous function 'confirmable' by wrapping the function call in a confirmation dialog box.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#confirmation-overlay { | |
position: absolute; | |
width: 100%; | |
height:100%; | |
background: rgba(0,0,0,0.5); | |
top:0; | |
left:0; | |
line-height:18px; | |
font-size:18px; | |
z-index:100000000; | |
} | |
#confirmation-dialog { | |
position:absolute; | |
width:20%; | |
min-width:200px; | |
background:#fff; | |
border:1px solid #eee; | |
top:40%; | |
left:40%; | |
z-index:100000001; | |
} | |
#confirmation-buttons { | |
margin-top:1em; | |
padding:1em; | |
} | |
#confirmation-buttons div { | |
display: inline-block; | |
padding:0.75em; | |
margin-bottom:0.5em; | |
padding:0.5em 1em; | |
float: right; | |
cursor: pointer; | |
} | |
#confirmation-cancel { | |
color: #bbb; | |
background:#fafafa; | |
} | |
#confirmation-confirm { | |
background: #4AA9DE; | |
color:#fff; | |
} | |
#confirmation-header { | |
background-color: #f3f3f3; | |
padding:0.5em 1em; | |
} | |
#confirmation-prompt { | |
padding:1em; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Returns the input function wrapped so that it will bring up | |
a confirmation dialog before continuing | |
startSomethingScary = requireConfirmation({ | |
fn : startSomethingScary, | |
headerText : 'Warning!' | |
prompt : 'This action will move the machine', | |
cancelText : 'Cancel', | |
confirmText : 'Ok' | |
}) | |
*/ | |
var requireConfirmation = (function(){ | |
function make_el(opts){ | |
if(!opts){ opts = {}; } | |
if(!opts.prompt){ opts.prompt = 'This action requires confirmation'; } | |
if(!opts.cancelText){ opts.cancelText = 'Cancel'; } | |
if(!opts.confirmText){ opts.confirmText = 'Ok'; } | |
if(!opts.headerText) { opts.headerText = 'Warning!'; } | |
return '<div id="confirmation-container">\ | |
<div id="confirmation-overlay"></div>\ | |
<div id="confirmation-dialog">\ | |
<div id="confirmation-header">\ | |
<h3>'+opts.headerText+'</h3>\ | |
</div>\ | |
<div id="confirmation-prompt">'+opts.prompt+'</div>\ | |
<div id="confirmation-buttons">\ | |
<div id="confirmation-cancel">'+opts.cancelText+'</div>\ | |
<div id="confirmation-confirm">'+opts.confirmText+'</div>\ | |
</div>\ | |
</div>\ | |
</div>'.replace("\n"," ") | |
} | |
function cancel(){ | |
$("#confirmation-container").remove(); | |
}; | |
var requireConfirmation = function(fn, opts){ | |
return function(fn_opts){ | |
$("body").append( make_el(opts) ); | |
// Bind canceling actions | |
$("#confirmation-cancel").click( cancel ); | |
$("#confirmation-overlay").click( cancel ); | |
// Bind confirm ation | |
$("#confirmation-confirm").click( function(){ | |
cancel(); | |
fn(fn_opts); | |
}) | |
} | |
} | |
return requireConfirmation; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment