sebfz1 / wicket-jquery-ui

jQuery UI & Kendo UI integration in Wicket
http://www.7thweb.net/wicket-jquery-ui/
Other
92 stars 58 forks source link

Dynamically change 'enabled' state of ContextMenu menu items #325

Closed nautavis closed 3 years ago

nautavis commented 3 years ago

Hallo all,

with a custom JQueryAjaxBehavior used in newOnOpenAjaxBehavior I am detecting, on which event a user clicked in the Scheduler and pass this information to the onOpen method. Here I would like to enable/disable certain IMenuItem entries, based on the clicked entry, right before the context menu will be shown. The model value is updated and isEnabled is called, but the menu items doesn't change their state in GUI. Calling reload(target) here is not an option because it re-renders the whole ContextMenu.

What would be the best approach for changing the enabled state at onOpen phase just before showing the items to the user?

sebfz1 commented 3 years ago

Hi,

The problem here is that you pass the information in onOpen, but it is too late. IMO, the best option is to send the information when you get it and pass it to the ContextMenu using the event bus and a customized payload.

When you get the info (considering you know the contextMenu, other you can use bubble or breadth):

@Override
public void onClick(AjaxRequestTarget target) {
    BroadcastUtils.exact(contextMenu, new MenuItemPayload(target, "my title"));
}

In the context menu, receive the event:

@Override
public void onEvent(IEvent<?> event) {
    super.onEvent(event);

    if (event.getPayload() instanceof MenuItemPayload) {
        MenuItemPayload payload = (MenuItemPayload) event.getPayload();

        MenuItem item = (MenuItem) this.getItemList().get(1);
        item.setTitle(Model.of(payload.getTitle()));

        this.reload(payload.getHandler());
    }
}

The payload would look like:

static class MenuItemPayload extends HandlerPayload {

    private String title;

    public MenuItemPayload(IPartialPageRequestHandler handler, String title) {
        super(handler);
        this.title = title;
    }

    public String getTitle() {
        return title;
    }
}

In the future please either the Google Forum (or even the Wicket ML) for questions. Github issues is for... issues.

nautavis commented 3 years ago

Works perfectly, thank you very much. I am sending the broadcast now from a preceding mousedown handler, this way it is possible to set the menu item states depending on whether the mouse click is coming from empty space in scheduler or an actual event.

Sorry for posting this question here instead of the forum or mailing list.