Skip to content

Commit 49454b1

Browse files
authored
feat(Tooltip): add hackTooltip function prevent javascript exception (#5318)
* refactor: 重构代码 * fix: 修复报错问题 * feat: 增加 hackTooltip 防止 bs 脚本报错 * chore: bump version 9.3.1-beta09
1 parent c5beff0 commit 49454b1

File tree

7 files changed

+42
-8
lines changed

7 files changed

+42
-8
lines changed

src/BootstrapBlazor/BootstrapBlazor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>9.3.1-beta08</Version>
4+
<Version>9.3.1-beta09</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

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

Lines changed: 4 additions & 2 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"]')
@@ -166,7 +168,7 @@ export function dispose(id) {
166168
EventHandler.off(document, 'click', confirm.closeConfirm)
167169
}
168170
if (confirm.popover) {
169-
confirm.popover.dispose()
171+
confirm.popover.dispose();
170172
}
171173
if (config.dismiss) {
172174
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/Table/Table.razor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,7 +1134,7 @@ private async Task ProcessFirstRender()
11341134

11351135
if (ColumnOrderCallback != null)
11361136
{
1137-
cols = ColumnOrderCallback(cols).ToList();
1137+
cols = [.. ColumnOrderCallback(cols)];
11381138
}
11391139

11401140
await ReloadColumnOrdersFromBrowserAsync(cols);
@@ -1271,7 +1271,7 @@ public List<TItem> Rows
12711271
// https://gitee.com/LongbowEnterprise/BootstrapBlazor/issues/I5JG5D
12721272
// 如果 QueryItems 无默认值
12731273
// 页面 OnInitializedAsync 二刷再 OnAfterRender 过程中导致 QueryItems 变量为空 ToList 报错
1274-
RowsCache ??= IsTree ? TreeRows.GetAllItems() : (Items ?? QueryItems).ToList();
1274+
RowsCache ??= IsTree ? TreeRows.GetAllItems() : [.. (Items ?? QueryItems)];
12751275
return RowsCache;
12761276
}
12771277
}

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)