|
1 | 1 | import Data from "../../modules/data.js" |
2 | 2 | import EventHandler from "../../modules/event-handler.js" |
3 | 3 | import { createPopper, computePosition } from '../../modules/floating-ui.js' |
| 4 | +import { registerBootstrapBlazorModule } from "../../modules/utility.js" |
4 | 5 |
|
5 | 6 | export function init(id) { |
6 | 7 | const el = document.getElementById(id) |
7 | 8 |
|
8 | 9 | if (el) { |
9 | | - window.bb = window.bb || {}; |
10 | | - if (bb.cancelContextMenuHandler === void 0) { |
11 | | - bb.contextMenus = [] |
12 | | - bb.cancelContextMenuHandler = e => { |
13 | | - const menu = document.querySelector('.bb-cm.show') |
14 | | - if (menu) { |
15 | | - const menuId = menu.getAttribute('id') |
16 | | - const cm = Data.get(menuId) |
17 | | - if (cm.popper) { |
18 | | - cm.popper() |
19 | | - } |
20 | | - |
21 | | - menu.classList.remove('show') |
22 | | - const zone = getZone(menu) |
23 | | - if (zone) { |
24 | | - zone.appendChild(menu) |
| 10 | + const cm = { el, zone: getZone(el) }; |
| 11 | + Data.set(id, cm); |
| 12 | + |
| 13 | + registerBootstrapBlazorModule("ContextMenu", id, context => { |
| 14 | + if (context.cancelContextMenuHandler === void 0) { |
| 15 | + context.cancelContextMenuHandler = e => { |
| 16 | + const menu = document.querySelector('.bb-cm.show') |
| 17 | + if (menu) { |
| 18 | + const menuId = menu.getAttribute('id') |
| 19 | + const cm = Data.get(menuId) |
| 20 | + if (cm.popper) { |
| 21 | + cm.popper() |
| 22 | + } |
| 23 | + |
| 24 | + menu.classList.remove('show') |
| 25 | + const zone = getZone(menu) |
| 26 | + if (zone) { |
| 27 | + zone.appendChild(menu) |
| 28 | + } |
25 | 29 | } |
26 | 30 | } |
27 | 31 | } |
28 | | - EventHandler.on(document, 'click', bb.cancelContextMenuHandler) |
29 | | - EventHandler.on(document, 'contextmenu', bb.cancelContextMenuHandler) |
30 | | - } |
31 | | - bb.contextMenus.push(el) |
32 | | - |
33 | | - const cm = { el, zone: getZone(el) } |
34 | | - Data.set(id, cm) |
| 32 | + EventHandler.on(document, 'click', context.cancelContextMenuHandler) |
| 33 | + EventHandler.on(document, 'contextmenu', context.cancelContextMenuHandler) |
| 34 | + }) |
35 | 35 | } |
36 | 36 | } |
37 | 37 |
|
@@ -64,19 +64,11 @@ export function dispose(id) { |
64 | 64 | cm.popper() |
65 | 65 | } |
66 | 66 |
|
67 | | - window.bb = window.bb || { contextMenus: [] } |
68 | | - const index = bb.contextMenus.indexOf(el) |
69 | | - if (index > -1) { |
70 | | - bb.contextMenus.splice(index, 1) |
71 | | - } |
72 | | - |
73 | | - if (bb.contextMenus.length === 0) { |
74 | | - if (bb.cancelContextMenuHandler) { |
75 | | - EventHandler.off(document, 'click', bb.cancelContextMenuHandler) |
76 | | - EventHandler.off(document, 'contextmenu', bb.cancelContextMenuHandler) |
77 | | - } |
78 | | - delete bb.cancelContextMenuHandler; |
79 | | - } |
| 67 | + const { ContextMenu } = window.BootstrapBlazor; |
| 68 | + ContextMenu.dispose(id, context => { |
| 69 | + EventHandler.off(document, 'click', context.cancelContextMenuHandler) |
| 70 | + EventHandler.off(document, 'contextmenu', context.cancelContextMenuHandler) |
| 71 | + }); |
80 | 72 | } |
81 | 73 | } |
82 | 74 |
|
|
0 commit comments