2
$.fn.colorTip = function(settings){
4
var defaultSettings = {
9
var supportedColors = ['organe','red','green','blue','white','yellow','black'];
11
/* Combining the default settings object with the supplied one */
12
settings = $.extend(defaultSettings,settings);
15
* Looping through all the elements and returning them afterwards.
16
* This will add chainability to the plugin.
19
return this.each(function(){
23
// If the title attribute is empty, continue with the next element
24
if(!elem.attr('title')) return true;
26
// Creating new eventScheduler and Tip objects for this element.
27
// (See the class definition at the bottom).
29
var scheduleEvent = new eventScheduler();
30
var tip = new Tip(elem.attr('title'));
32
// Adding the tooltip markup to the element and
33
// applying a special class:
35
elem.append(tip.generate()).addClass('colorTipContainer');
37
// Checking to see whether a supported color has been
38
// set as a classname on the element.
41
for(var i=0;i<supportedColors.length;i++)
43
if(elem.hasClass(supportedColors[i])){
49
// If it has been set, it will override the default color
52
elem.addClass(settings.color);
55
// On mouseenter, show the tip, on mouseleave set the
56
// tip to be hidden in half a second.
58
elem.hover(function(){
62
// If the user moves away and hovers over the tip again,
63
// clear the previously set event:
65
scheduleEvent.clear();
69
// Schedule event actualy sets a timeout (as you can
70
// see from the class definition below).
72
scheduleEvent.set(function(){
78
// Removing the title attribute, so the regular OS titles are
79
// not shown along with the tooltips.
81
elem.removeAttr('title');
88
/ Event Scheduler Class Definition
91
function eventScheduler(){}
93
eventScheduler.prototype = {
94
set : function (func,timeout){
96
// The set method takes a function and a time period (ms) as
97
// parameters, and sets a timeout
99
this.timer = setTimeout(func,timeout);
103
// The clear method clears the timeout
105
clearTimeout(this.timer);
111
/ Tip Class Definition
120
generate: function(){
122
// The generate method returns either a previously generated element
123
// stored in the tip variable, or generates it and saves it in tip for
124
// later use, after which returns it.
126
return this.tip || (this.tip = $('<span class="colorTip">'+this.content+
127
'<span class="pointyTipShadow"></span><span class="pointyTip"></span></span>'));
130
if(this.shown) return;
132
// Center the tip and start a fadeIn animation
133
this.tip.css('margin-left',-this.tip.outerWidth()/2).fadeIn('fast');