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

Commit

Permalink
Add common _bindEvents method to ensure initial event bindings functi…
Browse files Browse the repository at this point in the history
…on identically to subsequent calls. Fixes #596
  • Loading branch information
Craga89 committed Sep 16, 2013
1 parent 53e0ea6 commit 06d9d2d
Showing 1 changed file with 39 additions and 27 deletions.
66 changes: 39 additions & 27 deletions src/core/events.js
Expand Up @@ -160,6 +160,37 @@ PROTOTYPE._trigger = function(type, args, event) {
return !callback.isDefaultPrevented();
};

PROTOTYPE._bindEvents = function(showEvents, hideEvents, showTarget, hideTarget, showMethod, hideMethod) {
// If hide and show targets are the same...
if(hideTarget.add(showTarget).length === hideTarget.length) {
var toggleEvents = [];

// Filter identical show/hide events
hideEvents = $.map(hideEvents, function(type) {
var showIndex = $.inArray(type, showEvents);

// Both events are identical, remove from both hide and show events
// and append to toggleEvents
if(showIndex > -1) {
toggleEvents.push( showEvents.splice( showIndex, 1 )[0] );
return;
}

return type;
});

// Toggle events are special case of identical show/hide events, which happen in sequence
toggleEvents.length && this._bind(showTarget, toggleEvents, function(event) {
var state = this.rendered ? this.tooltip[0].offsetWidth > 0 : false;
(state ? hideMethod : showMethod).call(this, event);
});
}

// Apply show/hide/toggle events
this._bind(showTarget, showEvents, showMethod);
this._bind(hideTarget, hideEvents, hideMethod);
};

PROTOTYPE._assignInitialEvents = function(event) {
var options = this.options,
showTarget = options.show.target,
Expand Down Expand Up @@ -202,17 +233,15 @@ PROTOTYPE._assignInitialEvents = function(event) {
);
}

// Bind events to target
this._bind(showTarget, showEvents, hoverIntent);
if(options.show.event !== options.hide.event) {
this._bind(hideTarget, hideEvents, function() { clearTimeout(this.timers.show); });
}
// Filter and bind events
this._bindEvents(showEvents, hideEvents, showTarget, hideTarget, hoverIntent, function() {
clearTimeout(this.timers.show);
});

// Prerendering is enabled, create tooltip now
if(options.show.ready || options.prerender) { hoverIntent.call(this, event); }
};


// Event assignment method
PROTOTYPE._assignEvents = function() {
var self = this,
Expand All @@ -229,8 +258,8 @@ PROTOTYPE._assignEvents = function() {
windowTarget = $(window),

showEvents = options.show.event ? $.trim('' + options.show.event).split(' ') : [],
hideEvents = options.hide.event ? $.trim('' + options.hide.event).split(' ') : [],
toggleEvents = [];
hideEvents = options.hide.event ? $.trim('' + options.hide.event).split(' ') : [];


// Assign passed event callbacks
$.each(options.events, function(name, callback) {
Expand Down Expand Up @@ -285,25 +314,8 @@ PROTOTYPE._assignEvents = function() {
this._bind(hideTarget.add(tooltip), QTIP.inactiveEvents, inactiveMethod, '-inactive');
}

// Apply hide events (and filter identical show events)
hideEvents = $.map(hideEvents, function(type) {
var showIndex = $.inArray(type, showEvents);

// Both events and targets are identical, apply events using a toggle
if((showIndex > -1 && hideTarget.add(showTarget).length === hideTarget.length)) {
toggleEvents.push( showEvents.splice( showIndex, 1 )[0] ); return;
}

return type;
});

// Apply show/hide/toggle events
this._bind(showTarget, showEvents, showMethod);
this._bind(hideTarget, hideEvents, hideMethod);
this._bind(showTarget, toggleEvents, function(event) {
(this.tooltip[0].offsetWidth > 0 ? hideMethod : showMethod).call(this, event);
});

// Filter and bind events
this._bindEvents(showEvents, hideEvents, showTarget, hideTarget, showMethod, hideMethod);

// Mouse movement bindings
this._bind(showTarget.add(tooltip), 'mousemove', function(event) {
Expand Down

0 comments on commit 06d9d2d

Please sign in to comment.