jquery.notify.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 = 20;
  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. if (_this.options.level == TYPES.ERROR){
  55. el.addClass("alert-error");
  56. el.find(".message").html("<i class='fa fa-exclamation-triangle'></i> <strong>" + _this.options.message + "</strong>");
  57. }
  58. else if (_this.options.level == TYPES.INFO){
  59. el.addClass("alert-info");
  60. el.find(".message").html("<i class='fa fa-info-circle'></i> <strong>" + _this.options.message + "</strong>");
  61. }
  62. else {
  63. el.find(".message").html(_this.options.message);
  64. }
  65. if (_this.options.css != null){
  66. el.attr("style", _this.options.css);
  67. }
  68. if (_this.options.sticky){
  69. el.find(".close").click(function(){
  70. el.fadeOut();
  71. el.nextAll(".jHueNotify").animate({
  72. top:'-=' + (el.outerHeight() + MARGIN/3)
  73. }, 200);
  74. el.remove();
  75. }).show();
  76. el.show();
  77. }
  78. else {
  79. var t = window.setTimeout(function(){
  80. el.fadeOut();
  81. el.nextAll(".jHueNotify").animate({
  82. top:'-=' + (el.outerHeight() + MARGIN/3)
  83. }, 200);
  84. el.remove();
  85. }, 3000);
  86. el.click(function(){
  87. window.clearTimeout(t);
  88. $(this).stop(true);
  89. $(this).fadeOut();
  90. $(this).nextAll(".jHueNotify").animate({
  91. top:'-=' + ($(this).outerHeight() + MARGIN/3)
  92. }, 200);
  93. });
  94. el.show();
  95. }
  96. el.appendTo($("body"));
  97. };
  98. $[pluginName] = function () {
  99. };
  100. $[pluginName].info = function (message) {
  101. new Plugin({ level: TYPES.INFO, message: message});
  102. };
  103. $[pluginName].warn = function (message) {
  104. new Plugin({ level: TYPES.GENERAL, message: message, sticky: true});
  105. };
  106. $[pluginName].error = function (message) {
  107. new Plugin({ level: TYPES.ERROR, message: message, sticky: true});
  108. };
  109. $[pluginName].notify = function (options) {
  110. new Plugin(options);
  111. };
  112. })(jQuery, window, document);