jquery.notify.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* jQuery Notify UI Widget 1.5 by Eric Hynds
  2. * http://www.erichynds.com/jquery/a-jquery-ui-growl-ubuntu-notification-widget/
  3. *
  4. * Depends:
  5. * - jQuery 1.4+
  6. * - jQuery UI 1.8 widget factory
  7. *
  8. * Dual licensed under the MIT and GPL licenses:
  9. * http://www.opensource.org/licenses/mit-license.php
  10. * http://www.gnu.org/licenses/gpl.html
  11. */
  12. (function($){
  13. $.widget("ech.notify", {
  14. options: {
  15. speed: 500,
  16. expires: 5000,
  17. stack: "below",
  18. custom: false,
  19. queue: false
  20. },
  21. _create: function(){
  22. var self = this;
  23. this.templates = {};
  24. this.keys = [];
  25. // build and save templates
  26. this.element.addClass("ui-notify").children().addClass("ui-notify-message ui-notify-message-style").each(function(i){
  27. var key = this.id || i;
  28. self.keys.push(key);
  29. self.templates[key] = $(this).removeAttr("id").wrap("<div></div>").parent().html(); // because $(this).andSelf().html() no workie
  30. }).end().empty().show();
  31. },
  32. create: function(template, msg, opts){
  33. if(typeof template === "object"){
  34. opts = msg;
  35. msg = template;
  36. template = null;
  37. }
  38. var tpl = this.templates[ template || this.keys[0]];
  39. // remove default styling class if rolling w/ custom classes
  40. if(opts && opts.custom){
  41. tpl = $(tpl).removeClass("ui-notify-message-style").wrap("<div></div>").parent().html();
  42. }
  43. this.openNotifications = this.openNotifications || 0;
  44. // return a new notification instance
  45. return new $.ech.notify.instance(this)._create(msg, $.extend({}, this.options, opts), tpl);
  46. }
  47. });
  48. // instance constructor
  49. $.extend($.ech.notify, {
  50. instance: function(widget){
  51. this.__super = widget;
  52. this.isOpen = false;
  53. }
  54. });
  55. // instance methods
  56. $.extend($.ech.notify.instance.prototype, {
  57. _create: function(params, options, template){
  58. this.options = options;
  59. var self = this,
  60. // build html template
  61. html = template.replace(/#(?:\{|%7B)(.*?)(?:\}|%7D)/g, function($1, $2){
  62. return ($2 in params) ? params[$2] : '';
  63. }),
  64. // the actual message
  65. m = (this.element = $(html)),
  66. // close link
  67. closelink = m.find(".ui-notify-close");
  68. // clickable?
  69. if(typeof this.options.click === "function"){
  70. m.addClass("ui-notify-click").bind("click", function(e){
  71. self._trigger("click", e, self);
  72. });
  73. }
  74. // show close link?
  75. if(closelink.length){
  76. closelink.bind("click", function(){
  77. self.close();
  78. return false;
  79. });
  80. }
  81. this.__super.element.queue("notify", function(){
  82. self.open();
  83. // auto expire?
  84. if(typeof options.expires === "number" && options.expires > 0){
  85. setTimeout($.proxy(self.close, self), options.expires);
  86. }
  87. });
  88. if(!this.options.queue || this.__super.openNotifications <= this.options.queue - 1) {
  89. this.__super.element.dequeue("notify");
  90. }
  91. return this;
  92. },
  93. close: function(){
  94. var speed = this.options.speed;
  95. this.element.fadeTo(speed, 0).slideUp(speed, $.proxy(function(){
  96. this._trigger("close");
  97. this.isOpen = false;
  98. this.element.remove();
  99. this.__super.openNotifications -= 1;
  100. this.__super.element.dequeue("notify");
  101. }, this));
  102. return this;
  103. },
  104. open: function(){
  105. if(this.isOpen || this._trigger("beforeopen") === false){
  106. return this;
  107. }
  108. var self = this;
  109. this.__super.openNotifications += 1;
  110. this.element[this.options.stack === "above" ? "prependTo" : "appendTo"](this.__super.element).css({ display:"none", opacity:"" }).fadeIn(this.options.speed, function(){
  111. self._trigger("open");
  112. self.isOpen = true;
  113. });
  114. return this;
  115. },
  116. widget: function(){
  117. return this.element;
  118. },
  119. _trigger: function(type, e, instance){
  120. return this.__super._trigger.call( this, type, e, instance );
  121. }
  122. });
  123. })(jQuery);