예제 #1
0
        protected void OnClickInternal(UIMouseEventArgs e)
        {
            if (!Enabled)
            {
                return;
            }

            var menuId            = RegisteredFunction.Invoke <string>("BlazorContextMenu.MenuItem.GetMenuId", MenuItemElement);
            var menu              = BlazorContextMenuHandler.GetMenu(menuId);
            var contextMenuTarget = menu.GetTarget();
            var args              = new MenuItemClickEventArgs(e, menuId, contextMenuTarget, MenuItemElement, this);

            if (Click != null)
            {
                Click(args);
                if (!args.IsCanceled)
                {
                    BlazorContextMenuHandler.HideMenu(menuId);
                }
            }
            else if (ClickAsync != null)
            {
                ClickAsync(args).ContinueWith((t) =>
                {
                    if (!args.IsCanceled)
                    {
                        BlazorContextMenuHandler.HideMenu(menuId);
                    }
                });
            }
        }
예제 #2
0
        protected override void OnAfterRender()
        {
            if (EnabledHandler == null && EnabledHandlerAsync == null && VisibleHandler == null && VisibleHandlerAsync == null)
            {
                return;
            }

            var menuId            = RegisteredFunction.Invoke <string>("BlazorContextMenu.MenuItem.GetMenuId", MenuItemElement);
            var menu              = BlazorContextMenuHandler.GetMenu(menuId);
            var contextMenuTarget = menu.GetTarget();

            //menu is not showing. TODO: find a better way to figure this out
            if (contextMenuTarget == null)
            {
                return;
            }

            //Hacky but works. TODO: Improve this code when this stuff is supported in Blazor
            Task.Run(async() =>
            {
                var oldEnabledValue = Enabled;
                var oldVisibleValue = Visible;

                var args = new MenuItemHandlerArgs(menuId, contextMenuTarget, MenuItemElement, this);
                if (EnabledHandler != null)
                {
                    Enabled = EnabledHandler(args);
                }
                else if (EnabledHandlerAsync != null)
                {
                    Enabled = await EnabledHandlerAsync(args);
                }

                if (VisibleHandler != null)
                {
                    Visible = VisibleHandler(args);
                }
                else if (VisibleHandlerAsync != null)
                {
                    Visible = await VisibleHandlerAsync(args);
                }

                if (oldEnabledValue != Enabled || oldVisibleValue != Visible)
                {
                    StateHasChanged();
                }
            });
        }