Skip to content

Commit 2d599ae

Browse files
committed
feat: 增加 hackTooltip 防止 bs 脚本报错
1 parent 748e0aa commit 2d599ae

File tree

5 files changed

+38
-7
lines changed

5 files changed

+38
-7
lines changed

src/BootstrapBlazor/Components/Button/PopConfirmButton.razor.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getDescribedElement, getDescribedOwner, hackPopover, isDisabled } from "../../modules/utility.js"
1+
import { getDescribedElement, getDescribedOwner, hackTooltip, hackPopover, isDisabled } from "../../modules/utility.js"
22
import { showTooltip, removeTooltip } from "./Button.razor.js"
33
import Data from "../../modules/data.js"
44
import EventHandler from "../../modules/event-handler.js"
@@ -15,6 +15,8 @@ export function init(id) {
1515
return
1616
}
1717

18+
hackTooltip();
19+
1820
const confirm = {
1921
el,
2022
container: el.querySelector('[data-bb-toggle="confirm"]')
@@ -167,9 +169,6 @@ export function dispose(id) {
167169
}
168170
if (confirm.popover) {
169171
confirm.popover.dispose();
170-
// workaround for https://github.com/twbs/bootstrap/issues/37474
171-
confirm.popover._activeTrigger = {};
172-
confirm.popover._element = document.createElement('noscript'); // placeholder with no behavior
173172
}
174173
if (config.dismiss) {
175174
EventHandler.off(document, 'click', config.dismiss, confirm.dismissHandler)

src/BootstrapBlazor/Components/Popover/Popover.razor.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { getDescribedElement } from "../../modules/utility.js"
1+
import { getDescribedElement, hackTooltip } from "../../modules/utility.js"
22
import EventHandler from "../../modules/event-handler.js"
33

44
export function init(id, options) {
55
const el = document.getElementById(id)
66
if (el) {
7+
hackTooltip();
78
createPopover(el, options);
89
}
910
}

src/BootstrapBlazor/Components/Tooltip/Tooltip.razor.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import Data from "../../modules/data.js"
1+
import { hackTooltip } from "../../modules/utility.js"
2+
import Data from "../../modules/data.js"
23

34
export function init(id) {
45
const el = document.getElementById(id)
56
if (el) {
7+
hackTooltip();
8+
69
const fallbackPlacements = (el.getAttribute('data-bs-fallbackPlacements') || 'top,right,bottom,left').split(',');
710
const tip = {
811
tooltip: new bootstrap.Tooltip(el, {

src/BootstrapBlazor/wwwroot/modules/base-popover.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import { getDescribedElement, getDescribedOwner, hackPopover, isDisabled } from "./utility.js"
1+
import { getDescribedElement, getDescribedOwner, hackTooltip, hackPopover, isDisabled } from "./utility.js"
22
import EventHandler from "./event-handler.js"
33

44
const Popover = {
55
init(el, config) {
6+
hackTooltip();
7+
68
const popover = {
79
...{
810
el,

src/BootstrapBlazor/wwwroot/modules/utility.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,31 @@ const hackPopover = (popover, css) => {
570570
}
571571
}
572572

573+
const hackTooltip = function () {
574+
window.BootstrapBlazor ??= {};
575+
576+
if (window.BootstrapBlazor.Tooltip === void 0) {
577+
window.BootstrapBlazor.Tooltip = {
578+
hooked: false,
579+
hackDispose: function () {
580+
if (this.hooked === false) {
581+
this.hooked = true;
582+
583+
const originalDispose = bootstrap.Tooltip.prototype.dispose;
584+
bootstrap.Tooltip.prototype.dispose = function () {
585+
originalDispose.call(this);
586+
// fix https://github.com/twbs/bootstrap/issues/37474
587+
this._activeTrigger = {};
588+
this._element = document.createElement('noscript'); // placeholder with no behavior
589+
}
590+
}
591+
}
592+
}
593+
}
594+
595+
window.BootstrapBlazor.Tooltip.hackDispose();
596+
}
597+
573598
const setIndeterminate = (object, state) => {
574599
const element = getElementById(object)
575600
if (isElement(element)) {
@@ -832,6 +857,7 @@ export {
832857
getWindow,
833858
getWindowScroll,
834859
getUID,
860+
hackTooltip,
835861
hackPopover,
836862
removeLink,
837863
removeScript,

0 commit comments

Comments
 (0)