From df4c79cb14c6e1169a20a86844bfdb8bb1856cc4 Mon Sep 17 00:00:00 2001 From: Craig Michael Thompson Date: Thu, 15 Aug 2013 10:52:12 +0100 Subject: [PATCH] Add common cloneEvent method to reduce code. Fix destroy issues. --- src/core/events.js | 24 +++++++++++++++--------- src/core/jquery_methods.js | 8 ++------ src/core/toggle.js | 4 ++-- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/core/events.js b/src/core/events.js index 963f9201..9c293ee6 100644 --- a/src/core/events.js +++ b/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) { @@ -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 @@ -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 diff --git a/src/core/jquery_methods.js b/src/core/jquery_methods.js index 8afacc05..25f7f48a 100644 --- a/src/core/jquery_methods.js +++ b/src/core/jquery_methods.js @@ -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; @@ -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() { diff --git a/src/core/toggle.js b/src/core/toggle.js index 68ca6644..28341a34 100644 --- a/src/core/toggle.js +++ b/src/core/toggle.js @@ -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 @@ -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); }