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

Commit

Permalink
Add common cloneEvent method to reduce code. Fix destroy issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
Craga89 committed Aug 15, 2013
1 parent c2315b3 commit df4c79c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
24 changes: 15 additions & 9 deletions src/core/events.js
@@ -1,3 +1,15 @@
function cloneEvent(event) {
return event && {
type: event.type,
pageX: event.pageX,
pageY: event.pageY,
target: event.target,
relatedTarget: event.relatedTarget,
scrollX: event.scrollX || window.pageXOffset || document.body.scrollLeft || document.documentElement.scrollLeft,
scrollY: event.scrollY || window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop
} || {};
}

function delay(callback, duration) {
// If tooltip has displayed, start hide timer
if(duration > 0) {
Expand Down Expand Up @@ -76,13 +88,7 @@ function repositionMethod(event) {

// Store mouse coordinates
PROTOTYPE._storeMouse = function(event) {
this.mouse = {
pageX: event.pageX,
pageY: event.pageY,
type: 'mousemove',
scrollX: window.pageXOffset || document.body.scrollLeft || document.documentElement.scrollLeft,
scrollY: window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop
};
(this.mouse = cloneEvent(event)).type = 'mousemove';
};

// Bind events
Expand Down Expand Up @@ -182,10 +188,10 @@ PROTOTYPE._assignInitialEvents = function(event) {
// Define hoverIntent function
function hoverIntent(event) {
// Only continue if tooltip isn't disabled
if(this.disabled) { return FALSE; }
if(this.disabled || this.destroyed) { return FALSE; }

// Cache the event data
this.cache.event = $.extend({}, event);
this.cache.event = cloneEvent(event);
this.cache.target = event ? $(event.target) : [undefined];

// Start the event sequence
Expand Down
8 changes: 2 additions & 6 deletions src/core/jquery_methods.js
Expand Up @@ -80,7 +80,7 @@ function init(elem, id, opts) {

// Catch remove/removeqtip events on target element to destroy redundant tooltip
elem.one('remove.qtip-'+id+' removeqtip.qtip-'+id, function() {
var api; if((api = $(this).data(NAMESPACE))) { api.destroy(); }
var api; if((api = $(this).data(NAMESPACE))) { api.destroy(true); }
});

return obj;
Expand Down Expand Up @@ -135,20 +135,16 @@ QTIP = $.fn.qtip = function(options, notation, newValue)
opts = sanitizeOptions($.extend(TRUE, {}, options));

return this.each(function(i) {
var options, targets, events, namespace, api, id;
var api, id;

// Find next available ID, or use custom ID if provided
id = $.isArray(opts.id) ? opts.id[i] : opts.id;
id = !id || id === FALSE || id.length < 1 || QTIP.api[id] ? QTIP.nextid++ : id;

// Setup events namespace
namespace = '.qtip-'+id+'-create';

// Initialize the qTip and re-grab newly sanitized options
api = init($(this), id, opts);
if(api === FALSE) { return TRUE; }
else { QTIP.api[id] = api; }
options = api.options;

// Initialize plugins
$.each(PLUGINS, function() {
Expand Down
4 changes: 2 additions & 2 deletions src/core/toggle.js
Expand Up @@ -13,7 +13,7 @@ PROTOTYPE.toggle = function(state, event) {
}

// Cache event
cache.event = $.extend({}, event);
cache.event = cloneEvent(event);
}

// If we're currently waiting and we've just hidden... stop it
Expand Down Expand Up @@ -55,7 +55,7 @@ PROTOTYPE.toggle = function(state, event) {
// Execute state specific properties
if(state) {
// Store show origin coordinates
cache.origin = $.extend({}, this.mouse);
cache.origin = cloneEvent(this.mouse);

// Update tooltip content & title if it's a dynamic function
if($.isFunction(contentOptions.text)) { this._updateContent(contentOptions.text, FALSE); }
Expand Down

0 comments on commit df4c79c

Please sign in to comment.