Created
October 2, 2019 21:36
-
-
Save rauljose/970eba61e7bae100e3a8e3c25e2acbe9 to your computer and use it in GitHub Desktop.
jQueryUI dialog wrapper: presets and promises
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
/** | |
jQueryUI dialog wrapper: presets and promises | |
ia.alert, ia.alertHighlight, ia.alertInfo, ia.alertWarn, ia.alertError: promise.done clicked ok, promise.fail esc or closed | |
ia.confirm, ia.confirmDelete | |
example alert | |
ia.alertError("el error fue","paso un <b>error</b>",true); | |
var promise = ia.alertWarn("sigo", "ok <o> esc"); | |
promise.done(function(){ | |
console.log("iaAlert* dice: ","Clicked ok="+lalocal); | |
}); | |
promise.fail(function() { | |
console.log("iaAlert* dice: ","cancelo! tache o esc, mi var lalocal="+lalocal); | |
}); | |
example ia.confirm, ia.confirmDelete | |
function confirmExample() { | |
var lalocal="La local variable dice soy locatl"; | |
var promise = ia.confirm("quest its quest", "cera o sera"); | |
promise.done(function(){ // podria ser then | |
console.log("confirmPromiseExample dice:","hizo click en ok mi var lalocal="+lalocal); | |
}); | |
promise.fail(function() { // podria ser then | |
console.log("confirmPromiseExample dice:","click en cancelo, tache o escape mi var lalocal="+lalocal); | |
}); | |
console.log("confirmPromiseExample dice:","mientras le piensa sigo"); | |
} | |
*/ | |
/** | |
* jQueryUI dialog wrapper: presets and promises | |
* | |
* @author Raúl José Santos | |
* @author Informática Asociada | |
* @copyright 2017 | |
* @license MIT | |
* @version 1.0.2 | |
*/ | |
/* jshint strict: true */ | |
/* jshint futurehostile: true */ | |
/* jshint browser: true */ | |
/* jshint devel: false */ | |
/* jshint jquery: true */ | |
/* jshint undef: true, unused: true */ | |
var ia = ia || {}; | |
ia.alert = function(message, title, html, messageIconSpan, addClass, buttonText, buttonIcon) { | |
var defer = $.Deferred(), m=messageIconSpan==null ? '' : messageIconSpan+' '; | |
$('<div/>') | |
.html(m+(html===true ? message : message.replace(/</g,'< '))).addClass(addClass==null ? '' : addClass) | |
.dialog({ | |
autoOpen:true, | |
resizable:true, | |
draggable:true, | |
modal:true, | |
closeOnEscape:true, | |
width:'auto', | |
height:'auto', | |
title: title==null ? 'Aviso' : title, | |
buttons: [ | |
{text:buttonText==null ? 'Ok' : buttonText,icon:buttonIcon==null ? 'ui-icon-check' : buttonIcon, | |
click:function(e){ | |
e.target.disabled = true; | |
if(!defer.isResolved && !defer.isRejected) | |
defer.resolve("true"); | |
$(this).dialog("close"); | |
} | |
}, | |
], | |
close: function () { | |
if(!defer.isResolved && !defer.isRejected) | |
defer.reject(); | |
$(this).dialog('destroy').remove(); | |
} | |
}); | |
return defer.promise(); | |
}; | |
ia.alertHighlight = function(message, title, html, messageIconSpan, addClass, buttonText, buttonIcon) { | |
return ia.alert(message, title, html, messageIconSpan==null ? '<span class="ui-icon ui-icon-info"></span>':messageIconSpan, addClass==null ? 'ui-state-highlight' : addClass, buttonText, buttonIcon); | |
}; | |
ia.alertInfo = function(message, title, html, messageIconSpan, addClass, buttonText, buttonIcon) { | |
return ia.alert(message, title, html, messageIconSpan==null ? '<span class="ui-icon ui-icon-info"></span>':messageIconSpan, addClass, buttonText, buttonIcon); | |
}; | |
ia.alertWarn = function(message, title, html, messageIconSpan, addClass, buttonText, buttonIcon) { | |
return ia.alert(message, title, html, messageIconSpan==null ? '<span class="ui-icon ui-icon-alert"></span>':messageIconSpan, addClass, buttonText, buttonIcon); | |
}; | |
ia.alertError = function(message, title, html, messageIconSpan, addClass, buttonText, buttonIcon) { | |
return ia.alert(message, title, html, messageIconSpan==null ? '<span class="ui-icon ui-icon-alert"></span>':messageIconSpan,addClass==null ? 'ui-state-error-text' : addClass, buttonText, buttonIcon); | |
}; | |
ia.confirm = function(message, title, html) { | |
var defer = $.Deferred(); | |
$('<div/>') | |
.html(html===true ? message : message.replace(/</g,'< ')) | |
.dialog({ | |
autoOpen:true, | |
resizable:true, | |
draggable:true, | |
modal:true, | |
closeOnEscape:true, | |
width:'auto', | |
height:'auto', | |
title: title==null ? 'Por favor, confirma:' : title, | |
buttons: [ | |
{text:'Si',icon:'ui-icon-check', | |
click:function(e){ | |
e.target.disabled = true; | |
if(!defer.isResolved && !defer.isRejected) | |
defer.resolve("true"); | |
$(this).dialog("close"); | |
} | |
}, | |
{text:'No',icon:'ui-icon-cancel', | |
click:function(e){ | |
e.target.disabled = true; | |
if(!defer.isResolved && !defer.isRejected) | |
defer.reject(); | |
$(this).dialog("close"); | |
} | |
} | |
], | |
close: function () { | |
if(!defer.isResolved && !defer.isRejected) | |
defer.reject(); | |
$(this).dialog('destroy').remove(); | |
} | |
}); | |
return defer.promise(); | |
}; | |
ia.confirmDelete = function(message,title,html){ | |
var defer = $.Deferred(); | |
$("<div />").html('<span class="ui-icon ui-icon-trash"></span> ' + (html===true ? message : message.replace(/</g,'< '))).addClass('ui-state-error-text') | |
.dialog({ | |
autoOpen:true, | |
resizable:true, | |
draggable:true, | |
modal:true, | |
closeOnEscape:true, | |
width:'auto', | |
height:'auto', | |
title: title==null ? 'Confirme Eliminar:' : title, | |
buttons: [ | |
{text: "Eliminar",icon:"ui-icon-trash",'class':"ui-state-error-text",tabIndex:-1, | |
click: function(e) { | |
e.target.disabled = true; | |
if(!defer.isResolved && !defer.isRejected) | |
defer.resolve("true"); | |
$(this).dialog("close"); | |
} | |
}, | |
{text:'Cancelar',icon:'ui-icon-cancel', | |
click:function(e){ | |
e.target.disabled = true; | |
if(!defer.isResolved && !defer.isRejected) | |
defer.reject(); | |
$(this).dialog("close"); | |
} | |
} | |
], | |
close: function () { | |
if(!defer.isResolved && !defer.isRejected) | |
defer.reject(); | |
$(this).dialog('destroy').remove(); | |
} | |
}); | |
return defer.promise(); | |
}; | |
ia.tagProtect = function(html) { | |
var tags=['script','object','embed','iframe','frame','applet','button','input','form','base','param'] ; | |
for(var i=0,tagsLen=tags.length; i<tagsLen; i++ ) { | |
var regExp = new RegExp( '<(' + tags[i] + ')|<\/(' + tags[i] +')', 'gimu'); | |
html = html.replace(regExp, ' $1 /$2'); | |
} | |
return html; | |
}; | |
ia.textWidth = function(text,addClass){ | |
var calc = $('<span style="display:none">' + text + '</span>'); | |
if(typeof addClass!=='undefined') | |
calc.addClass(addClass); | |
else | |
calc.addClass('ui-dialog-title-bar ui-widget-header ui-dialog-title'); // add 46 for dialog width | |
$('body').append(calc); | |
var width = calc.width(); | |
calc.remove(); | |
return width; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment