Skip to content

Instantly share code, notes, and snippets.

@felipefrancisco
Last active January 23, 2016 14:18
Show Gist options
  • Save felipefrancisco/0d14aa0d826ab6a09d0c to your computer and use it in GitHub Desktop.
Save felipefrancisco/0d14aa0d826ab6a09d0c to your computer and use it in GitHub Desktop.
ModalEngine with support for Bootbox and Bootstrap-SweetAlert
/**
* ModalEngine with support for Bootbox and Bootstrap-SweetAlert
*
* @author Felipe Francisco
* @see https://gist.github.com/felipefrancisco/0d14aa0d826ab6a09d0c/
*
* Get bootbox & bootstrap-SweetAlert.
* @see https://github.com/lipis/bootstrap-sweetalert
* @see https://github.com/makeusabrew/bootbox
*/
function SweetAlertEngine() {
this.engine = swal;
this.alert = function(opts) {
this.engine({
title: opts.title || 'Informação',
text: opts.message,
type: "info",
confirmButtonClass: 'btn-info',
confirmButtonText: 'OK'
}, function() {
if(typeof opts.confirm == 'function')
opts.confirm(opts);
});
return $('.sweet-alert');
};
this.error = function(opts) {
this.engine({
title: opts.title || 'Erro',
text: opts.message,
type: "error",
confirmButtonClass: 'btn-danger',
confirmButtonText: 'OK'
}, function() {
if(typeof opts.confirm == 'function')
opts.confirm(opts);
});
return $('.sweet-alert');
};
this.dialog = function(opts) {
this.engine({
title: opts.title || 'Informação',
text: opts.message,
type: "info",
showCancelButton:true,
confirmButtonClass: 'btn-info',
confirmButtonText: 'OK'
}, function(isConfirm) {
if (isConfirm) {
if(typeof opts.confirm == 'function')
opts.confirm(opts);
} else {
if(typeof opts.cancel == 'function')
opts.cancel(opts);
}
});
return $('.sweet-alert');
};
}
SweetAlertEngine.prototype.name = "SweetAlertEngine";
SweetAlertEngine.prototype.constructor = SweetAlertEngine;
function BootboxEngine() {
this.engine = bootbox;
this.alert = function(opts) {
if(!opts)
opts = {};
return this.engine.dialog({
message: opts.message,
title: opts.title || "Informação",
closeButton: false,
buttons: opts.buttons || {
ok: {
label: "OK",
className: "btn-primary",
callback: function(e) {
if(opts.hasOwnProperty('confirm') && typeof opts.confirm == 'function')
return opts.confirm(opts);
}
}
}
});
};
this.dialog = function(opts) {
if(!opts)
opts = {};
return this.engine.dialog({
message: opts.message,
title: opts.title || "Confirmação",
closeButton: false,
buttons: opts.buttons || {
cancel: {
label: "Cancelar",
callback: function(e) {
if(opts.hasOwnProperty('cancel') && typeof opts.cancel == 'function')
return opts.cancel(opts);
}
},
ok: {
label: "OK",
className: "btn-primary",
callback: function(e) {
if(opts.hasOwnProperty('confirm') && typeof opts.confirm == 'function')
return opts.confirm(opts, e);
}
}
}
});
};
this.error = function(opts) {
if(!opts)
opts = {};
var modal = this.engine.dialog({
message: opts.message,
title: opts.title || "Confirmação",
closeButton: false,
buttons: opts.buttons || {
ok: {
label: "OK",
className: "btn-danger",
callback: function(e) {
if(opts.hasOwnProperty('confirm') && typeof opts.confirm == 'function')
return opts.confirm(opts, e);
}
}
}
});
return modal;
};
}
BootboxEngine.prototype.name = "BootboxEngine";
BootboxEngine.prototype.constructor = BootboxEngine;
function ModalEngine(engine) {
this.engine = engine;
this.alert = function(opts) {
return this.engine.alert(opts);
};
this.dialog = function(opts) {
return this.engine.dialog(opts);
};
this.error = function(opts) {
return this.engine.error(opts);
};
}
ModalEngine.prototype.name = "ModalEngine";
ModalEngine.prototype.constructor = ModalEngine;
// use this in your app
var Modal = new ModalEngine( new BootboxEngine() );
var Modal = new ModalEngine( new SweetAlertEngine() );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment