jquery.toastmessage.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Copyright 2010 akquinet
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. /**
  16. * This JQuery Plugin will help you in showing some nice Toast-Message like notification messages. The behavior is
  17. * similar to the android Toast class.
  18. * You have 4 different toast types you can show. Each type comes with its own icon and colored border. The types are:
  19. * - notice
  20. * - success
  21. * - warning
  22. * - error
  23. *
  24. * The following methods will display a toast message:
  25. *
  26. * $().toastmessage('showNoticeToast', 'some message here');
  27. * $().toastmessage('showSuccessToast', "some message here");
  28. * $().toastmessage('showWarningToast', "some message here");
  29. * $().toastmessage('showErrorToast', "some message here");
  30. *
  31. * // user configured toastmessage:
  32. * $().toastmessage('showToast', {
  33. * text : 'Hello World',
  34. * sticky : true,
  35. * position : 'top-right',
  36. * type : 'success',
  37. * close : function () {console.log("toast is closed ...");}
  38. * });
  39. *
  40. * To see some more examples please have a look into the Tests in src/test/javascript/ToastmessageTest.js
  41. *
  42. * For further style configuration please see corresponding css file: jquery-toastmessage.css
  43. *
  44. * This plugin is based on the jquery-notice (http://sandbox.timbenniks.com/projects/jquery-notice/)
  45. * but is enhanced in several ways:
  46. *
  47. * configurable positioning
  48. * convenience methods for different message types
  49. * callback functionality when closing the toast
  50. * included some nice free icons
  51. * reimplemented to follow jquery plugin good practices rules
  52. *
  53. * Author: Daniel Bremer-Tonn
  54. **/
  55. (function($)
  56. {
  57. var settings = {
  58. inEffect: {opacity: 'show'}, // in effect
  59. inEffectDuration: 600, // in effect duration in miliseconds
  60. stayTime: 3000, // time in miliseconds before the item has to disappear
  61. text: '', // content of the item. Might be a string or a jQuery object. Be aware that any jQuery object which is acting as a message will be deleted when the toast is fading away.
  62. sticky: false, // should the toast item sticky or not?
  63. type: 'notice', // notice, warning, error, success
  64. position: 'top-right', // top-left, top-center, top-right, middle-left, middle-center, middle-right ... Position of the toast container holding different toast. Position can be set only once at the very first call, changing the position after the first call does nothing
  65. closeText: '', // text which will be shown as close button, set to '' when you want to introduce an image via css
  66. close: null // callback function when the toastmessage is closed
  67. };
  68. var methods = {
  69. init : function(options)
  70. {
  71. if (options) {
  72. $.extend( settings, options );
  73. }
  74. },
  75. showToast : function(options)
  76. {
  77. var localSettings = {};
  78. $.extend(localSettings, settings, options);
  79. // declare variables
  80. var toastWrapAll, toastItemOuter, toastItemInner, toastItemClose, toastItemImage;
  81. toastWrapAll = (!$('.toast-container').length) ? $('<div></div>').addClass('toast-container').addClass('toast-position-' + localSettings.position).appendTo('body') : $('.toast-container');
  82. toastItemOuter = $('<div></div>').addClass('toast-item-wrapper');
  83. toastItemInner = $('<div></div>').hide().addClass('toast-item toast-type-' + localSettings.type).appendTo(toastWrapAll).html($('<p>').append (localSettings.text)).animate(localSettings.inEffect, localSettings.inEffectDuration).wrap(toastItemOuter);
  84. toastItemClose = $('<div></div>').addClass('toast-item-close').prependTo(toastItemInner).html(localSettings.closeText).click(function() { $().toastmessage('removeToast',toastItemInner, localSettings) });
  85. toastItemImage = $('<div></div>').addClass('toast-item-image').addClass('toast-item-image-' + localSettings.type).prependTo(toastItemInner);
  86. if(navigator.userAgent.match(/MSIE 6/i))
  87. {
  88. toastWrapAll.css({top: document.documentElement.scrollTop});
  89. }
  90. if(!localSettings.sticky)
  91. {
  92. setTimeout(function()
  93. {
  94. $().toastmessage('removeToast', toastItemInner, localSettings);
  95. },
  96. localSettings.stayTime);
  97. }
  98. return toastItemInner;
  99. },
  100. showNoticeToast : function (message)
  101. {
  102. var options = {text : message, type : 'notice'};
  103. return $().toastmessage('showToast', options);
  104. },
  105. showSuccessToast : function (message)
  106. {
  107. var options = {text : message, type : 'success'};
  108. return $().toastmessage('showToast', options);
  109. },
  110. showErrorToast : function (message)
  111. {
  112. var options = {text : message, type : 'error'};
  113. return $().toastmessage('showToast', options);
  114. },
  115. showWarningToast : function (message)
  116. {
  117. var options = {text : message, type : 'warning'};
  118. return $().toastmessage('showToast', options);
  119. },
  120. removeToast: function(obj, options)
  121. {
  122. obj.animate({opacity: '0'}, 600, function()
  123. {
  124. obj.parent().animate({height: '0px'}, 300, function()
  125. {
  126. obj.parent().remove();
  127. });
  128. });
  129. // callback
  130. if (options && options.close !== null)
  131. {
  132. options.close();
  133. }
  134. }
  135. };
  136. $.fn.toastmessage = function( method ) {
  137. // Method calling logic
  138. if ( methods[method] ) {
  139. return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
  140. } else if ( typeof method === 'object' || ! method ) {
  141. return methods.init.apply( this, arguments );
  142. } else {
  143. $.error( 'Method ' + method + ' does not exist on jQuery.toastmessage' );
  144. }
  145. };
  146. })(jQuery);