jquery.notify.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. ;(function ($, window, document, undefined) {
  21. var pluginName = "jHueNotify",
  22. TYPES = {
  23. INFO: "INFO",
  24. ERROR: "ERROR",
  25. GENERAL: "GENERAL"
  26. },
  27. defaults = {
  28. level: TYPES.GENERAL,
  29. message: "",
  30. sticky: false,
  31. css: null
  32. };
  33. function Plugin(options) {
  34. this.options = $.extend({}, defaults, options) ;
  35. this._defaults = defaults;
  36. this._name = pluginName;
  37. this.show();
  38. }
  39. Plugin.prototype.setOptions = function(options) {
  40. this.options = $.extend({}, defaults, options) ;
  41. };
  42. Plugin.prototype.show = function () {
  43. var _this = this;
  44. var MARGIN = 10;
  45. var el = $("#jHueNotify").clone();
  46. el.removeAttr("id");
  47. // stops all the current animations and resets the style
  48. el.stop(true);
  49. el.attr("class", "alert jHueNotify");
  50. el.find(".close").hide();
  51. if ($(".jHueNotify").last().position() != null) {
  52. el.css("top", $(".jHueNotify").last().position().top + $(".jHueNotify").last().outerHeight() + MARGIN - $(window).scrollTop());
  53. }
  54. _this.options.message = $("<span>").html(_this.options.message).text();
  55. if (_this.options.level == TYPES.ERROR){
  56. el.addClass("alert-error");
  57. el.find(".message").html("<i class='fa fa-exclamation-triangle'></i> <strong>" + _this.options.message + "</strong>");
  58. }
  59. else if (_this.options.level == TYPES.INFO){
  60. el.addClass("alert-info");
  61. el.find(".message").html("<i class='fa fa-info-circle'></i> <strong>" + _this.options.message + "</strong>");
  62. }
  63. else {
  64. el.find(".message").html(_this.options.message);
  65. }
  66. if (_this.options.css != null){
  67. el.attr("style", _this.options.css);
  68. }
  69. if (_this.options.sticky){
  70. el.find(".close").click(function(){
  71. el.fadeOut();
  72. el.nextAll(".jHueNotify").animate({
  73. top:'-=' + (el.outerHeight() + MARGIN)
  74. }, 200);
  75. el.remove();
  76. }).show();
  77. el.show();
  78. }
  79. else {
  80. var t = window.setTimeout(function(){
  81. el.fadeOut();
  82. el.nextAll(".jHueNotify").animate({
  83. top:'-=' + (el.outerHeight() + MARGIN)
  84. }, 200);
  85. el.remove();
  86. }, 3000);
  87. el.click(function(){
  88. window.clearTimeout(t);
  89. $(this).stop(true);
  90. $(this).fadeOut();
  91. $(this).nextAll(".jHueNotify").animate({
  92. top:'-=' + ($(this).outerHeight() + MARGIN)
  93. }, 200);
  94. });
  95. el.show();
  96. }
  97. el.appendTo($("body"));
  98. };
  99. $[pluginName] = function () {
  100. };
  101. $[pluginName].info = function (message) {
  102. new Plugin({ level: TYPES.INFO, message: message});
  103. };
  104. $[pluginName].warn = function (message) {
  105. new Plugin({ level: TYPES.GENERAL, message: message, sticky: true});
  106. };
  107. $[pluginName].error = function (message) {
  108. new Plugin({ level: TYPES.ERROR, message: message, sticky: true});
  109. };
  110. $[pluginName].notify = function (options) {
  111. new Plugin(options);
  112. };
  113. })(jQuery, window, document);