Skip to content

Instantly share code, notes, and snippets.

@bitifet
Last active June 13, 2022 22:38
Show Gist options
  • Save bitifet/eeefce6f97de2229b5c1 to your computer and use it in GitHub Desktop.
Save bitifet/eeefce6f97de2229b5c1 to your computer and use it in GitHub Desktop.
jQuery-UI replacement for window's alert() cofirm() and prompt() dialog functions.
// document dialogs overload:
// ==========================
//
// WARNING: This javascript versions CAN'T stop rest of javascript processing (except by an infinite
// loop which will also block this dialogs functionalities).
//
// I tryed to implement a "non-self-blocking" internal loop but need a nextTick() function which is
// not available on most (if not all) browser javascript implementations.
//
// I tryed to emulate it using ontimeout()'s and other tricks, but it doesn't work anyway.
//
// Hence I left confirm() and prompt() replacements commented out because they returns instantly even
// they render and works correctly and, then, there is no way to return any result regarding (actually future)
// user input.
//
// But BE AWARE that even alert() replacement DOESN'T stop javascript loop as native window.alert() does. This can
// cause unexpected behaviours and, because this implementation reuses DOM template, also can HIDE some alerts
// OVERWRITTEN by next calls.
(function jQueryzeWindowDialogs (options) {
var setup = false;
var lock = false;
// Dialog layout://{{{
var dialog = $("<div></div>")
.css({
background: "#ffffff",
padding: "1em",
})
;//}}}
function parseText(text) {//{{{
text = text.split(/\n+/);
return "<p>" + text.join("</p><p>") + "</p>";
};//}}}
function triggerPopup(title, contents, buttonSet) {//{{{
if (! setup) {//{{{
dialog
.dialog({
autoOpen: false,
modal: true,
buttons: buttonSet
})
;
setup = true;
};//}}}
/// while (lock) {nextTick();};
/// lock = true;
dialog.dialog("option", "title", title);
dialog.html("");
dialog.append(contents);
dialog.dialog("open");
/// while (lock) {nextTick();};
};//}}}
window.alert = function jqueryAlertReplace (text, title) {//{{{
triggerPopup (
title ? title : options.defaultAlertTitle,
$("<div></div>").html(parseText(text)),
{
ok: {
text: options.okLabel,
click: function() {
$(this).dialog( "close" );
lock = false;
}
},
}
);
};//}}}
/* DISABLED: (See top comments)
window.confirm = function jqueryConfirmReplace (text, title) {//{{{
var response;
triggerPopup (
title ? title : options.defaultConfirmTitle,
$("<div></div>").html(parseText(text)),
{
cancel: {
text: options.cancelLabel,
click: function() {
$(this).dialog( "close" );
response = false;
lock = false;
}
},
ok: {
text: options.okLabel,
click: function() {
$(this).dialog( "close" );
response = true;
lock = false;
}
},
}
);
return response;
};//}}}
window.prompt = function jqueryPromptReplace (text, title) {//{{{
var response;
var input = $("<input></input>")
.css({
width: "100%",
})
;
triggerPopup (
title ? title : options.defaultConfirmTitle,
$("<div></div>").html(parseText(text)).append(input),
{
cancel: {
text: options.cancelLabel,
click: function() {
$(this).dialog( "close" );
lock = false;
}
},
ok: {
text: options.okLabel,
click: function() {
$(this).dialog( "close" );
response = input.val();
lock = false;
}
},
}
);
return response;
};//}}}
*/
})({
defaultAlertTitle: "Avís",
defaultConfirmTitle: "Confirmació",
okLabel: "D'acord",
cancelLabel: "Cancel·lar",
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment