Advertisement
Guest User

Constellation notification method

a guest
Jun 11th, 2012
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Display a notification. If the page is not yet ready, delay the notification until it is ready.
  3.  * @var string message a text or html message to display
  4.  * @var object options an object with any options for the message - optional
  5.  *      - closeButton: true to add a close button to the message (default: true)
  6.  *      - autoClose: true to close message after (closeDelay) ms (default: true)
  7.  *      - closeDelay: delay before message close (default: 8000)
  8.  */
  9. var notify = function(message, options)
  10. {
  11.     var block = jQuery('#notifications');
  12.    
  13.     // If ready
  14.     if (block.length > 0)
  15.     {
  16.         var settings = jQuery.extend({}, notify.defaults, options);
  17.  
  18.         // Style
  19.         var styles = [];
  20.         if ( settings.background ) { styles.push( 'background: '+settings.background ); }
  21.         if ( settings.color ) { styles.push( 'color: '+settings.color ); }
  22.         var styleString = styles.length ? ' style="'+styles.join(';')+'"' : '';
  23.        
  24.         // Append message
  25.         var closeButton = settings.closeButton ? '<span class="close-bt"></span>' : '';
  26.         var element = jQuery('#notifications').append('<li'+styleString+'>'+message+closeButton+'</li>').children(':last-child');
  27.  
  28.         // Effect
  29.         element.expand();
  30.        
  31.         // If closing
  32.         if (settings.autoClose)
  33.         {
  34.             // Timer
  35.             var timeoutId = setTimeout(function() { element.fadeAndRemove(); }, settings.closeDelay);
  36.            
  37.             // Prevent closing when hover
  38.             element.hover(function()
  39.             {
  40.                 clearTimeout(timeoutId);
  41.                
  42.             }, function()
  43.             {
  44.                 timeoutId = setTimeout(function() { element.fadeAndRemove(); }, settings.closeDelay);
  45.             });
  46.         }
  47.     }
  48.     else
  49.     {
  50.         // Not ready, delay action
  51.         setTimeout(function() { notify(message, options); }, 40);
  52.     }
  53. };
  54.  
  55. // Defaults values for the notify method
  56. notify.defaults = {
  57.     closeButton: true,          // Add a close button to the message
  58.     autoClose: true,            // Message will close after (closeDelay) ms
  59.     closeDelay: 8000,           // Delay before message closes
  60.     background: null,           // Background for notification
  61.     color: null                 // Text color
  62. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement