package com.evolveum.midpoint.web.component.menu.cog;

import com.evolveum.midpoint.web.component.util.VisibleEnableBehaviour;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.AbstractLink;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;

/**
 * @author lazyman
 */
public class MenuLinkPanel extends Panel {

    private static String ID_MENU_ITEM_LINK = "menuItemLink";
    private static String ID_MENU_ITEM_LABEL = "menuItemLabel";

    public MenuLinkPanel(String id, IModel<InlineMenuItem> item) {
        super(id);

        initLayout(item);
    }

    private void initLayout(IModel<InlineMenuItem> item) {
        final InlineMenuItem dto = item.getObject();

        AbstractLink a;
        if (dto.isSubmit()) {
            a = new AjaxSubmitLink(ID_MENU_ITEM_LINK) {

                @Override
                protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
                    MenuLinkPanel.this.onSubmit(target, form, dto.getAction());
                }

                @Override
                protected void onError(AjaxRequestTarget target, Form<?> form) {
                    MenuLinkPanel.this.onError(target, form, dto.getAction());
                }
            };
        } else {
            a = new AjaxLink(ID_MENU_ITEM_LINK) {

                @Override
                public void onClick(AjaxRequestTarget target) {
                    MenuLinkPanel.this.onClick(target, dto.getAction());
                }
            };
        }
        add(a);

        a.add(new VisibleEnableBehaviour() {

            @Override
            public boolean isVisible() {
                if (dto.getAction() == null) {
                    return false;
                }
                return true;
            }
        });

        Label span = new Label(ID_MENU_ITEM_LABEL, dto.getLabel());
        span.setRenderBodyOnly(true);
        a.add(span);
    }

    protected void onSubmit(AjaxRequestTarget target, Form<?> form, InlineMenuItemAction action) {
        if (action != null) {
            action.onSubmit(target, form);
        }
    }

    protected void onError(AjaxRequestTarget target, Form<?> form, InlineMenuItemAction action) {
        if (action != null) {
            action.onError(target, form);
        }
    }

    protected void onClick(AjaxRequestTarget target, InlineMenuItemAction action) {
        if (action != null) {
            action.onClick(target);
        }
    }
}

@@ -169,16 +169,80 @@
 Target;%0A
+import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;%0A
 import o
@@ -1549,251 +1549,835 @@
   %7D%0A
-            %7D;%0A        %7D else %7B%0A            a = new AjaxLink(ID_MENU_ITEM_LINK) %7B%0A%0A                @Override%0A                public void onClick(AjaxRequestTarget target) %7B%0A                    MenuLinkPanel.this.onClick(target, dto.getAction()
+%0A                @Override%0A                protected void updateAjaxAttributes(AjaxRequestAttributes attributes) %7B%0A                    super.updateAjaxAttributes(attributes);%0A                    attributes.setEventPropagation(AjaxRequestAttributes.EventPropagation.BUBBLE);%0A                %7D%0A            %7D;%0A        %7D else %7B%0A            a = new AjaxLink(ID_MENU_ITEM_LINK) %7B%0A%0A                @Override%0A                public void onClick(AjaxRequestTarget target) %7B%0A                    MenuLinkPanel.this.onClick(target, dto.getAction());%0A                %7D%0A%0A                @Override%0A                protected void updateAjaxAttributes(AjaxRequestAttributes attributes) %7B%0A                    super.updateAjaxAttributes(attributes);%0A                    attributes.setEventPropagation(AjaxRequestAttributes.EventPropagation.BUBBLE
 );%0A 

fix for menu toggle on org. tree page which was not working on collapse/expand all. MID-1784