jQuery.UI Messenger\Outlook like message notification Widget

by mosessaur| 16 July 2008| 23 Comments

Today, Matt posted a cool progressbar widget using jQuery.UI. He gave a good introduction and explanation about how to build a widget using jQuery.UI.

Here, I'm following his sample to build my own one, which is Messenger\Outlook like message notification [view demo]. So for reference and how to build one please return to jQuery.UI ProgressBar Widget by Matt Berseth.

Messenger like message notification widget is something I wished to do long time ago, and was lazy to think and code it, because I thought it would take to much coding. And I read Bilal's article How to Show Messenger-Like Popups Using AJAX but it wasn't a reusable component. So I just used his styles for demonstration.

After going through Matt's post, I decided to build it up right away. So Let's dive into the code. Below is the init method for the widget, I just initialize some local variables with to store current bottom position and height of the popup message element.

init: function() {
  $.ui.notificationmsg._bottompost=this.element.css("bottom");
  $.ui.notificationmsg._height=this.element.css("height");  
}

Next I decided to build only 2 methods show & hide. show method checks if the popup element is hidden, if so it initializes the desired animation defined in the options and shows the message.

show: function(){
        var o = this.options;
        if(this.element.is(":hidden")){
            this.element.queue(function(){$.ui.notificationmsg.animations[o.animation](this, o);}).dequeue();
        }            
}

hide method does the reverse, it checks if the popup element is visible, if so it initializes the desired animation defined in the options and hides the message. But it also stops all animations; this action I made because the popup element can disappear automatically based on period option which I'm going to discuss later.

hide: function(){
        this.element.stop(true);
        var o = this.options;
        if(this.element.is(":visible")){
            this.element.queue(function(){$.ui.notificationmsg.animations[o.animation](this, o);}).dequeue();
        }
}

I'm going to laugh, this is just copy and past of Matt's post. Anyway, it works. So next is all about animation, as Matt did, I also wished to proved out of the box animations, basically 3 types, Slide, Fade and Slidethru. Also the widget support automatic hide of the popup element using period option. But this required a trick which I learned from Effect Delay Trick post by Karl on http://www.learningjquery.com/.

I'm going to take piece by piece, first the options which are declared and initialized with default values. They are 3 options, speed for animation speed, period which is the amount of time for the popup message to be visible and finally animation which define the animation algorithm to be used and it can be slide, fade or slidethru. Of course you can extend that and provide your own animations. And this is the beauty about the whole subject.

