-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjQuery.pTooltip.js
More file actions
109 lines (92 loc) · 3.12 KB
/
jQuery.pTooltip.js
File metadata and controls
109 lines (92 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* Created : Thu 03 Jan 2013 05:43:15 PM EST
* Modified : Tue 08 Jan 2013 01:32:50 PM EST
* Author : GI <gi1242+js@nospam.com> (replace nospam with gmail)
*
* Copyright 2013, GI.
* Licensed under the MIT licenses:
* http://www.opensource.org/licenses/mit-license.php
*
* Persistent tool tips: Tool tips close after a delay, unless the user is
* hovering over the tool tip. In this case the tool tip will close when the
* mouse leaves the tool tip window.
*
* As of now this works with jquery-1.8.3 and jquery-ui-1.9.2
*/
(function($) {
function closeTip( tip, uid )
{
var d = tip.data('pTooltip')
// Do nothing if the calling uid and tip.uid don't match.
// Don't close if the mouse has entered the tooltip. (In
// this case the tip will be closed by a mouseleave event)
if( uid === d.uid && !d.mouseEntered )
tip.hide();
}
$.fn.pTooltip = function( args )
{
// this: jQuery object with a list of items for which the tool tip is required.
var options = {
tipCloseDelay: 500, /* ms after which we attempt to close the tip */
findToolTip: function(t) {
/*
* Should return a jQuery object containining the tool tip for
* the jQuery object t. (Default: title attribute to each
* object contains the ID to the tool tip element.)
*/
return $( '#' + t.attr('title') );
},
tipPosition: function(t) {
/*
* Returns the position at which the tip of "t" should be placed.
*/
return { my: 'center top+15', at: 'center bottom', of: t,
collision: 'fit flip' };
}
};
$.extend( options, args );
return this.each( function()
{
// this is a DOM element. ID of tooltip element is in the title attribute
var t = $(this);
var tip = options.findToolTip(t);
// Debug check
if( tip.length === 0 )
throw( 'No element with ID ' + t.attr('title') );
// Remove title attribute from element
t.removeAttr( 'title' );
// Show tooltip on mouse over.
t.mouseover( function()
{
// Hide all visible tips
$(':visible:data(pTooltip)').hide();
// Set UID to 0 here to cancel any closeTip events that
// might fire from other elements that showed the same tip.
var d = $.extend( tip.data('pTooltip'),
{ mouseEntered: false, uid: 0 } );
tip.data( 'pTooltip', d );
tip.show().position( options.tipPosition(t) );
});
// Wait a little, and then call the close function.
t.mouseleave( function()
{
// Use time (in miliseconds) as a UID
var uid = $.now();
var d = $.extend( tip.data('pTooltip'), { uid: uid} );
tip.data( 'pTooltip', d );
setTimeout( function() { closeTip( tip, uid ); },
options.tipCloseDelay );
});
// Set pTooltip.mouseEntered when the mouse enters.
tip.mouseover( function()
{
var d = $.extend( tip.data('pTooltip'), { mouseEntered: true } );
tip.data( 'pTooltip', d );
} );
// Close the tool tip when the mouse leaves
tip.mouseleave( function() { tip.hide(); } );
// Style the tip
tip.hide().addClass( 'ui-tooltip ui-widget ui-corner-all ui-widget-content' );
} );
}
})(jQuery);