diff options
| author | Adrien Siami <[email protected]> | 2015-03-25 14:46:21 +0100 |
|---|---|---|
| committer | Chris Rebert <[email protected]> | 2015-04-21 22:20:15 -0700 |
| commit | e949505b89ca146e3af0cf735e100c82703f1cda (patch) | |
| tree | edc2027677d0377a98d3fa0a3f2a263b93491815 /js | |
| parent | 48232aad6cdb5280e55e0aa0c42a4bb0d06ac03b (diff) | |
| download | bootstrap-e949505b89ca146e3af0cf735e100c82703f1cda.tar.xz bootstrap-e949505b89ca146e3af0cf735e100c82703f1cda.zip | |
Allow viewport option to be a function
Closes #16151 by merging a rebased version of it that adds docs and 1 more assertion.
Diffstat (limited to 'js')
| -rw-r--r-- | js/tests/unit/tooltip.js | 31 | ||||
| -rw-r--r-- | js/tooltip.js | 2 |
2 files changed, 32 insertions, 1 deletions
diff --git a/js/tests/unit/tooltip.js b/js/tests/unit/tooltip.js index 8086631c8..0cb964e9b 100644 --- a/js/tests/unit/tooltip.js +++ b/js/tests/unit/tooltip.js @@ -733,6 +733,37 @@ $(function () { $styles.remove() }) + QUnit.test('should get viewport element from function', function (assert) { + assert.expect(3) + var styles = '<style>' + + '.tooltip, .tooltip .tooltip-inner { width: 200px; height: 200px; max-width: none; }' + + '.container-viewport { position: absolute; top: 50px; left: 60px; width: 300px; height: 300px; }' + + 'a[rel="tooltip"] { position: fixed; }' + + '</style>' + var $styles = $(styles).appendTo('head') + + var $container = $('<div class="container-viewport"/>').appendTo(document.body) + var $target = $('<a href="#" rel="tooltip" title="tip" style="top: 50px; left: 350px;"/>').appendTo($container) + $target + .bootstrapTooltip({ + placement: 'bottom', + viewport: function ($element) { + assert.strictEqual($element[0], $target[0], 'viewport function was passed target as argument') + return ($element.closest('.container-viewport')) + } + }) + + $target.bootstrapTooltip('show') + var $tooltip = $container.find('.tooltip') + assert.strictEqual(Math.round($tooltip.offset().left), Math.round(60 + $container.width() - $tooltip[0].offsetWidth)) + + $target.bootstrapTooltip('hide') + assert.strictEqual($('.tooltip').length, 0, 'tooltip removed from dom') + + $container.remove() + $styles.remove() + }) + QUnit.test('should not error when trying to show an auto-placed tooltip that has been removed from the dom', function (assert) { assert.expect(1) var passed = true diff --git a/js/tooltip.js b/js/tooltip.js index bbff2cdec..b2d775938 100644 --- a/js/tooltip.js +++ b/js/tooltip.js @@ -50,7 +50,7 @@ this.type = type this.$element = $(element) this.options = this.getOptions(options) - this.$viewport = this.options.viewport && $(this.options.viewport.selector || this.options.viewport) + this.$viewport = this.options.viewport && $($.isFunction(this.options.viewport) ? this.options.viewport.call(this, this.$element) : (this.options.viewport.selector || this.options.viewport)) if (this.$element[0] instanceof document.constructor && !this.options.selector) { throw new Error('`selector` option must be specified when initializing ' + this.type + ' on the window.document object!') |