$.extend($.ui.notificationmsg, {
    defaults: {
        // provide a speed for the animation
        speed: 1000,
        // provide a period for the popup to keep showing
        period: 2000, 
        // default the animation algorithm to the basic slide
        animation:'slide'
    },
.....

Now time to explore animation methods, Nothing fancy, just if/else and copy/paste from Matt's post with few tweaks.

animations: {
 slide: function(e, options) {
     if($(e).is(":hidden")){
        
         //  animate
         $anim = $(e).animate({height: "show"}, options.speed)
            
         if(options.period && options.period > 0){
             $anim.animate({opacity: 1.0}, options.period)
                 .animate({height: "hide"}, options.speed);
         }
     }
     else{
         $(e).animate({height: "hide"}, options.speed)
     }
     
     $(e).css("height",$.ui.notificationmsg._height);
 },
 fade: function(e, options) {
     if($(e).is(":hidden")){
         //  animate
         $anim = $(e).animate({opacity: "show"}, options.speed);
         
         if(options.period && options.period > 0){
             $anim.animate({opacity: 1.0}, options.period)
                 .animate({opacity: "hide"}, options.speed);
         }
     }
     else{
         $(e).animate({opacity: "hide"}, options.speed);
     }
     
     $(e).css("opacity",1.0);
 },
 slidethru: function(e, options) {
     //  set the position and left
     var b = $.ui.notificationmsg._bottompost;
     var h = $.ui.notificationmsg._height;
     if($(e).is(":hidden")){
         //  animate
         $anim = $(e).animate({height: "show"}, options.speed);
         
         if(options.period && options.period > 0){                       
             $anim.animate({opacity: 1.0}, options.period)
               .animate({height: "hide", bottom: h}, options.speed)
               .animate({bottom: b}, 1);
         }
     }
     else{
         $(e).css({height:h,bottom:b});
         $(e).animate({height: "hide", bottom: h}, options.speed)
             .animate({bottom: b}, 1);
     }
                    
 }

That was it all about. You can download the demo or view it here to see how it works. Below is the full code of this widget.

/*
 * jQuery UI Notification Message
 *
 * Depends:
 *	    ui.core.js
 */
 
(function($) {
$.widget("ui.notificationmsg", {
    
    init: function() {
        $.ui.notificationmsg._bottompost=this.element.css("bottom");
        $.ui.notificationmsg._height=this.element.css("height");  
    },
    
    show: function(){
        var o = this.options;
        if(this.element.is(":hidden")){
            this.element.queue(function(){$.ui.notificationmsg.animations[o.animation](this, o);}).dequeue();
        }            
    },
    
    hide: function(){
        this.element.stop(true);
        var o = this.options;
        if(this.element.is(":visible")){
            this.element.queue(function(){$.ui.notificationmsg.animations[o.animation](this, o);}).dequeue();
        }
    }
});    
$.ui.notificationmsg._bottompost = "0px";
$.ui.notificationmsg._height;
$.extend($.ui.notificationmsg, {
    defaults: {
        // provide a speed for the animation
        speed: 1000,
        // provide a period for the popup to keep showing
        period: 2000, 
        // default the animation algorithm to the basic slide
        animation:'slide'
    },
    animations: {
        slide: function(e, options) {
            if($(e).is(":hidden")){
                
                //  animate
                $anim = $(e).animate({height: "show"}, options.speed)
                
                if(options.period && options.period > 0){
                    $anim.animate({opacity: 1.0}, options.period)
                        .animate({height: "hide"}, options.speed);
                }
            }
            else{
                $(e).animate({height: "hide"}, options.speed)
            }
            
            $(e).css("height",$.ui.notificationmsg._height);
        },
        fade: function(e, options) {
            if($(e).is(":hidden")){
                //  animate
                $anim = $(e).animate({opacity: "show"}, options.speed);
                
                if(options.period && options.period > 0){
                    $anim.animate({opacity: 1.0}, options.period)
                        .animate({opacity: "hide"}, options.speed);
                }
            }
            else{
                $(e).animate({opacity: "hide"}, options.speed);
            }
            
            $(e).css("opacity",1.0);
        },
        slidethru: function(e, options) {
            //  set the position and left
            var b = $.ui.notificationmsg._bottompost;
            var h = $.ui.notificationmsg._height;
            if($(e).is(":hidden")){
                //  animate
                $anim = $(e).animate({height: "show"}, options.speed);
                
                if(options.period && options.period > 0){                       
                    $anim.animate({opacity: 1.0}, options.period)
                        .animate({height: "hide", bottom: h}, options.speed)
                        .animate({bottom: b}, 1);
                }
            }
            else{
                $(e).css({height:h,bottom:b});
                $(e).animate({height: "hide", bottom: h}, options.speed)
                    .animate({bottom: b}, 1);
            }
                           
        }
    }
});
})(jQuery);

Few things might be added, such as pause method. This can be used to pause the animation so the automatic hide don't work while you hover over the the popup element. Or it can be an option to apply pause on hover or not.

kick it on DotNetKicks.com

Comments (23) -

Matt Berseth
Matt Berseth United States on 7/9/2008 6:00 AM That is one killer post and demo.  Nice work!
mosessaur
mosessaur Egypt on 7/9/2008 6:20 AM Learned from the best Matt, really your post was inspiring and easy to follow!
Really thank you for that amazing post
Ian Suttle
Ian Suttle United States on 7/9/2008 9:33 AM Well done!
redsquare
redsquare United Kingdom on 7/9/2008 2:04 PM Nice article, slight improvement if you didnt create a new jquery object all the time e.g $(e), instead at the top of every function do var $el = $(e) and then reference the jquery variable $el from then on.
mosessaur
mosessaur Egypt on 7/9/2008 4:18 PM @Ian thank you for cheering.
@redsqaure thank you for the tip, sorry, I'm using jQuery from about 4 or three months, but I don't know it real tips and tricks. Thank you again
jesús
jesús Mexico on 7/10/2008 7:49 AM great one... I'm glad to see I'm not the only one using jquery in a .net environment Laughing
mosessaur
mosessaur Egypt on 7/10/2008 8:55 AM @jesús I guess there are many many web developers using jQuery!
Janko
Janko Serbia on 7/10/2008 10:33 AM Muhammad, this is awesome!!!
mosessaur
mosessaur Egypt on 7/10/2008 10:36 AM Smile Thank you Janko
Amr Elsehemy
Amr Elsehemy Egypt on 7/13/2008 5:31 PM This is one GREAT post and demo,
keep on it.
Vishal
Vishal India on 7/24/2008 3:28 PM Hi Mosess,
         Thanks for a great and a killer post.

         Keep it up !
  
mosessaur
mosessaur Egypt on 7/24/2008 3:33 PM @Vishal Thanks for cheering Smile
ye
ye Singapore on 9/26/2008 10:25 AM amazing article
Chris Berry
Chris Berry United States on 10/4/2008 1:32 PM Have you guys ever noticed a modal popping up on a postback? It not called in a event or anything is just shows up
mosessaur
mosessaur Egypt on 10/6/2008 11:12 AM @Chris Berry I didn't notice that myself.
Diego
Diego Colombia on 10/10/2008 3:31 AM Good article men ..!! thanks for help us sharing this code, it's great !
Chris berry
Chris berry United States on 10/10/2008 3:58 AM The modal was called by JavaScript, instead of using the ajax extension. No problem now.
D K
D K Vietnam on 11/29/2008 7:40 AM I am using your plugin in a master page in our ASP.NET software. It works fine with IE7. But! I did had a hard time when the plugin somehow doesn't work properly with Firefox (Mozilla/5.0 (Windows; U; Windows NT 5.2; en-GB; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14). The background it not rendered and the close icon is not shown or is shown when the close text is clicked. So i suspected the CSS file. The line 'background-image: url(img/bg.gif)' should be 'background-image: url(./img/bg.gif)'. Now it works fine for me with IE7 and FF2.
mosessaur
mosessaur Egypt on 11/30/2008 1:53 PM @D k Thank you for the update Smile
nazmin
nazmin Malaysia on 1/25/2009 1:35 AM Bro can you show me on how to do it in php version? Bcoz i try to do it using php but it throw an error like parse error...i've change the ajax url to my correspond page...plz help
Steven
Steven Norway on 1/28/2009 4:35 AM Niche widget.
I have onw problem though. FireFox gives me a javascript error:

$.widgetis not a function
http://site/scripts/ui/jquery.ui.notification.js
Line 26


I'm jusing the widget in Drupal.
Toronto mover
Toronto mover Canada on 4/22/2009 8:16 AM I almost surfed the entire web looking for a jquery notification box to implement in our new website and I must say that this is indeed an excellent piece. I will use it to provide some flash promos to customers on our Home page. I was Just wondering if you could explain how to launch it on page load, have it stuck and maybe use a cookie so it doesn't pop again once it's closed.
prashanthganathe
prashanthganathe India on 5/28/2009 5:41 PM This is working well in a single page but as I am trying to make it as a user control and to use the usercontrol in a content page.. due to jquery clientid i guess i am not getting the alters.
from firebug getting this.

$('#msg1').notificationmsg({period: 2500});
TestUC.aspx (line 615)
$("#msg1").notificationmsg is not a function
[Break on this error] $('#msg1').notificationmsg({period: 2500});


I tried even replacing  " $('#msg1') " by $$('#msg1'), but no use.
A user control on this will be a great help so we can implement this in  all the pages and checking the messages from a timer.

Thanks.

Pingbacks and trackbacks (4)+

Comments are closed