jquery.bootstrap-growl.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. (function() {
  2. var $;
  3. $ = jQuery;
  4. $.bootstrapGrowl = function(message, options) {
  5. var $alert, css, offsetAmount;
  6. options = $.extend({}, $.bootstrapGrowl.default_options, options);
  7. $alert = $("<div>");
  8. $alert.attr("class", "bootstrap-growl alert");
  9. if (options.type) {
  10. $alert.addClass("alert-" + options.type);
  11. }
  12. if (options.allow_dismiss) {
  13. $alert.addClass("alert-dismissible");
  14. $alert.append("<button class=\"close\" data-dismiss=\"alert\" type=\"button\"><span aria-hidden=\"true\">&times;</span><span class=\"sr-only\">Close</span></button>");
  15. }
  16. $alert.append(message);
  17. if (options.top_offset) {
  18. options.offset = {
  19. from: "top",
  20. amount: options.top_offset
  21. };
  22. }
  23. offsetAmount = options.offset.amount;
  24. $(".bootstrap-growl").each(function() {
  25. return offsetAmount = Math.max(offsetAmount, parseInt($(this).css(options.offset.from)) + $(this).outerHeight() + options.stackup_spacing);
  26. });
  27. css = {
  28. "position": (options.ele === "body" ? "fixed" : "absolute"),
  29. "margin": 0,
  30. "z-index": "9999",
  31. "display": "none"
  32. };
  33. css[options.offset.from] = offsetAmount + "px";
  34. $alert.css(css);
  35. if (options.width !== "auto") {
  36. $alert.css("width", options.width + "px");
  37. }
  38. $(options.ele).append($alert);
  39. switch (options.align) {
  40. case "center":
  41. $alert.css({
  42. "left": "50%",
  43. "margin-left": "-" + ($alert.outerWidth() / 2) + "px"
  44. });
  45. break;
  46. case "left":
  47. $alert.css("left", "20px");
  48. break;
  49. default:
  50. $alert.css("right", "20px");
  51. }
  52. $alert.fadeIn();
  53. if (options.delay > 0) {
  54. $alert.delay(options.delay).fadeOut(function() {
  55. return $(this).alert("close");
  56. });
  57. }
  58. return $alert;
  59. };
  60. $.bootstrapGrowl.default_options = {
  61. ele: "body",
  62. type: "info",
  63. offset: {
  64. from: "top",
  65. amount: 20
  66. },
  67. align: "right",
  68. width: 250,
  69. delay: 4000,
  70. allow_dismiss: true,
  71. stackup_spacing: 10
  72. };
  73. }).call(this);