Hi thanks for help! With your explanations and MenuBehavior example I made it
work. Now one last thing is to recognize which link was chosen for context
menu. I see this documentation about callback:
(function) callback
    Specifies the default callback to be used in case an item does not
expose its own callback.
    The default callback behaves just like item.callback.
    Example: {callback: callback: function(key, opt){ alert("Clicked on " +
key + " on element " + opt.$trigger.attr("id")); }}

Is opt.$trigger.attr("id") what I want? How does this work out? I debugged
my select event for RequestCycleUtils.getQueryParameterValue("options") but
it returns nothing.

Here is my current working version of context menu:

public class ContextMenuBehavior extends JQueryBehavior implements
IJQueryAjaxAware {

    private static final long serialVersionUID = 1L;

    private String selector;

    private List<MenuItem> menuItems;

    private Map<String, MenuItem> menuItemsMap = new HashMap<String,

    private JQueryAjaxBehavior onSelectBehavior;

    public ContextMenuBehavior(String selector, List<MenuItem> menuItems) {
        this.selector = selector;
        this.menuItems = menuItems;
        for (MenuItem menuItem : menuItems) {
            menuItemsMap.put(menuItem.getId(), menuItem);

        add(new JavaScriptResourceReference(ContextMenuBehavior.class,
        add(new JavaScriptResourceReference(ContextMenuBehavior.class,
        add(new CssResourceReference(ContextMenuBehavior.class,

    // Methods //
    public void bind(Component component) {

        component.add(this.onSelectBehavior = this.newOnSelectBehavior());

    // Events //
    public void onConfigure(Component component) {


    protected String $() {
        // build menu items for jquery
        StringBuilder items = new StringBuilder("items: {");
        int nbOfMenuItems = menuItems.size();
        for (int i = 0; i < nbOfMenuItems; i++) {
            MenuItem menuItem = menuItems.get(i);
            items.append("'").append(menuItem.getId()).append("': {name:
                    .append("', icon:
            if (i < nbOfMenuItems - 1) {
        return String.format("$(function(){$.contextMenu({selector: '%s',
callback: %s, %s});});", selector,
                onSelectBehavior.getCallbackFunction(), items.toString());

    public void onAjax(AjaxRequestTarget target, JQueryEvent event) {
        if (event instanceof SelectEvent) {
            IMenuItem item = this.menuItemsMap.get(((SelectEvent)

            if (item != null) {

    protected JQueryAjaxBehavior newOnSelectBehavior() {
        return new JQueryAjaxBehavior(this) {

            private static final long serialVersionUID = 1L;

            protected CallbackParameter[] getCallbackParameters() {
                return new CallbackParameter[] {
CallbackParameter.explicit("key"), CallbackParameter.context("options") };

            protected JQueryEvent newEvent() {
                return new SelectEvent();

    protected static class SelectEvent extends JQueryEvent {
        private final String key;

        public SelectEvent() {
            this.key =

        public String getKey() {
            return this.key;

View this message in context: 
Sent from the Users forum mailing list archive at Nabble.com.

To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org

Reply via email to