views:

636

answers:

2

Any ideas how to implement tipsy tooltips over jQuery's UI Datepicker? Basically I want to get a tooltip when the user moves on a specific date in the Datepicker. The Datepicker will be displayed inline and always visible.

Thanks!

+1  A: 

That sounds pretty cool,

Here's my solution. Read the comments.

(function($){


/** 
 * Returns a dictionary, where the keys are the day of the month, 
 * and the value is the text.
 * @param year - The year of the events.
 * @param month - The month of the events.
 * @param calendarID - Events for a specific calendar.
 */
function getMonthEvents(year, month, calendarId){
  return {11: "My birthday.", 23: "My anniversary" };
}

// Receives January->1
function addTipsys(year, month, calendarId){
   var theEvents = getMonthEvents(year, month, calendarId);
   var theDateLinks = $('#' + calendarId + ' .ui-datepicker-calendar a');
   for(eventDay in theEvents){
      // Minus one, because the date in the tipies are regular dates (1-31)
      // and the links are 0-based.
      theDateLinks.eq(eventDay-1)  // select the right link
         .attr('original-title', theEvents[eventDay])  // set the text
         .tipsy();   // init the tipsy, set your properties.
   }
}

// Because the the event `onChangeMonthYear` get's called before updating 
// the items, we'll add our code after the elements get rebuilt. We will hook 
// to the `_updateDatepicker` method in the `Datepicker`.
// Saves the original function.
var _updateDatepicker_o = $.datepicker._updateDatepicker;
// Replaces the function.
$.datepicker._updateDatepicker = function(inst){ 
   // First we call the original function from the appropiate context.
   _updateDatepicker_o.apply(this, [inst]); 
   // No we can update the Tipsys.
   addTipsys(inst.drawYear, inst.drawMonth+1, inst.id);
};

// Finally the calendar initializer.
$(function(){
   // Creates the date picker, with your options.
   $("#datepicker").datepicker();
   // Gets the date and initializes the first round of tipsies.
   var currentDate = $('#datepicker').datepicker('getDate');
   // month+1 because the event considers January->1
   // Last element is null, because, it doesn't actualy get used in the hanlder.
   addTipsys(currentDate.getYear(), currentDate.getMonth()+1, 'datepicker');
});

})(jQuery);

Inconveniences:

  1. The _updateDatepicker method get's called also when the user selects a day form the visible month, or when you set a date via datepicker('setDate', theDate), which could be a little inefficient.

  2. It relies in a private function of the Datepicker, if in future versions they decide to change it's functionality, or change the name, this code will break. Although because of the nature of the function I don't see that happening soon.

NOTE: My first approach was to hook to the onChangeMonthYear event of the ui.datepicker, but because the event is triggered, before the replacing of the dates in the calendar, the addTipsys method, would add the tipsy's to the calendar dates that are about to get cleared. Therefore the need to call the addTipsys event AFTER the elements get refreshed.

EASY HACK: Hook a method to the onChangeMonthYear event of your calendar, and do a setTimeout to call the tipsy's. Some validation will need to be done.

UberNeet
Hey there, thanks! It seems you put some effort here and I appreciate it. I'm still having some issues implementing this tho.I keep getting a 'year' not defined error on `addTipsys(year, month, inst.id);`Maybe I'm doing something wrong. Can you please upload your test files somewhere so I can take a look ?Thanks again!
remedix
Ohh, yeah, my bad....inside the `$.datepicker._updateDatepicker` function change the `addTipsys(year, month, inst.id)` to `addTipsys(inst.drawYear, inst.drawMonth+1, inst.id)`, sorry I don't have test files. I worked from Firebug console.
UberNeet
Please let me know if this work for you.
UberNeet
Hey there, yeap it does work! Thanks a million!I'm needing the tipsy tooltip now to stay visible (even when the user hovers over the tooltip) until the user moves to another tooltip. I'm planning to place some links within the tooltip and hopefully I'll manage to do it with tipsy. Otherwise I'll use another jquery tooltip plugin. Thanks again for your help mate!
remedix
Great! and, you could still use this, with any other tooltip plugin, just make the changes in the `addTipsys` event. Glad I could help.
UberNeet
A: 

Hey UberNeet, Thanks a lot. It works really great. I just have a few questions.

  1. I still don't know how to give a link to dates with events. Can you please explain it?

  2. I want those dates to appear in a different color. Is it possible?

  3. and how to modify this part -

function getMonthEvents(year, month, calendarId){ return {11: "My birthday.", 23: "My anniversary" }; }

With this code, the tooltip appears on 11th and 23rd of every month. I want it to appear on a precise date. Sorry I dont know the format which should be used here. Sorry for the trouble... a newbie here... Thanks !!

Akshay Ghate