Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
if (context is ContextMenuDivider)
{
<Divider></Divider>
continue;
}
else if (context is ContextMenuItem item)
{
Expand Down
64 changes: 28 additions & 36 deletions src/BootstrapBlazor/Components/ContextMenu/ContextMenu.razor.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
import Data from "../../modules/data.js"
import EventHandler from "../../modules/event-handler.js"
import { createPopper, computePosition } from '../../modules/floating-ui.js'
import { registerBootstrapBlazorModule } from "../../modules/utility.js"

export function init(id) {
const el = document.getElementById(id)

if (el) {
window.bb = window.bb || {};
if (bb.cancelContextMenuHandler === void 0) {
bb.contextMenus = []
bb.cancelContextMenuHandler = e => {
const menu = document.querySelector('.bb-cm.show')
if (menu) {
const menuId = menu.getAttribute('id')
const cm = Data.get(menuId)
if (cm.popper) {
cm.popper()
}

menu.classList.remove('show')
const zone = getZone(menu)
if (zone) {
zone.appendChild(menu)
const cm = { el, zone: getZone(el) };
Data.set(id, cm);

registerBootstrapBlazorModule("ContextMenu", id, context => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (complexity): Consider extracting the event registration and disposal logic into helper functions to reduce nested callbacks.

You can reduce the nested callback complexity by extracting the event registration/disposal logic into helper functions. This will keep your module registration intact but flatten the structure of your code, making it easier to follow.

For example:

// Define helpers to attach and detach events

function setupContextMenuEvents(context) {
  if (context.cancelContextMenuHandler === void 0) {
    context.cancelContextMenuHandler = e => {
      const menu = document.querySelector('.bb-cm.show');
      if (menu) {
        const menuId = menu.getAttribute('id');
        const cm = Data.get(menuId);
        if (cm && cm.popper) {
          cm.popper();
        }
        menu.classList.remove('show');
        const zone = getZone(menu);
        if (zone) {
          zone.appendChild(menu);
        }
      }
    };
  }
  EventHandler.on(document, 'click', context.cancelContextMenuHandler);
  EventHandler.on(document, 'contextmenu', context.cancelContextMenuHandler);
}

function disposeContextMenuEvents(context) {
  EventHandler.off(document, 'click', context.cancelContextMenuHandler);
  EventHandler.off(document, 'contextmenu', context.cancelContextMenuHandler);
}

Then, modify your module registration and disposal calls as follows:

// In init using the module registration call
registerBootstrapBlazorModule("ContextMenu", id, context => {
  setupContextMenuEvents(context);
});
// In cleanup, dispose the events with a similar pattern
const { ContextMenu } = window.BootstrapBlazor;
ContextMenu.dispose(id, context => {
  disposeContextMenuEvents(context);
});

This refactoring keeps the same functionality but reduces the depth of nesting by separating concerns into dedicated functions.

if (context.cancelContextMenuHandler === void 0) {
context.cancelContextMenuHandler = e => {
const menu = document.querySelector('.bb-cm.show')
if (menu) {
const menuId = menu.getAttribute('id')
const cm = Data.get(menuId)
if (cm.popper) {
cm.popper()
}

menu.classList.remove('show')
const zone = getZone(menu)
if (zone) {
zone.appendChild(menu)
}
}
}
}
EventHandler.on(document, 'click', bb.cancelContextMenuHandler)
EventHandler.on(document, 'contextmenu', bb.cancelContextMenuHandler)
}
bb.contextMenus.push(el)

const cm = { el, zone: getZone(el) }
Data.set(id, cm)
EventHandler.on(document, 'click', context.cancelContextMenuHandler)
EventHandler.on(document, 'contextmenu', context.cancelContextMenuHandler)
})
}
}

Expand Down Expand Up @@ -64,19 +64,11 @@ export function dispose(id) {
cm.popper()
}

window.bb = window.bb || { contextMenus: [] }
const index = bb.contextMenus.indexOf(el)
if (index > -1) {
bb.contextMenus.splice(index, 1)
}

if (bb.contextMenus.length === 0) {
if (bb.cancelContextMenuHandler) {
EventHandler.off(document, 'click', bb.cancelContextMenuHandler)
EventHandler.off(document, 'contextmenu', bb.cancelContextMenuHandler)
}
delete bb.cancelContextMenuHandler;
}
const { ContextMenu } = window.BootstrapBlazor;
ContextMenu.dispose(id, context => {
EventHandler.off(document, 'click', context.cancelContextMenuHandler)
EventHandler.off(document, 'contextmenu', context.cancelContextMenuHandler)
});
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/BootstrapBlazor/wwwroot/modules/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ export function registerBootstrapBlazorModule(name, identifier, callback) {
}
if (this._init === false) {
this._init = true;
cb();
cb(this);
}
return this;
},
Expand All @@ -832,7 +832,7 @@ export function registerBootstrapBlazorModule(name, identifier, callback) {
}
if (this._items.length === 0 && cb) {
this._init = false;
cb();
cb(this);
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion test/UnitTest/Components/LayoutTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ public void Authorized_Ok()
pb.Add(a => a.AdditionalAssemblies, new Assembly[] { GetType().Assembly });
pb.Add(a => a.OnAuthorizing, url => Task.FromResult(true));
});
cut.Contains("<section class=\"layout\" style=\"--bb-layout-header-height: 0px; --bb-layout-footer-height: 0px;\"><main class=\"layout-main\"></main></section>");
cut.MarkupMatches("<section id:ignore class=\"layout\" style=\"--bb-layout-header-height: 0px; --bb-layout-footer-height: 0px;\"><main class=\"layout-main\"></main></section>");
Context.DisposeComponents();
}
}
1 change: 0 additions & 1 deletion test/UnitTest/Services/ThrottleTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ await Assert.ThrowsAnyAsync<InvalidOperationException>(() => dispatcher.Throttle
{
count++;
});
Assert.Equal(2, count);
}

[Fact]
Expand Down