Skip to content

Commit 3792a7b

Browse files
Add example for merging overlapping tooltips (#1032)
Co-Authored-By: Ömür Yıldırım <[email protected]>
1 parent a3c2b42 commit 3792a7b

File tree

7 files changed

+128
-3
lines changed

7 files changed

+128
-3
lines changed

documentation/_run/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700" rel="stylesheet">
1212

13-
<link href="/nouislider/documentation/assets/base.css?v=2" rel="stylesheet">
13+
<link href="/nouislider/documentation/assets/base.css?v=3" rel="stylesheet">
1414
<link href="/nouislider/documentation/assets/prism.css" rel="stylesheet">
1515
<script src="/nouislider/documentation/assets/wNumb.js"></script>
1616

documentation/assets/base.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@
120120
content: "";
121121
}
122122
.viewer-header.open + .viewer-content {
123-
max-height: 2000px;
123+
max-height: 2500px;
124124
}
125125

126126
.options {
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php sect('merging-tooltips'); ?>
2+
<h1>Merging overlapping tooltips</h1>
3+
4+
<section>
5+
6+
<div class="view">
7+
<p><a href="https://github.com/leongersen/noUiSlider/issues/1032">Issue #1032</a> asks to merge overlapping tooltips. As this feature is outside the scope of the tooltips-feature in noUiSlider, this example can be used to implement this feature using the event system.</p>
8+
9+
<div class="example">
10+
<div id="merging-tooltips"></div>
11+
<?php run('merging-tooltips'); ?>
12+
<?php run('merging-tooltips-slider'); ?>
13+
</div>
14+
</div>
15+
16+
<div class="side">
17+
<div class="viewer-header">Initializing the slider</div>
18+
19+
<div class="viewer-content">
20+
<?php code('merging-tooltips-slider'); ?>
21+
</div>
22+
23+
<div class="viewer-header">Merging overlapping tooltips</div>
24+
25+
<div class="viewer-content">
26+
<?php code('merging-tooltips'); ?>
27+
</div>
28+
</div>
29+
</section>

documentation/examples.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
<section>
88
<ul>
9-
<li><a href="#section-colorpicker">Color Picker</a></li>
9+
<li><a href="#section-colorpicker">Color picker</a></li>
1010
<li><a href="#section-dates">Using dates</a></li>
11+
<li><a href="#section-merging-tooltips">Merging overlapping tooltips</a></li>
1112
<li><a href="#section-html5">Working with HTML5 input types</a></li>
1213
<li><a href="#section-non-linear">Using non linear ranges</a></li>
1314
<li><a href="#section-lock">Locking two sliders together</a></li>
@@ -29,6 +30,7 @@
2930

3031
<?php include 'examples-content/colorpicker.php'; ?>
3132
<?php include 'examples-content/dates.php'; ?>
33+
<?php include 'examples-content/merging-tooltips.php'; ?>
3234
<?php include 'examples-content/html5.php'; ?>
3335
<?php include 'examples-content/non-linear.php'; ?>
3436
<?php include 'examples-content/lock.php'; ?>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var mergingTooltipSlider = document.getElementById('merging-tooltips');
2+
3+
noUiSlider.create(mergingTooltipSlider, {
4+
start: [20, 32, 50, 70, 80, 90],
5+
connect: true,
6+
tooltips: [false, true, true, true, true, true],
7+
range: {
8+
'min': 0,
9+
'max': 100
10+
}
11+
});
12+
13+
mergeTooltips(mergingTooltipSlider, 15, ' - ');
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* @param slider HtmlElement with an initialized slider
3+
* @param threshold Minimum proximity (in percentages) to merge tooltips
4+
* @param separator String joining tooltips
5+
*/
6+
function mergeTooltips(slider, threshold, separator) {
7+
8+
var textIsRtl = getComputedStyle(slider).direction === 'rtl';
9+
var isRtl = slider.noUiSlider.options.direction === 'rtl';
10+
var isVertical = slider.noUiSlider.options.orientation === 'vertical';
11+
var tooltips = slider.noUiSlider.getTooltips();
12+
var origins = slider.noUiSlider.getOrigins();
13+
14+
// Move tooltips into the origin element. The default stylesheet handles this.
15+
tooltips.forEach(function (tooltip, index) {
16+
if (tooltip) {
17+
origins[index].appendChild(tooltip);
18+
}
19+
});
20+
21+
slider.noUiSlider.on('update', function (values, handle, unencoded, tap, positions) {
22+
23+
var pools = [[]];
24+
var poolPositions = [[]];
25+
var poolValues = [[]];
26+
var atPool = 0;
27+
28+
// Assign the first tooltip to the first pool, if the tooltip is configured
29+
if (tooltips[0]) {
30+
pools[0][0] = 0;
31+
poolPositions[0][0] = positions[0];
32+
poolValues[0][0] = values[0];
33+
}
34+
35+
for (var i = 1; i < positions.length; i++) {
36+
if (!tooltips[i] || (positions[i] - positions[i - 1]) > threshold) {
37+
atPool++;
38+
pools[atPool] = [];
39+
poolValues[atPool] = [];
40+
poolPositions[atPool] = [];
41+
}
42+
43+
if (tooltips[i]) {
44+
pools[atPool].push(i);
45+
poolValues[atPool].push(values[i]);
46+
poolPositions[atPool].push(positions[i]);
47+
}
48+
}
49+
50+
pools.forEach(function (pool, poolIndex) {
51+
var handlesInPool = pool.length;
52+
53+
for (var j = 0; j < handlesInPool; j++) {
54+
var handleNumber = pool[j];
55+
56+
if (j === handlesInPool - 1) {
57+
var offset = 0;
58+
59+
poolPositions[poolIndex].forEach(function (value) {
60+
offset += 1000 - 10 * value;
61+
});
62+
63+
var direction = isVertical ? 'bottom' : 'right';
64+
var last = isRtl ? 0 : handlesInPool - 1;
65+
var lastOffset = 1000 - 10 * poolPositions[poolIndex][last];
66+
offset = (textIsRtl && !isVertical ? 100 : 0) + (offset / handlesInPool) - lastOffset;
67+
68+
// Center this tooltip over the affected handles
69+
tooltips[handleNumber].innerHTML = poolValues[poolIndex].join(separator);
70+
tooltips[handleNumber].style.display = 'block';
71+
tooltips[handleNumber].style[direction] = offset + '%';
72+
} else {
73+
// Hide this tooltip
74+
tooltips[handleNumber].style.display = 'none';
75+
}
76+
}
77+
});
78+
});
79+
}

documentation/slider-options.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,8 @@
358358

359359
<p>Tooltips can be removed from a slider using the <code>removeTooltips()</code> method.</p>
360360

361+
<p>To merge overlapping tooltips, refer to <a href="/nouislider/examples/#section-merging-tooltips">this example</a>.</p>
362+
361363
<div class="example overflow">
362364
<div id="slider-tooltips"></div>
363365
<?php run('tooltips'); ?>

0 commit comments

Comments
 (0)