mirror of
https://github.com/twbs/bootstrap.git
synced 2025-08-18 11:21:23 +02:00
Add support for tooltip offset option to be a function.
This commit is contained in:
@@ -32,7 +32,7 @@ const DefaultType = {
|
||||
html : 'boolean',
|
||||
selector : '(string|boolean)',
|
||||
placement : '(string|function)',
|
||||
offset : '(number|string)',
|
||||
offset : '(number|string|function)',
|
||||
container : '(string|element|boolean)',
|
||||
fallbackPlacement : '(string|array)',
|
||||
boundary : '(string|element)'
|
||||
@@ -273,6 +273,16 @@ class Tooltip {
|
||||
const attachment = this._getAttachment(placement)
|
||||
this.addAttachmentClass(attachment)
|
||||
|
||||
const offsetConf = {}
|
||||
if (typeof this.config.offset === 'function') {
|
||||
offsetConf.fn = (data) => {
|
||||
data.offsets = $.extend({}, data.offsets, this.config.offset(data.offsets, this.element) || {})
|
||||
return data
|
||||
}
|
||||
} else {
|
||||
offsetConf.offset = this.config.offset
|
||||
}
|
||||
|
||||
const container = this._getContainer()
|
||||
$(tip).data(this.constructor.DATA_KEY, this)
|
||||
|
||||
@@ -285,9 +295,7 @@ class Tooltip {
|
||||
this._popper = new Popper(this.element, tip, {
|
||||
placement: attachment,
|
||||
modifiers: {
|
||||
offset: {
|
||||
offset: this.config.offset
|
||||
},
|
||||
offset: offsetConf,
|
||||
flip: {
|
||||
behavior: this.config.fallbackPlacement
|
||||
},
|
||||
|
@@ -1069,4 +1069,41 @@ $(function () {
|
||||
|
||||
assert.strictEqual(tooltip._isEnabled, true)
|
||||
})
|
||||
|
||||
QUnit.test('should create offset modifier correctly when offset option is a function', function (assert) {
|
||||
assert.expect(2)
|
||||
|
||||
var getOffset = function (offsets) {
|
||||
return offsets
|
||||
}
|
||||
|
||||
var $trigger = $('<a href="#" rel="tooltip" data-trigger="click" title="Another tooltip"/>')
|
||||
.appendTo('#qunit-fixture')
|
||||
.bootstrapTooltip({
|
||||
offset: getOffset
|
||||
})
|
||||
|
||||
var tooltip = $trigger.data('bs.tooltip')
|
||||
var offset = tooltip._getOffset()
|
||||
|
||||
assert.ok(typeof offset.offset === 'undefined')
|
||||
assert.ok(typeof offset.fn === 'function')
|
||||
})
|
||||
|
||||
QUnit.test('should create offset modifier correctly when offset option is not a function', function (assert) {
|
||||
assert.expect(2)
|
||||
|
||||
var myOffset = 42
|
||||
var $trigger = $('<a href="#" rel="tooltip" data-trigger="click" title="Another tooltip"/>')
|
||||
.appendTo('#qunit-fixture')
|
||||
.bootstrapTooltip({
|
||||
offset: myOffset
|
||||
})
|
||||
|
||||
var tooltip = $trigger.data('bs.tooltip')
|
||||
var offset = tooltip._getOffset()
|
||||
|
||||
assert.strictEqual(offset.offset, myOffset)
|
||||
assert.ok(typeof offset.fn === 'undefined')
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user