jquery.toaster.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /***********************************************************************************
  2. * Add Array.indexOf *
  3. ***********************************************************************************/
  4. (function ()
  5. {
  6. if (typeof Array.prototype.indexOf !== 'function')
  7. {
  8. Array.prototype.indexOf = function(searchElement, fromIndex)
  9. {
  10. for (var i = (fromIndex || 0), j = this.length; i < j; i += 1)
  11. {
  12. if ((searchElement === undefined) || (searchElement === null))
  13. {
  14. if (this[i] === searchElement)
  15. {
  16. return i;
  17. }
  18. }
  19. else if (this[i] === searchElement)
  20. {
  21. return i;
  22. }
  23. }
  24. return -1;
  25. };
  26. }
  27. })();
  28. /**********************************************************************************/
  29. (function ($,undefined)
  30. {
  31. var toasting =
  32. {
  33. gettoaster : function ()
  34. {
  35. var toaster = $('#' + settings.toaster.id);
  36. if(toaster.length < 1)
  37. {
  38. toaster = $(settings.toaster.template).attr('id', settings.toaster.id).css(settings.toaster.css).addClass(settings.toaster['class']);
  39. if ((settings.stylesheet) && (!$("link[href=" + settings.stylesheet + "]").length))
  40. {
  41. $('head').appendTo('<link rel="stylesheet" href="' + settings.stylesheet + '">');
  42. }
  43. $(settings.toaster.container).append(toaster);
  44. }
  45. return toaster;
  46. },
  47. notify : function (title, message, priority)
  48. {
  49. var $toaster = this.gettoaster();
  50. var $toast = $(settings.toast.template.replace('%priority%', priority)).hide().css(settings.toast.css).addClass(settings.toast['class']);
  51. $('.title', $toast).css(settings.toast.csst).html(title);
  52. $('.message', $toast).css(settings.toast.cssm).html(message);
  53. if ((settings.debug) && (window.console))
  54. {
  55. console.log(toast);
  56. }
  57. $toaster.append(settings.toast.display($toast));
  58. if (settings.donotdismiss.indexOf(priority) === -1)
  59. {
  60. var timeout = (typeof settings.timeout === 'number') ? settings.timeout : ((typeof settings.timeout === 'object') && (priority in settings.timeout)) ? settings.timeout[priority] : 1500;
  61. setTimeout(function()
  62. {
  63. settings.toast.remove($toast, function()
  64. {
  65. $toast.remove();
  66. });
  67. }, timeout);
  68. }
  69. }
  70. };
  71. var defaults =
  72. {
  73. 'toaster' :
  74. {
  75. 'id' : 'toaster',
  76. 'container' : 'body',
  77. 'template' : '<div></div>',
  78. 'class' : 'toaster',
  79. 'css' :
  80. {
  81. 'position' : 'fixed',
  82. 'top' : '10px',
  83. 'right' : '10px',
  84. 'width' : '400px',
  85. 'zIndex' : 50000
  86. }
  87. },
  88. 'toast' :
  89. {
  90. 'template' :
  91. '<div class="alert alert-%priority% alert-dismissible" role="alert">' +
  92. '<button type="button" class="close" data-dismiss="alert">' +
  93. '<span aria-hidden="true">&times;</span>' +
  94. '<span class="sr-only">Close</span>' +
  95. '</button>' +
  96. '<span class="title"></span>: <span class="message"></span>' +
  97. '</div>',
  98. 'defaults' :
  99. {
  100. 'title' : 'Notice',
  101. 'priority' : 'success'
  102. },
  103. 'css' : {},
  104. 'cssm' : {},
  105. 'csst' : { 'fontWeight' : 'bold' },
  106. 'fade' : 'slow',
  107. 'display' : function ($toast)
  108. {
  109. return $toast.fadeIn(settings.toast.fade);
  110. },
  111. 'remove' : function ($toast, callback)
  112. {
  113. return $toast.animate(
  114. {
  115. opacity : '0',
  116. padding : '0px',
  117. margin : '0px',
  118. height : '0px'
  119. },
  120. {
  121. duration : settings.toast.fade,
  122. complete : callback
  123. }
  124. );
  125. }
  126. },
  127. 'debug' : false,
  128. 'timeout' : 1500,
  129. 'stylesheet' : null,
  130. 'donotdismiss' : []
  131. };
  132. var settings = {};
  133. $.extend(settings, defaults);
  134. $.toaster = function (options)
  135. {
  136. if (typeof options === 'object')
  137. {
  138. if ('settings' in options)
  139. {
  140. settings = $.extend(true, settings, options.settings);
  141. }
  142. }
  143. else
  144. {
  145. var values = Array.prototype.slice.call(arguments, 0);
  146. var labels = ['message', 'title', 'priority'];
  147. options = {};
  148. for (var i = 0, l = values.length; i < l; i += 1)
  149. {
  150. options[labels[i]] = values[i];
  151. }
  152. }
  153. var title = (('title' in options) && (typeof options.title === 'string')) ? options.title : settings.toast.defaults.title;
  154. var message = ('message' in options) ? options.message : null;
  155. var priority = (('priority' in options) && (typeof options.priority === 'string')) ? options.priority : settings.toast.defaults.priority;
  156. if (message !== null)
  157. {
  158. toasting.notify(title, message, priority);
  159. }
  160. };
  161. $.toaster.reset = function ()
  162. {
  163. settings = {};
  164. $.extend(settings, defaults);
  165. };
  166. })(jQuery);