I'm making a calendar and this is one part of the larger project. This adds a tooltip with more information when a calendar event is clicked. It then disappears after 10 seconds, or earlier if a click is detected. The exception being if the click in on the tooltip itself. This code works but feels kind of "cheaty" to me. I wanted a fresh pair of eyes to let me know if there's a better way of doing this all or a specific part I could do differently.
setUpCalendar: function() {
var self = this; //Refers to the main Calendar Object
//Render Calendar
this.elem.fullCalendar({
height: this.options.elemHeight,
events: this.entries,
eventClick: function(event, jsEvent, view) {
var left = parseInt($(this).css("left")) - 4;
var top = parseInt($(this).css("top")) - 80;
self.placeTooltip(left, top, event);
return false;
}
});
},
placeTooltip: function(left, top, event) {
var toolTip = $(".tooltip"),
date = $.fullCalendar.formatDate(event.start, "MM dd yyyy hh TT");
//Format date
date = date.replace(" ", "/");
date = date.replace(" ", "/");
toolTip
.css({
"display": "block",
"position": "absolute",
"z-index": 10,
"left": left,
"top": top
})
.html(event.title + "<br />" + date)
.on("click", function(e) {
e.stopPropagation(); //Stop click from closing tooltip
});
//Remove it later
$(document).on("click", function() {
toolTip.hide(); //Hide if clicked anywhere
});
if(this.closeTool)
clearTimeout(this.closeTool);
this.closeTool = setTimeout(function() {
toolTip.fadeOut(); //Run this timeout to hide tooltip if not clicked
}, 10000);
}
delay
function instead ofsetTimeout
: underscorejs.org/#delay \$\endgroup\$delay
handle multiple calls? Does it cancel the previous ones, or add them all to a queue of sorts? \$\endgroup\$