Skip to content

Commit

Permalink
Fix #10575: 13.0.2 MenuItem oncomplete script is not called if no act…
Browse files Browse the repository at this point in the history
…ion is specified (#10775)
  • Loading branch information
melloware committed Oct 4, 2023
1 parent 9f729bb commit 3fbb4b5
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,9 @@ public interface MenuItem extends MenuElement, Confirmable {

Object getBadge();

String getUpdate();

String getProcess();

boolean isResetValues();
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,37 @@ public void decode(FacesContext context, UIComponent component) {
decodeDynamicMenuItem(context, component);
}

protected boolean isMenuItemLink(FacesContext context, UIComponent source, MenuItem menuitem) {
return LangUtils.isNotBlank(menuitem.getUrl()) || LangUtils.isNotBlank(menuitem.getOutcome());
}

protected boolean isMenuItemSubmitting(FacesContext context, UIComponent source, MenuItem menuitem) {
boolean submitting;

// #1 first check for assigned server side callbacks
submitting = menuitem.getFunction() != null || LangUtils.isNotBlank(menuitem.getCommand());
if (!submitting && menuitem instanceof UIMenuItem) {
submitting = ((UIMenuItem) menuitem).getActionExpression() != null
|| ((UIMenuItem) menuitem).getActionListeners().length > 0;
}

// 2# AJAX
if (!submitting && menuitem.isAjax()) {
submitting = menuitem.isResetValues()
|| LangUtils.isNotBlank(menuitem.getUpdate())
|| LangUtils.isNotBlank(menuitem.getProcess());
}

return submitting;
}

protected void encodeOnClick(FacesContext context, UIComponent source, MenuItem menuitem) throws IOException {
ResponseWriter writer = context.getResponseWriter();
setConfirmationScript(context, menuitem);

String onclick = menuitem.getOnclick();
boolean isLink = LangUtils.isNotBlank(menuitem.getUrl()) || LangUtils.isNotBlank(menuitem.getOutcome());
boolean isCommand = LangUtils.isNotBlank(menuitem.getCommand());
if (!isCommand && menuitem instanceof UIMenuItem) {
UIMenuItem uim = (UIMenuItem) menuitem;
isCommand = uim.getActionExpression() != null || uim.getActionListeners().length > 0;
}
boolean isLink = isMenuItemLink(context, source, menuitem);
boolean isSubmitting = isMenuItemSubmitting(context, source, menuitem);

//GET
if (isLink) {
Expand All @@ -83,7 +103,7 @@ protected void encodeOnClick(FacesContext context, UIComponent source, MenuItem
writer.writeAttribute("href", "#", null);
}

if (isCommand) {
if (isSubmitting) {
String menuClientId = source.getClientId(context);
UIForm form = ComponentTraversalUtils.closestForm(source);
if (form == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* The MIT License
*
* Copyright (c) 2009-2023 PrimeTek Informatics
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.primefaces.renderkit;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.primefaces.model.menu.DefaultMenuItem;

public class MenuItemAwareRendererTest extends MenuItemAwareRenderer {

@Test
public void testIsMenuItemSubmitting() {
DefaultMenuItem item = null;

item = new DefaultMenuItem();
item.setOncomplete("test");
Assertions.assertFalse(isMenuItemSubmitting(null, null, item));

item = new DefaultMenuItem();
item.setCommand("#{test.test()}");
Assertions.assertTrue(isMenuItemSubmitting(null, null, item));

item = new DefaultMenuItem();
item.setFunction((menuitem) -> {
return "test";
});
Assertions.assertTrue(isMenuItemSubmitting(null, null, item));

item = new DefaultMenuItem();
item.setUpdate("test");
Assertions.assertTrue(isMenuItemSubmitting(null, null, item));

item = new DefaultMenuItem();
item.setProcess("test");
Assertions.assertTrue(isMenuItemSubmitting(null, null, item));

item = new DefaultMenuItem();
item.setProcess("test");
item.setAjax(false);
Assertions.assertFalse(isMenuItemSubmitting(null, null, item));

item = new DefaultMenuItem();
item.setResetValues(true);
item.setAjax(false);
Assertions.assertFalse(isMenuItemSubmitting(null, null, item));
}

}

0 comments on commit 3fbb4b5

Please sign in to comment.