Skip to content
This repository has been archived by the owner on Jun 14, 2020. It is now read-only.

Commit

Permalink
Implemented some $.data optimizations and caching
Browse files Browse the repository at this point in the history
  • Loading branch information
Craga89 committed Jan 19, 2011
1 parent b31eec8 commit 6d38e12
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 106 deletions.
2 changes: 1 addition & 1 deletion dist/jquery.qtip.css
Expand Up @@ -9,7 +9,7 @@
* http://en.wikipedia.org/wiki/MIT_License
* http://en.wikipedia.org/wiki/GNU_General_Public_License
*
* Date: Tue Jan 18 16:29:50 2011 +0000
* Date: Tue Jan 18 17:28:31 2011 +0000
*/

/* Fluid class for determining actual width in IE */
Expand Down
48 changes: 26 additions & 22 deletions dist/jquery.qtip.js
Expand Up @@ -9,7 +9,7 @@
* http://en.wikipedia.org/wiki/MIT_License
* http://en.wikipedia.org/wiki/GNU_General_Public_License
*
* Date: Tue Jan 18 16:29:50 2011 +0000
* Date: Tue Jan 18 17:28:31 2011 +0000
*/

"use strict"; // Enable ECMAScript "strict" operation for this function. See more: http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/
Expand Down Expand Up @@ -368,7 +368,7 @@ function QTip(target, options, id)
// Insert into 'fx' queue our image dimension checker which will halt the showing of the tooltip until image dimensions can be detected
tooltip.queue('fx', function(next) {
// Find all content images without dimensions
var images = $('img:not([height]):not([width])', elem);
var images = elem.find('img:not([height]):not([width])');

// Update tooltip width and position when all images are loaded
function imageLoad(img) {
Expand Down Expand Up @@ -425,6 +425,7 @@ function QTip(target, options, id)
container: posOptions.container[0] === docBody ? document : posOptions.container
},
events = { show: String(options.show.event).split(' '), hide: String(options.hide.event).split(' ') },
$doc = $(document),
IE6 = $.browser.msie && parseInt($.browser.version, 10) === 6;

// Define show event method
Expand Down Expand Up @@ -453,8 +454,9 @@ function QTip(target, options, id)
if(tooltip.hasClass(disabled)) { return FALSE; }

// Check if new target was actually the tooltip element
var ontoTooltip = $(event.relatedTarget || event.target).parents(selector)[0] === tooltip[0],
ontoTarget = $(event.relatedTarget || event.target)[0] === targets.show[0];
var relatedTarget = $(event.relatedTarget || event.target),
ontoTooltip = relatedTarget.parents(selector)[0] === tooltip[0],
ontoTarget = relatedTarget[0] === targets.show[0];

// Clear timers and stop animation queue
clearTimeout(self.timers.show);
Expand Down Expand Up @@ -534,10 +536,11 @@ function QTip(target, options, id)

// Apply hide events
$.each(events.hide, function(index, type) {
var showIndex = $.inArray(type, events.show);
var showIndex = $.inArray(type, events.show),
targetHide = $(targets.hide);

// Both events and targets are identical, apply events using a toggle
if((showIndex > -1 && $(targets.hide).add(targets.show).length === $(targets.hide).length) || type === 'unfocus')
if((showIndex > -1 && targetHide.add(targets.show).length === targetHide.length) || type === 'unfocus')
{
targets.show.bind(type+namespace, function(event)
{
Expand Down Expand Up @@ -575,17 +578,18 @@ function QTip(target, options, id)

// Hide tooltip on document mousedown if unfocus events are enabled
if((/unfocus/i).test(options.hide.event)) {
$(document).bind('mousedown'+namespace, function(event) {
if($(event.target).parents(selector).length === 0 && $(event.target).add(target).length > 1 &&
isVisible() && !tooltip.hasClass(disabled)) {
$doc.bind('mousedown'+namespace, function(event) {
var $target = $(event.target);

if($target.parents(selector).length === 0 && $target.add(target).length > 1 && isVisible() && !tooltip.hasClass(disabled)) {
self.hide(event);
}
});
}

// If mouse is the target, update tooltip position on document mousemove
if(posOptions.target === 'mouse') {
$(document).bind('mousemove'+namespace, function(event) {
$doc.bind('mousemove'+namespace, function(event) {
// Update the tooltip position only if the tooltip is visible and adjustment is enabled
if(posOptions.adjust.mouse && !tooltip.hasClass(disabled) && isVisible()) {
self.reposition(event || $.fn.qtip.mouse);
Expand Down Expand Up @@ -972,7 +976,7 @@ function QTip(target, options, id)
});

// Fire blur event for focused tooltip
$(selector + '.' + focusClass).qtip('blur', cachedEvent);
qtips.filter('.' + focusClass).qtip('blur', cachedEvent);
}

// Store currently focused element
Expand Down Expand Up @@ -1232,7 +1236,7 @@ function QTip(target, options, id)

destroy: function()
{
var oldtitle = target.data('oldtitle');
var oldtitle = $.data(target[0], 'oldtitle');

// Destroy tooltip and any associated plugins if rendered
if(self.rendered) {
Expand Down Expand Up @@ -1315,7 +1319,7 @@ function init(id, opts)
posOptions.my = new $.fn.qtip.plugins.Corner(posOptions.my);

// Destroy previous tooltip if overwrite is enabled, or skip element if not
if(elem.data('qtip')) {
if($.data(this, 'qtip')) {
if(config.overwrite) {
elem.qtip('destroy');
}
Expand All @@ -1326,12 +1330,13 @@ function init(id, opts)

// Remove title attribute and store it if present
if(elem.attr('title')) {
elem.data('oldtitle', elem.attr('title')).removeAttr('title');
$.data(this, 'oldtitle', elem.attr('title'));
elem.removeAttr('title');
}

// Initialize the tooltip and add API reference
obj = new QTip(elem, config, id);
elem.data('qtip', obj);
$.data(this, 'qtip', obj);

// Catch remove events on target element to destroy redundant tooltip
elem.bind('remove.qtip', function(){ obj.destroy(); });
Expand All @@ -1346,20 +1351,19 @@ $.fn.qtip = function(options, notation, newValue)
returned = NULL,
args = command === 'disable' ? [TRUE] : $.makeArray(arguments).slice(1, 10),
event = args[args.length - 1],
opts;
opts = this[0] ? $.data(this[0], 'qtip') : NULL;

// Check for API request
if((!arguments.length && this.data('qtip')) || command === 'api') {
opts = this.data('qtip');
return opts ? opts : undefined;
if((!arguments.length && opts) || command === 'api') {
return opts;
}

// Execute API command if present
else if('string' === typeof options)
{
this.each(function()
{
var api = $(this).data('qtip');
var api = $.data(this, 'qtip');
if(!api) { return TRUE; }

// Call APIcommand
Expand Down Expand Up @@ -1472,8 +1476,8 @@ $.fn.qtip.bind = function(opts, event)
$.each({
/* Allow other plugins to successfully retrieve the title of an element with a qTip applied */
attr: function(attr) {
var self = $(this), api = self.data('qtip');
return (arguments.length === 1 && attr === 'title' && api && api.rendered === TRUE) ? self.data('oldtitle') : NULL;
var api = $.data(this, 'qtip');
return (arguments.length === 1 && attr === 'title' && api && api.rendered === TRUE) ? $.data(this, 'oldtitle') : NULL;
},

/*
Expand Down

0 comments on commit 6d38e12

Please sign in to comment.