Skip to content

Commit fbd0555

Browse files
committed
add fix for hint alignments + add callbacks
1 parent 12064c2 commit fbd0555

File tree

3 files changed

+83
-13
lines changed

3 files changed

+83
-13
lines changed

example/hint/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ <h3 class="muted">Intro.js</h3>
3333
<hr>
3434

3535
<div class="jumbotron">
36-
<h1 data-hint="This is a tooltip!" data-hintPosition="top-middle">Hints</h1>
36+
<h1 data-hint="This is a tooltip!" data-hintPosition="top-middle" data-position="bottom-right-aligned">Hints</h1>
3737
<p class="lead">Add hints using <code>data-hint</code> attribute.</p>
3838
<a class="btn btn-large btn-success" href="javascript:void(0);" onclick="javascript:introJs().addHints();">Add hints</a>
3939
</div>

intro.js

Lines changed: 79 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -428,15 +428,18 @@
428428
* @param {HTMLElement} tooltipLayer
429429
* @param {HTMLElement} arrowLayer
430430
* @param {HTMLElement} helperNumberLayer
431+
* @param {Boolean} hintMode
431432
*/
432-
function _placeTooltip(targetElement, tooltipLayer, arrowLayer, helperNumberLayer) {
433+
function _placeTooltip(targetElement, tooltipLayer, arrowLayer, helperNumberLayer, hintMode) {
433434
var tooltipCssClass = '',
434435
currentStepObj,
435436
tooltipOffset,
436437
targetOffset,
437438
windowSize,
438439
currentTooltipPosition;
439440

441+
hintMode = hintMode || false;
442+
440443
//reset the old style
441444
tooltipLayer.style.top = null;
442445
tooltipLayer.style.right = null;
@@ -474,11 +477,17 @@
474477
targetOffset = _getOffset(targetElement);
475478
tooltipOffset = _getOffset(tooltipLayer);
476479
windowSize = _getWinSize();
480+
477481
switch (currentTooltipPosition) {
478482
case 'top':
479483
arrowLayer.className = 'introjs-arrow bottom';
480484

481-
var tooltipLayerStyleLeft = 15;
485+
if (hintMode) {
486+
var tooltipLayerStyleLeft = 0;
487+
} else {
488+
var tooltipLayerStyleLeft = 15;
489+
}
490+
482491
_checkRight(targetOffset, tooltipLayerStyleLeft, tooltipOffset, windowSize, tooltipLayer);
483492
tooltipLayer.style.bottom = (targetOffset.height + 20) + 'px';
484493
break;
@@ -494,7 +503,7 @@
494503
}
495504
break;
496505
case 'left':
497-
if (this._options.showStepNumbers == true) {
506+
if (!hintMode && this._options.showStepNumbers == true) {
498507
tooltipLayer.style.top = '15px';
499508
}
500509

@@ -536,6 +545,12 @@
536545
arrowLayer.className = 'introjs-arrow top-middle';
537546

538547
var tooltipLayerStyleLeftRight = targetOffset.width / 2 - tooltipOffset.width / 2;
548+
549+
// a fix for middle aligned hints
550+
if (hintMode) {
551+
tooltipLayerStyleLeftRight += 5;
552+
}
553+
539554
if (_checkLeft(targetOffset, tooltipLayerStyleLeftRight, tooltipOffset, tooltipLayer)) {
540555
tooltipLayer.style.right = null;
541556
_checkRight(targetOffset, tooltipLayerStyleLeftRight, tooltipOffset, windowSize, tooltipLayer);
@@ -1146,10 +1161,13 @@
11461161
* @method _removeHintTooltip
11471162
*/
11481163
function _removeHintTooltip() {
1149-
var tooltip = this._targetElement.querySelector('.introjs-hintTooltip');
1164+
var tooltip = this._targetElement.querySelector('.introjs-hintReference');
1165+
11501166

11511167
if (tooltip) {
1168+
var step = tooltip.getAttribute('data-step');
11521169
tooltip.parentNode.removeChild(tooltip);
1170+
return step;
11531171
}
11541172
};
11551173

@@ -1198,8 +1216,8 @@
11981216

11991217
_addHints.call(this);
12001218

1201-
if (window.addEventListener) {
1202-
window.addEventListener('click', _removeHintTooltip.bind(this), true);
1219+
if (document.addEventListener) {
1220+
document.addEventListener('click', _removeHintTooltip.bind(this), false);
12031221
//for window resize
12041222
//window.addEventListener('resize', this._onResize, true);
12051223
} else if (document.attachEvent) { //IE
@@ -1231,6 +1249,7 @@
12311249
* @method _addHints
12321250
*/
12331251
function _addHints() {
1252+
var self = this;
12341253
var hintsWrapper = document.createElement('div');
12351254
hintsWrapper.className = 'introjs-hints';
12361255

@@ -1240,8 +1259,16 @@
12401259
var hint = document.createElement('a');
12411260
hint.href = "javascript:void(0);";
12421261

1243-
// when user clicks on the hint element
1244-
hint.onclick = _hintClick.bind(this, hint, item, i);
1262+
(function (hint, item, i) {
1263+
// when user clicks on the hint element
1264+
hint.onclick = function(e) {
1265+
var evt = e ? e : window.event;
1266+
if (evt.stopPropagation) evt.stopPropagation();
1267+
if (evt.cancelBubble != null) evt.cancelBubble = true;
1268+
1269+
_hintClick.call(self, hint, item, i);
1270+
};
1271+
}(hint, item, i));
12451272

12461273
hint.className = 'introjs-hint';
12471274
var hintDot = document.createElement('div');
@@ -1307,7 +1334,13 @@
13071334
* @param {Number} stepId
13081335
*/
13091336
function _hintClick(hintElement, item, stepId) {
1310-
_removeHintTooltip.call(this);
1337+
// remove all open tooltips
1338+
var removedStep = _removeHintTooltip.call(this);
1339+
1340+
// to toggle the tooltip
1341+
if (parseInt(removedStep, 10) == stepId) {
1342+
return;
1343+
}
13111344

13121345
var tooltipLayer = document.createElement('div');
13131346
var tooltipTextLayer = document.createElement('div');
@@ -1316,6 +1349,17 @@
13161349

13171350
tooltipLayer.className = 'introjs-tooltip';
13181351

1352+
tooltipLayer.onclick = function (e) {
1353+
//IE9 & Other Browsers
1354+
if (e.stopPropagation) {
1355+
e.stopPropagation();
1356+
}
1357+
//IE8 and Lower
1358+
else {
1359+
e.cancelBubble = true;
1360+
}
1361+
};
1362+
13191363
tooltipTextLayer.className = 'introjs-tooltiptext';
13201364

13211365
var tooltipWrapper = document.createElement('p');
@@ -1338,15 +1382,15 @@
13381382
this._currentStep = hintElement.getAttribute('data-step');
13391383

13401384
// align reference layer position
1341-
referenceLayer.className = 'introjs-tooltipReferenceLayer introjs-hintTooltip';
1385+
referenceLayer.className = 'introjs-tooltipReferenceLayer introjs-hintReference';
13421386
referenceLayer.setAttribute('data-step', hintElement.getAttribute('data-step'));
13431387
_setHelperLayerPosition.call(this, referenceLayer);
13441388

13451389
referenceLayer.appendChild(tooltipLayer);
13461390
document.body.appendChild(referenceLayer);
13471391

13481392
//set proper position
1349-
_placeTooltip.call(this, hintElement, tooltipLayer, arrowLayer);
1393+
_placeTooltip.call(this, hintElement, tooltipLayer, arrowLayer, null, true);
13501394
};
13511395

13521396
/**
@@ -1508,6 +1552,30 @@
15081552
}
15091553
return this;
15101554
},
1555+
onhintsadded: function(providedCallback) {
1556+
if (typeof (providedCallback) === 'function') {
1557+
this._hintsAddedCallback = providedCallback;
1558+
} else {
1559+
throw new Error('Provided callback for onhintsadded was not a function.');
1560+
}
1561+
return this;
1562+
},
1563+
onhintclick: function(providedCallback) {
1564+
if (typeof (providedCallback) === 'function') {
1565+
this._hintClickCallback = providedCallback;
1566+
} else {
1567+
throw new Error('Provided callback for onhintclick was not a function.');
1568+
}
1569+
return this;
1570+
},
1571+
onhintremove: function(providedCallback) {
1572+
if (typeof (providedCallback) === 'function') {
1573+
this._hintRemoveCallback = providedCallback;
1574+
} else {
1575+
throw new Error('Provided callback for onhintremove was not a function.');
1576+
}
1577+
return this;
1578+
},
15111579
onexit: function(providedCallback) {
15121580
if (typeof (providedCallback) === 'function') {
15131581
this._introExitCallback = providedCallback;

introjs.css

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,9 @@ tr.introjs-showElement > th {
347347

348348
.introjs-hint {
349349
position: absolute;
350-
background: rgba(99, 99, 99, 0.18);
350+
background: transparent;
351+
width: 20px;
352+
height: 15px;
351353
}
352354

353355
.introjs-hint:hover > .introjs-hint-pulse {

0 commit comments

Comments
 (0)