Last active
December 13, 2015 20:18
-
-
Save relipse/4968446 to your computer and use it in GitHub Desktop.
a simple way to add a modal confirm box to your website
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
/** | |
* Open confirmation dialog (jqueryui modal) | |
* | |
* @requires jquery, jqueryui | |
* @param {string} c_text text/html to show in the dialog box | |
* @param {string|function(dlg_element)} c_title|confirm_callback title of the dialog box (or callback function) | |
* @param {string|function(dlg_element)} c_btn_text|confirm_callback confirm button text (or callback function) | |
* @param {string|function(dlg_element)} c_btn_cancel_text|confirm_callback cancel button text (defaults to 'Cancel') (or callback function) | |
* @param {function(dlg_element)} confirm_callback callback after the modal box is confirmed | |
*/ | |
function dlgConfirm(c_text, c_title, c_btn_text, c_btn_cancel_text, confirm_callback){ | |
if (typeof(confirm_callback) !== 'function'){ | |
if (typeof(c_title) == 'function'){ | |
confirm_callback = c_title; | |
}else if (typeof(c_btn_text) == 'function'){ | |
confirm_callback = c_btn_text; | |
}else if (typeof(c_btn_cancel_text) == 'function'){ | |
confirm_callback = c_btn_cancel_text; | |
} | |
} | |
if (typeof(c_text) !== 'string'){ | |
c_text = 'Are you sure?'; | |
} | |
if (typeof(c_title) !== 'string'){ | |
c_title = 'Confirm'; | |
} | |
if (typeof(c_btn_text) !== 'string'){ | |
c_btn_text = 'Confirm'; | |
} | |
if (typeof(c_btn_cancel_text) !== 'string'){ | |
c_btn_cancel_text = 'Cancel'; | |
} | |
if ($('#dlgConfirm').length == 0){ | |
$('body').append('<div id="dlgConfirm" title="Confirm" style="display: none">Are you sure?</div>'); | |
} | |
var btns = {}; | |
btns[c_btn_text] = function() { | |
var dlg = this; | |
if (typeof(confirm_callback) == 'function'){ | |
if (confirm_callback(dlg) !== false){ | |
$(this).dialog('close'); | |
} | |
} | |
}; | |
btns[c_btn_cancel_text] = function() { | |
$( this ).dialog("close"); | |
}; | |
$('#dlgConfirm').dialog({ | |
title: c_title, | |
autoOpen: false, | |
resizable: false, | |
//height:170, //commented out so autosize works | |
modal: true, | |
buttons: btns | |
}).html(c_text).dialog('open'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment