Skip to content

Added openDelay option #141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/core.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var session = {
previousY: 0,
desyncTimeout: null,
closeDelayTimeout: null,
openDelayTimeout: null,
mouseTrackingActive: false,
delayInProgress: false,
windowWidth: 0,
Expand Down Expand Up @@ -134,7 +135,7 @@ $.fn.powerTip = function(opts, arg) {
});
} else {
targetElements.on(evt + EVENT_NAMESPACE, function elementOpen(event) {
$.powerTip.show(this, event);
$.powerTip.show(this, event, false);
});
}
});
Expand Down Expand Up @@ -176,6 +177,7 @@ $.fn.powerTip.defaults = {
intentSensitivity: 7,
intentPollInterval: 100,
closeDelay: 100,
openDelay: 100,
placement: 'n',
smartPlacement: false,
offset: 10,
Expand Down Expand Up @@ -215,18 +217,19 @@ $.powerTip = {
* @param {jQuery|Element} element The element to open the tooltip for.
* @param {jQuery.Event=} event jQuery event for hover intent and mouse
* tracking (optional).
* @param {Boolean=} disableDelay Disable open delay (Optional)
* @return {jQuery|Element} The original jQuery object or DOM Element.
*/
show: function apiShowTip(element, event) {
show: function apiShowTip(element, event, disableDelay) {
// if we were given a mouse event then run the hover intent testing,
// otherwise, simply show the tooltip asap
if (isMouseEvent(event)) {
trackMouse(event);
session.previousX = event.pageX;
session.previousY = event.pageY;
$(element).data(DATA_DISPLAYCONTROLLER).show();
$(element).data(DATA_DISPLAYCONTROLLER).show(element, undefined, disableDelay);
} else {
$(element).first().data(DATA_DISPLAYCONTROLLER).show(true, true);
$(element).first().data(DATA_DISPLAYCONTROLLER).show(true, true, disableDelay);
}
return element;
},
Expand Down Expand Up @@ -285,7 +288,7 @@ $.powerTip = {
$.powerTip.hide(element, !isMouseEvent(event));
} else {
// tooltip for element is not active, so open it
$.powerTip.show(element, event);
$.powerTip.show(element, event, true);
}
return element;
},
Expand Down
41 changes: 38 additions & 3 deletions src/displaycontroller.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,23 @@
*/
function DisplayController(element, options, tipController) {
var hoverTimer = null,
myCloseDelay = null;
myCloseDelay = null,
myOpenDelay = null;

/**
* Begins the process of showing a tooltip.
* @private
* @param {boolean=} immediate Skip intent testing (optional).
* @param {boolean=} forceOpen Ignore cursor position and force tooltip to
* @param {boolean=} disableDelay Disable open delay (optional).
* open (optional).
*/
function openTooltip(immediate, forceOpen) {
function openTooltip(immediate, forceOpen, disableDelay) {
// if this instance already has an open delay in progress then halt it
if (myOpenDelay) {
myOpenDelay = session.openDelayTimeout = clearTimeout(myOpenDelay);
session.delayInProgress = false;
}
cancelTimer();
if (!element.data(DATA_HASACTIVEHOVER)) {
if (!immediate) {
Expand All @@ -44,7 +51,23 @@ function DisplayController(element, options, tipController) {
element.data(DATA_FORCEDOPEN, true);
}
closeAnyDelayed();
tipController.showTip(element);
if (!disableDelay) {
session.delayInProgress = true;
session.openDelayTimeout = setTimeout(
function openDelay() {
session.openDelayTimeout = null;
tipController.showTip(element);
session.delayInProgress = false;
myOpenDelay = null;
},
options.openDelay
);
// save internal reference open delay id so we can check if the
// active open delay belongs to this instance
myOpenDelay = session.openDelayTimeout;
} else {
tipController.showTip(element);
}
}
} else {
// cursor left and returned to this element, cancel close
Expand Down Expand Up @@ -123,6 +146,10 @@ function DisplayController(element, options, tipController) {
if (session.closeDelayTimeout && myCloseDelay === session.closeDelayTimeout || stopClose) {
cancelClose();
}
// Cancel the current open delay
if (myOpenDelay === session.openDelayTimeout) {
cancelOpen();
}
}

/**
Expand All @@ -133,6 +160,14 @@ function DisplayController(element, options, tipController) {
session.closeDelayTimeout = clearTimeout(session.closeDelayTimeout);
session.delayInProgress = false;
}
/**
* Cancels any active open delay timer.
* @private
*/
function cancelOpen() {
session.openDelayTimeout = clearTimeout(session.openDelayTimeout);
session.delayInProgress = false;
}

/**
* Asks any tooltips waiting on their close delay to close now.
Expand Down
13 changes: 10 additions & 3 deletions test/edge-cases.html
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,19 @@ <h2>Auto-disabling button</h2>
<p>The button below will disable itself 2 seconds after you hover or focus on it. The tooltip should close.</p>
<input type="button" value="Hover over me" title="This is the tooltip text." />
</section>
<section id="long-delay">
<h2>Long delay</h2>
<p>The two buttons below have tooltips with long delays. Mousing from one to the other should open tooltips normally.</p>
<section id="long-close-delay">
<h2>Long close delay</h2>
<p>The two buttons below have tooltips with long close delays. Mousing from one to the other should open tooltips normally.</p>
<input id="first-button" type="button" value="Hover over me" title="This is the tooltip text." />
<input id="second-button" type="button" value="Hover over me" title="This is the tooltip text." />
</section>

<section id="long-open-delay">
<h2>Long open delay</h2>
<p>The two buttons below have tooltips , one with long open delay, the other with no open delay. </p>
<input id="first-button" type="button" value="Hover over me openDelay" title="This is the tooltip text." />
<input id="second-button" type="button" value="Hover over me" title="This is the tooltip text." />
</section>
<section id="manual-and-interactive">
<h2>Manual and interactive</h2>
<p>The buttons below have tooltips, one with manual enabled, the other with mouseOnToPopup enabled. The manual tooltip should not close when you mouse off of the tooltip element.</p>
Expand Down
10 changes: 7 additions & 3 deletions test/tests-edge.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@ $(function() {
});
$('#auto-disable-button input').powerTip({ placement: 'e' });

// Long delay tooltips
$('#long-delay #first-button').powerTip({ closeDelay: 2000, mouseOnToPopup: true });
$('#long-delay #second-button').powerTip({ closeDelay: 2000 });
// Long close delay tooltips
$('#long-close-delay #first-button').powerTip({ closeDelay: 2000, mouseOnToPopup: true });
$('#long-close-delay #second-button').powerTip({ closeDelay: 2000 });

// Long open delay tooltips
$('#long-open-delay #first-button').powerTip({ openDelay: 2000 });
$('#long-open-delay #second-button').powerTip();

// Manual and interactive tooltips
$('#manual-and-interactive #manual-button').on('click', function() {
Expand Down
1 change: 1 addition & 0 deletions test/unit/core.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ $(function() {
ok($.fn.powerTip.defaults.hasOwnProperty('intentSensitivity'), 'intentSensitivity exists');
ok($.fn.powerTip.defaults.hasOwnProperty('intentPollInterval'), 'intentPollInterval exists');
ok($.fn.powerTip.defaults.hasOwnProperty('closeDelay'), 'closeDelay exists');
ok($.fn.powerTip.defaults.hasOwnProperty('openDelay'), 'openDelay exists');
ok($.fn.powerTip.defaults.hasOwnProperty('placement'), 'placement exists');
ok($.fn.powerTip.defaults.hasOwnProperty('smartPlacement'), 'smartPlacement exists');
ok($.fn.powerTip.defaults.hasOwnProperty('offset'), 'offset exists');
Expand Down
2 changes: 1 addition & 1 deletion test/unit/displaycontroller.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ $(function() {
)
);

dc.show(true);
dc.show(true, undefined, true);

ok(showCalled, 'showTip called');
});
Expand Down