Created
January 11, 2019 13:59
-
-
Save wattry/0de158db279379c7d4ba1451d06caea6 to your computer and use it in GitHub Desktop.
A simple Bootstrap alert/notification manager
This file contains hidden or 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
/* | |
* A simple notification manager to handle Bootstrap alerts. | |
*/ | |
/** | |
* | |
* @param {String} selector a jQuery element selector | |
* @param {String} alertClass success|info|danger a Bootstrap alert class. | |
* @param {String} message a message to be displayed in the bootstrap alert. | |
* @param {Boolean} append should this message should append or replace the current message. | |
* @param {Integer} timeout the number of seconds | |
*/ | |
function setNotification(selector, alertClass, message, append, timeout) { | |
try { | |
var alertDiv = $('<div>', { | |
class: "alert alert-" + alertClass + " alert-dismissable fade show" | |
}); | |
var alertButton = $('<button>', { | |
type: "button", | |
class: "close", | |
'data-dismiss': "alert", | |
'aria-hidden': true, | |
text: "×" | |
}) | |
if (append) { | |
$(selector).append($(alertDiv).append(alertButton, message)); | |
} else { | |
$(selector).html($(alertDiv).append(alertButton, message)); | |
} | |
if (alertClass === "success" || timeout) { | |
timeout = timeout ? timeout * 1000 : 10 * 1000; | |
setTimeout(function () { | |
$(alertDiv).fadeTo(1000, 0).slideUp(1000, function () { | |
$(this).remove(); | |
}); | |
}, timeout) | |
} | |
} catch (error) { | |
console.log("An error occurred while setting a notification. ", error); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment