jquery.notify.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Licensed to Cloudera, Inc. under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. Cloudera, Inc. licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. /*
  17. * jHue notify plugin
  18. *
  19. */
  20. ;
  21. (function ($, window, document, undefined) {
  22. var pluginName = "jHueNotify",
  23. TYPES = {
  24. INFO: "INFO",
  25. ERROR: "ERROR",
  26. GENERAL: "GENERAL"
  27. },
  28. defaults = {
  29. level: TYPES.GENERAL,
  30. message: "",
  31. sticky: false,
  32. css: null
  33. };
  34. function Plugin(options) {
  35. this.options = $.extend({}, defaults, options);
  36. this._defaults = defaults;
  37. this._name = pluginName;
  38. this.show();
  39. }
  40. Plugin.prototype.setOptions = function (options) {
  41. this.options = $.extend({}, defaults, options);
  42. };
  43. Plugin.prototype.show = function () {
  44. var _this = this;
  45. var MARGIN = 10;
  46. if (_this.options.level == TYPES.ERROR && $(".jHueNotify.alert-error").length > 0) {
  47. $(".jHueNotify.alert-error").find(".message").html("<i class='fa fa-exclamation-triangle'></i> <strong>" + _this.options.message + "</strong>");
  48. }
  49. else {
  50. var el = $("#jHueNotify").clone();
  51. el.removeAttr("id");
  52. // stops all the current animations and resets the style
  53. el.stop(true);
  54. el.attr("class", "alert jHueNotify");
  55. el.find(".close").hide();
  56. if ($(".jHueNotify").last().position() != null) {
  57. el.css("top", $(".jHueNotify").last().position().top + $(".jHueNotify").last().outerHeight() + MARGIN - $(window).scrollTop());
  58. }
  59. _this.options.message = $("<span>").html(_this.options.message).text();
  60. if (_this.options.level == TYPES.ERROR) {
  61. el.addClass("alert-error");
  62. el.find(".message").html("<i class='fa fa-exclamation-triangle'></i> <strong>" + _this.options.message + "</strong>");
  63. }
  64. else if (_this.options.level == TYPES.INFO) {
  65. el.addClass("alert-info");
  66. el.find(".message").html("<i class='fa fa-info-circle'></i> <strong>" + _this.options.message + "</strong>");
  67. }
  68. else {
  69. el.find(".message").html(_this.options.message);
  70. }
  71. if (_this.options.css != null) {
  72. el.attr("style", _this.options.css);
  73. }
  74. if (_this.options.sticky) {
  75. el.find(".close").click(function () {
  76. el.fadeOut();
  77. el.nextAll(".jHueNotify").animate({
  78. top: '-=' + (el.outerHeight() + MARGIN)
  79. }, 200);
  80. el.remove();
  81. }).show();
  82. el.show();
  83. }
  84. else {
  85. var t = window.setTimeout(function () {
  86. el.fadeOut();
  87. el.nextAll(".jHueNotify").animate({
  88. top: '-=' + (el.outerHeight() + MARGIN)
  89. }, 200);
  90. el.remove();
  91. }, 3000);
  92. el.click(function () {
  93. window.clearTimeout(t);
  94. $(this).stop(true);
  95. $(this).fadeOut();
  96. $(this).nextAll(".jHueNotify").animate({
  97. top: '-=' + ($(this).outerHeight() + MARGIN)
  98. }, 200);
  99. });
  100. el.show();
  101. }
  102. el.appendTo($("body"));
  103. }
  104. };
  105. $[pluginName] = function () {
  106. };
  107. $[pluginName].info = function (message) {
  108. new Plugin({ level: TYPES.INFO, message: message});
  109. };
  110. $[pluginName].warn = function (message) {
  111. new Plugin({ level: TYPES.GENERAL, message: message, sticky: true});
  112. };
  113. $[pluginName].error = function (message) {
  114. new Plugin({ level: TYPES.ERROR, message: message, sticky: true});
  115. };
  116. $[pluginName].notify = function (options) {
  117. new Plugin(options);
  118. };
  119. })(jQuery, window, document);