diff --git a/src/BootstrapBlazor/Components/AutoComplete/AutoComplete.razor.js b/src/BootstrapBlazor/Components/AutoComplete/AutoComplete.razor.js index 6aaa4aee02e..cfca16f9777 100644 --- a/src/BootstrapBlazor/Components/AutoComplete/AutoComplete.razor.js +++ b/src/BootstrapBlazor/Components/AutoComplete/AutoComplete.razor.js @@ -1,14 +1,10 @@ -import { debounce } from "../../modules/utility.js" +import { debounce, registerBootstrapBlazorModule } from "../../modules/utility.js" import { handleKeyUp, select, selectAllByFocus, selectAllByEnter } from "../Input/BootstrapInput.razor.js" import Data from "../../modules/data.js" import EventHandler from "../../modules/event-handler.js" import Input from "../../modules/input.js" import Popover from "../../modules/base-popover.js" -if (window.BootstrapBlazor === void 0) { - window.BootstrapBlazor = {}; -} - export function init(id, invoke) { const el = document.getElementById(id) const menu = el.querySelector('.dropdown-menu') @@ -76,32 +72,29 @@ export function init(id, invoke) { filterCallback(v); }); - if (window.BootstrapBlazor.AutoComplete === void 0) { - window.BootstrapBlazor.AutoComplete = { - hooked: false, - registerCloseDropdownHandler: function () { - if (this.hooked === false) { - this.hooked = true; - - EventHandler.on(document, 'click', e => { - [...document.querySelectorAll('.auto-complete.show')].forEach(a => { - const ac = e.target.closest('.auto-complete'); - if (ac === a) { - return; - } - - const el = a.querySelector('[data-bs-toggle="bb.dropdown"]'); - if (el === null) { - a.classList.remove('show'); - } - }); + const module = registerBootstrapBlazorModule('AutoComplete', { + hooked: false, + registerCloseDropdownHandler: function () { + if (this.hooked === false) { + this.hooked = true; + + EventHandler.on(document, 'click', e => { + [...document.querySelectorAll('.auto-complete.show')].forEach(a => { + const ac = e.target.closest('.auto-complete'); + if (ac === a) { + return; + } + + const el = a.querySelector('[data-bs-toggle="bb.dropdown"]'); + if (el === null) { + a.classList.remove('show'); + } }); - } + }); } } - } - - window.BootstrapBlazor.AutoComplete.registerCloseDropdownHandler(); + }); + module.registerCloseDropdownHandler(); } const handlerKeyup = (ac, e) => { diff --git a/src/BootstrapBlazor/wwwroot/modules/utility.js b/src/BootstrapBlazor/wwwroot/modules/utility.js index ff76dbff688..7e203f6e7fe 100644 --- a/src/BootstrapBlazor/wwwroot/modules/utility.js +++ b/src/BootstrapBlazor/wwwroot/modules/utility.js @@ -571,28 +571,23 @@ const hackPopover = (popover, css) => { } const hackTooltip = function () { - window.BootstrapBlazor ??= {}; - - if (window.BootstrapBlazor.Tooltip === void 0) { - window.BootstrapBlazor.Tooltip = { - hooked: false, - hackDispose: function () { - if (this.hooked === false) { - this.hooked = true; - - const originalDispose = bootstrap.Tooltip.prototype.dispose; - bootstrap.Tooltip.prototype.dispose = function () { - originalDispose.call(this); - // fix https://github.com/twbs/bootstrap/issues/37474 - this._activeTrigger = {}; - this._element = document.createElement('noscript'); // placeholder with no behavior - } + const tooltip = registerBootstrapBlazorModule('Tooltip', { + hooked: false, + hackDispose: function () { + if (this.hooked === false) { + this.hooked = true; + + const originalDispose = bootstrap.Tooltip.prototype.dispose; + bootstrap.Tooltip.prototype.dispose = function () { + originalDispose.call(this); + // fix https://github.com/twbs/bootstrap/issues/37474 + this._activeTrigger = {}; + this._element = document.createElement('noscript'); // placeholder with no behavior } } } - } - - window.BootstrapBlazor.Tooltip.hackDispose(); + }); + tooltip.hackDispose(); } const setIndeterminate = (object, state) => { @@ -802,13 +797,17 @@ export function switchTheme(theme, x = 0, y = 0, sync = true) { } } -const deepMerge = (obj1, obj2) => { - for (let key in obj2) { +const deepMerge = (obj1, obj2, skipNull = true) => { + for (const key in obj2) { if (obj2.hasOwnProperty(key)) { if (obj2[key] instanceof Object && obj1[key] instanceof Object) { obj1[key] = deepMerge(obj1[key], obj2[key]); } else { + const value = obj2[key]; + if (skipNull && value === null) { + continue; + } obj1[key] = obj2[key]; } } @@ -816,6 +815,12 @@ const deepMerge = (obj1, obj2) => { return obj1; } +export function registerBootstrapBlazorModule(name, module) { + window.BootstrapBlazor ??= {}; + window.BootstrapBlazor[name] ??= deepMerge(window.BootstrapBlazor[name] ?? {}, module); + return window.BootstrapBlazor[name]; +} + export function setTitle(title) { document.title = title; }