]> git.ipfire.org Git - thirdparty/bootstrap.git/commitdiff
Add support for tooltip offset option to be a function.
authorJason Golieb <jgolieb@hortonworks.com>
Mon, 28 Jan 2019 20:55:21 +0000 (22:55 +0200)
committerXhmikosR <xhmikosr@gmail.com>
Tue, 5 Feb 2019 08:24:49 +0000 (10:24 +0200)
js/src/tooltip.js
js/tests/unit/tooltip.js
site/docs/4.2/components/tooltips.md

index 366781b3e894881c9c3f204491d7325bea634fd5..0b6e21085f8f203d83ee19aa2aa69f0245b81ba8 100644 (file)
@@ -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
           },
index 54dbe57bd86b20eb5badad2224bc7f3c9dd50777..30829d24d56d2f0e0b3bf1edfbe90a086bbdd146 100644 (file)
@@ -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')
+  })
 })
index e49db563479a3d7fc96d5bf5178aa716cb8a5d97..0a6be475888daec3c994d40a75e6efa70791afb4 100644 (file)
@@ -234,9 +234,13 @@ Options can be passed via data attributes or JavaScript. For data attributes, ap
     </tr>
     <tr>
       <td>offset</td>
-      <td>number | string</td>
+      <td>number | string | function</td>
       <td>0</td>
-      <td>Offset of the tooltip relative to its target. For more information refer to Popper.js's <a href="https://popper.js.org/popper-documentation.html#modifiers..offset.offset">offset docs</a>.</td>
+      <td>
+        <p>Offset of the tooltip relative to its target.</p>
+        <p>When a function is used to determine the offset, it is called with an object containing the offset data as its first argument. The function must return an object with the same structure. The triggering element DOM node is passed as the second argument.</p>
+        <p>For more information refer to Popper.js's <a href="https://popper.js.org/popper-documentation.html#modifiers..offset.offset">offset docs</a>.</p>
+      </td>
     </tr>
     <tr>
       <td>fallbackPlacement</td>