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,
MenuItem>();

    private JQueryAjaxBehavior onSelectBehavior;

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

        add(new JavaScriptResourceReference(ContextMenuBehavior.class,
"jquery.ui.position.js"));
        add(new JavaScriptResourceReference(ContextMenuBehavior.class,
"jquery.contextMenu.js"));
        add(new CssResourceReference(ContextMenuBehavior.class,
"jquery.contextMenu.css"));
    }

    // Methods //
    @Override
    public void bind(Component component) {
        super.bind(component);

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

    // Events //
    @Override
    public void onConfigure(Component component) {
        super.onConfigure(component);

        this.setOption("select",
this.onSelectBehavior.getCallbackFunction());
    }

    @Override
    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(menuItem.getTitle().getObject())
                    .append("', icon:
'").append(menuItem.getIcon()).append("'}");
            if (i < nbOfMenuItems - 1) {
                items.append(",");
            }
        }
        items.append("}");
        return String.format("$(function(){$.contextMenu({selector: '%s',
callback: %s, %s});});", selector,
                onSelectBehavior.getCallbackFunction(), items.toString());
    }

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

            if (item != null) {
                item.onClick(target);
            }
        }
    }

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

            private static final long serialVersionUID = 1L;

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

            @Override
            protected JQueryEvent newEvent() {
                return new SelectEvent();
            }
        };
    }

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

        public SelectEvent() {
            super();
            this.key =
RequestCycleUtils.getQueryParameterValue("key").toString();
        }

        public String getKey() {
            return this.key;
        }
    }
}



--
View this message in context: 
http://apache-wicket.1842946.n4.nabble.com/Wicket-context-menu-component-tp4659306p4659381.html
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