diff --git a/docs/_includes/js/tooltips.html b/docs/_includes/js/tooltips.html
index 2656556bed33..b952e8504022 100644
--- a/docs/_includes/js/tooltips.html
+++ b/docs/_includes/js/tooltips.html
@@ -134,6 +134,14 @@
Options
Appends the tooltip to a specific element. Example: container: 'body'
+
+ viewport |
+ string | object |
+ { selector: 'body', padding: 0 } |
+
+ Keeps the tooltip within the bounds of this element. Example: viewport: '#viewport' or { selector: '#viewport', padding: 0 }
+ |
+
diff --git a/examples/tooltips/viewport.html b/examples/tooltips/viewport.html
new file mode 100644
index 000000000000..6efcd0495cd0
--- /dev/null
+++ b/examples/tooltips/viewport.html
@@ -0,0 +1,103 @@
+
+
+
+
+
+
+
+
+
+
+ Tooltip Viewport Example for Bootstrap
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ There is a button down there ↓
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/js/tests/unit/tooltip.js b/js/tests/unit/tooltip.js
index 9df234236c36..e670883e358b 100644
--- a/js/tests/unit/tooltip.js
+++ b/js/tests/unit/tooltip.js
@@ -337,12 +337,12 @@ $(function () {
})
test('should add position class before positioning so that position-specific styles are taken into account', function () {
- $('head').append('')
+ $('head').append('')
var container = $('').appendTo('body'),
target = $('')
.appendTo(container)
- .tooltip({placement: 'right'})
+ .tooltip({placement: 'right', viewport: null})
.tooltip('show'),
tooltip = container.find('.tooltip')
@@ -352,6 +352,7 @@ $(function () {
var topDiff = top - top2
ok(topDiff <= 1 && topDiff >= -1)
target.tooltip('hide')
+ $('head #test').remove()
})
test('tooltip title test #1', function () {
@@ -428,4 +429,80 @@ $(function () {
ttContainer.remove()
})
+ test('should adjust the tip\'s top when up against the top of the viewport', function () {
+ $('head').append('')
+
+ var container = $('').appendTo('body'),
+ target = $('')
+ .appendTo(container)
+ .tooltip({placement: 'right', viewport: {selector: 'body', padding: 12}})
+ .tooltip('show'),
+ tooltip = container.find('.tooltip')
+
+ ok( Math.round(tooltip.offset().top) === 12 )
+ target.tooltip('hide')
+ $('head #test').remove()
+ })
+
+ test('should adjust the tip\'s top when up against the bottom of the viewport', function () {
+ $('head').append('')
+
+ var container = $('').appendTo('body'),
+ target = $('')
+ .appendTo(container)
+ .tooltip({placement: 'right', viewport: {selector: 'body', padding: 12}})
+ .tooltip('show'),
+ tooltip = container.find('.tooltip')
+
+ ok( Math.round(tooltip.offset().top) === Math.round($(window).height() - 12 - tooltip[0].offsetHeight) )
+ target.tooltip('hide')
+ $('head #test').remove()
+ })
+
+ test('should adjust the tip\'s left when up against the left of the viewport', function () {
+ $('head').append('')
+
+ var container = $('').appendTo('body'),
+ target = $('')
+ .appendTo(container)
+ .tooltip({placement: 'bottom', viewport: {selector: 'body', padding: 12}})
+ .tooltip('show'),
+ tooltip = container.find('.tooltip')
+
+ ok( Math.round(tooltip.offset().left) === 12 )
+ target.tooltip('hide')
+ $('head #test').remove()
+ })
+
+ test('should adjust the tip\'s left when up against the right of the viewport', function () {
+ $('head').append('')
+
+ var container = $('').appendTo('body'),
+ target = $('')
+ .appendTo(container)
+ .tooltip({placement: 'bottom', viewport: {selector: 'body', padding: 12}})
+ .tooltip('show'),
+ tooltip = container.find('.tooltip')
+
+ ok( Math.round(tooltip.offset().left) === Math.round($(window).width() - 12 - tooltip[0].offsetWidth) )
+ target.tooltip('hide')
+ $('head #test').remove()
+ })
+
+ test('should adjust the tip when up against the right of an arbitrary viewport', function () {
+ $('head').append('')
+ $('head').append('')
+
+ var container = $('', {class: 'container-viewport'}).appendTo('body'),
+ target = $('')
+ .appendTo(container)
+ .tooltip({placement: 'bottom', viewport: '.container-viewport'})
+ .tooltip('show'),
+ tooltip = container.find('.tooltip')
+
+ ok( Math.round(tooltip.offset().left) === Math.round(60 + container.width() - tooltip[0].offsetWidth) )
+ target.tooltip('hide')
+ $('head #test').remove()
+ $('head #viewport-style').remove()
+ })
})
diff --git a/js/tooltip.js b/js/tooltip.js
index eb7875c9fe37..f27beacc6da0 100644
--- a/js/tooltip.js
+++ b/js/tooltip.js
@@ -34,14 +34,19 @@
title: '',
delay: 0,
html: false,
- container: false
+ container: false,
+ viewport: {
+ selector: 'body',
+ padding: 0
+ }
}
Tooltip.prototype.init = function (type, element, options) {
- this.enabled = true
- this.type = type
- this.$element = $(element)
- this.options = this.getOptions(options)
+ this.enabled = true
+ this.type = type
+ this.$element = $(element)
+ this.options = this.getOptions(options)
+ this.$viewport = this.options.viewport && $(this.options.viewport.selector || this.options.viewport)
var triggers = this.options.trigger.split(' ')
@@ -157,18 +162,14 @@
var actualHeight = $tip[0].offsetHeight
if (autoPlace) {
- var $parent = this.$element.parent()
-
var orgPlacement = placement
- var docScroll = document.documentElement.scrollTop
- var parentWidth = this.options.container == 'body' ? window.innerWidth : $parent.outerWidth()
- var parentHeight = this.options.container == 'body' ? window.innerHeight : $parent.outerHeight()
- var parentLeft = this.options.container == 'body' ? 0 : $parent.offset().left
-
- placement = placement == 'bottom' && pos.top + pos.height + actualHeight - docScroll > parentHeight ? 'top' :
- placement == 'top' && pos.top - docScroll - actualHeight < 0 ? 'bottom' :
- placement == 'right' && pos.right + actualWidth > parentWidth ? 'left' :
- placement == 'left' && pos.left - actualWidth < parentLeft ? 'right' :
+ var $parent = this.$element.parent()
+ var parentDim = this.getPosition($parent)
+
+ placement = placement == 'bottom' && pos.top + pos.height + actualHeight - parentDim.scroll > parentDim.height ? 'top' :
+ placement == 'top' && pos.top - parentDim.scroll - actualHeight < 0 ? 'bottom' :
+ placement == 'right' && pos.right + actualWidth > parentDim.width ? 'left' :
+ placement == 'left' && pos.left - actualWidth < parentDim.left ? 'right' :
placement
$tip
@@ -228,29 +229,20 @@
var actualHeight = $tip[0].offsetHeight
if (placement == 'top' && actualHeight != height) {
- replace = true
offset.top = offset.top + height - actualHeight
}
- if (/bottom|top/.test(placement)) {
- var delta = 0
+ var delta = this.getViewportAdjustedDelta(placement, offset, actualWidth, actualHeight)
- if (offset.left < 0) {
- delta = offset.left * -2
- offset.left = 0
+ if (delta.left) offset.left += delta.left
+ else offset.top += delta.top
- $tip.offset(offset)
-
- actualWidth = $tip[0].offsetWidth
- actualHeight = $tip[0].offsetHeight
- }
+ var arrowDelta = delta.left ? delta.left * 2 - width + actualWidth : delta.top * 2 - height + actualHeight
+ var arrowPosition = delta.left ? 'left' : 'top'
+ var arrowOffsetPosition = delta.left ? 'offsetWidth' : 'offsetHeight'
- this.replaceArrow(delta - width + actualWidth, actualWidth, 'left')
- } else {
- this.replaceArrow(actualHeight - height, actualHeight, 'top')
- }
-
- if (replace) $tip.offset(offset)
+ $tip.offset(offset)
+ this.replaceArrow(arrowDelta, $tip[0][arrowOffsetPosition], arrowPosition)
}
Tooltip.prototype.replaceArrow = function (delta, dimension, position) {
@@ -303,12 +295,15 @@
return this.getTitle()
}
- Tooltip.prototype.getPosition = function () {
- var el = this.$element[0]
- return $.extend({}, (typeof el.getBoundingClientRect == 'function') ? el.getBoundingClientRect() : {
- width: el.offsetWidth,
- height: el.offsetHeight
- }, this.$element.offset())
+ Tooltip.prototype.getPosition = function ($element) {
+ $element = $element || this.$element
+ var el = $element[0]
+ var isBody = el.tagName == 'BODY'
+ return $.extend({}, (typeof el.getBoundingClientRect == 'function') ? el.getBoundingClientRect() : null, {
+ scroll: isBody ? document.documentElement.scrollTop || document.body.scrollTop : $element.scrollTop(),
+ width: isBody ? $(window).width() : $element.outerWidth(),
+ height: isBody ? $(window).height() : $element.outerHeight()
+ }, isBody ? {top: 0, left: 0} : $element.offset())
}
Tooltip.prototype.getCalculatedOffset = function (placement, pos, actualWidth, actualHeight) {
@@ -316,6 +311,35 @@
placement == 'top' ? { top: pos.top - actualHeight, left: pos.left + pos.width / 2 - actualWidth / 2 } :
placement == 'left' ? { top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left - actualWidth } :
/* placement == 'right' */ { top: pos.top + pos.height / 2 - actualHeight / 2, left: pos.left + pos.width }
+
+ }
+
+ Tooltip.prototype.getViewportAdjustedDelta = function (placement, pos, actualWidth, actualHeight) {
+ var delta = { top: 0, left: 0 }
+ if (!this.$viewport) return delta
+
+ var viewportPadding = this.options.viewport && this.options.viewport.padding || 0
+ var viewportDimensions = this.getPosition(this.$viewport)
+
+ if (/right|left/.test(placement)) {
+ var topEdgeOffset = pos.top - viewportPadding - viewportDimensions.scroll
+ var bottomEdgeOffset = pos.top + viewportPadding - viewportDimensions.scroll + actualHeight
+ if (topEdgeOffset < viewportDimensions.top) { // top overflow
+ delta.top = viewportDimensions.top - topEdgeOffset
+ } else if (bottomEdgeOffset > viewportDimensions.top + viewportDimensions.height) { // bottom overflow
+ delta.top = viewportDimensions.top + viewportDimensions.height - bottomEdgeOffset
+ }
+ } else {
+ var leftEdgeOffset = pos.left - viewportPadding
+ var rightEdgeOffset = pos.left + viewportPadding + actualWidth
+ if (leftEdgeOffset < viewportDimensions.left) { // left overflow
+ delta.left = viewportDimensions.left - leftEdgeOffset
+ } else if (rightEdgeOffset > viewportDimensions.width) { // right overflow
+ delta.left = viewportDimensions.left + viewportDimensions.width - rightEdgeOffset
+ }
+ }
+
+ return delta
}
Tooltip.prototype.getTitle = function () {