Skip to content

Commit

Permalink
WICKET-7140 fix for textarea submit on enter (now ctrl+enter) (#1076)
Browse files Browse the repository at this point in the history
* WICKET-7140 fix for textarea submit on enter if form.defaultButton is set (now submits on CTRL+enter for textarea)

* WICKET-7140 indentation fix

* WICKET-7140 improvement for textarea and contenteditable

* WICKET-7140 form default submit js moved out to separate file, default handling for textarea/contenteditable is NONE (no action)

* WICKET-7140 build fix

* upgraded resource optimizer javascript language level from ecmascript_3 to ecmascript_2015

* WICKET-7140 changed var/let variable definitions to const

* WICKET-7140 minor improvements in examples

* WICKET-7140 simplified JS and embedded, textarea/contenteditable not handled at all

* WICKET-7140 fixed tests

* WICKET-7140 JS language version moved back to ES 3 (default)
  • Loading branch information
1azyman authored Jan 17, 2025
1 parent c91e34a commit 69f1eb7
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<script type="text/javascript">
/*<![CDATA[*/
Wicket.Event.add(window, "domready", function(event) {
Wicket.Event.add('form2', 'keypress', function(event) { var b = document.getElementById('default1');if (window.getComputedStyle(b).visibility === 'hidden') return;if (event.which == 13) {event.stopPropagation();event.preventDefault();if (b != null && b.onclick != null && typeof (b.onclick) != 'undefined') {var r = Wicket.bind(b.onclick, b)();if (r != false) b.click();} else {b.click();}return false;};});;
Wicket.Event.add('form2', 'keypress', function(event) { if (event.target.tagName.toLowerCase() !== 'input' || event.which != 13) return;var b = document.getElementById('default1');if (window.getComputedStyle(b).visibility === 'hidden') return;event.stopPropagation();event.preventDefault();if (b != null && b.onclick != null && typeof (b.onclick) != 'undefined') {var r = Wicket.bind(b.onclick, b)();if (r != false) b.click();} else {b.click();}return false;;});;
Wicket.Event.publish(Wicket.Event.Topic.AJAX_HANDLERS_BOUND);
;});
/*]]>*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

<input type="text" wicket:id="parentInput"/>

<textarea wicket:id="parentTextarea"></textarea>

<button wicket:id="parentSubmit"></button>

<form wicket:id="childForm">
<input type="text" wicket:id="childInput"/>

<textarea wicket:id="childTextarea"></textarea>

<button wicket:id="childSubmit"></button>
</form>
</form>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class FormHierarchyDefaultButtonTestPage extends WebPage {
public final Button childSubmit;
public final TextField<?> parentInput;
public final TextField<?> childInput;
public final TextArea<?> parentTextarea;
public final TextArea<?> childTextarea;

/**
* Construct.
Expand All @@ -44,6 +46,9 @@ public FormHierarchyDefaultButtonTestPage() {
parentInput = new TextField<>("parentInput");
parentForm.add(parentInput);

parentTextarea = new TextArea<>("parentTextarea");
parentForm.add(parentTextarea);

parentSubmit = new Button("parentSubmit");
parentSubmit.add(new AjaxFormSubmitBehavior(parentForm, "click") {

Expand All @@ -62,6 +67,9 @@ protected void onSubmit(AjaxRequestTarget target) {
childInput = new TextField<>("childInput");
childForm.add(childInput);

childTextarea = new TextArea<>("childTextarea");
childForm.add(childTextarea);

childSubmit = new Button("childSubmit");
childForm.setDefaultButton(childSubmit);
childForm.add(childSubmit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
/*<![CDATA[*/
Wicket.Event.add(window, "domready", function(event) {
Wicket.Ajax.ajax({"u":"./org.apache.wicket.markup.html.form.FormHierarchyDefaultButtonTestPage?0-1.0-parentForm-parentSubmit","m":"POST","c":"parentSubmit2","f":"parentForm1","sc":"parentSubmit","e":"click"});;
Wicket.Event.add('childForm4', 'keypress', function(event) { var b = document.getElementById('childSubmit3');if (window.getComputedStyle(b).visibility === 'hidden') return;if (event.which == 13) {event.stopPropagation();event.preventDefault();if (b != null && b.onclick != null && typeof (b.onclick) != 'undefined') {var r = Wicket.bind(b.onclick, b)();if (r != false) b.click();} else {b.click();}return false;};});;
Wicket.Event.add('parentForm1', 'keypress', function(event) { var b = document.getElementById('parentSubmit2');if (window.getComputedStyle(b).visibility === 'hidden') return;if (event.which == 13) {event.stopPropagation();event.preventDefault();if (b != null && b.onclick != null && typeof (b.onclick) != 'undefined') {var r = Wicket.bind(b.onclick, b)();if (r != false) b.click();} else {b.click();}return false;};});;
Wicket.Event.add('childForm4', 'keypress', function(event) { if (event.target.tagName.toLowerCase() !== 'input' || event.which != 13) return;var b = document.getElementById('childSubmit3');if (window.getComputedStyle(b).visibility === 'hidden') return;event.stopPropagation();event.preventDefault();if (b != null && b.onclick != null && typeof (b.onclick) != 'undefined') {var r = Wicket.bind(b.onclick, b)();if (r != false) b.click();} else {b.click();}return false;;});;
Wicket.Event.add('parentForm1', 'keypress', function(event) { if (event.target.tagName.toLowerCase() !== 'input' || event.which != 13) return;var b = document.getElementById('parentSubmit2');if (window.getComputedStyle(b).visibility === 'hidden') return;event.stopPropagation();event.preventDefault();if (b != null && b.onclick != null && typeof (b.onclick) != 'undefined') {var r = Wicket.bind(b.onclick, b)();if (r != false) b.click();} else {b.click();}return false;;});;
Wicket.Event.publish(Wicket.Event.Topic.AJAX_HANDLERS_BOUND);
;});
/*]]>*/
Expand All @@ -26,11 +26,15 @@

<input type="text" wicket:id="parentInput" value="" name="parentInput"/>

<textarea wicket:id="parentTextarea" name="parentTextarea"></textarea>

<button wicket:id="parentSubmit" name="parentSubmit" id="parentSubmit2"></button>

<div wicket:id="childForm" id="childForm4">
<input type="text" wicket:id="childInput" value="" name="childForm:childInput"/>

<textarea wicket:id="childTextarea" name="childForm:childTextarea"></textarea>

<button wicket:id="childSubmit" name="childForm:childSubmit" id="childSubmit3"></button>
</div>
</form>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.apache.wicket.WicketRuntimeException;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.behavior.Behavior;
import org.apache.wicket.core.request.handler.IPartialPageRequestHandler;
import org.apache.wicket.core.util.string.CssUtils;
import org.apache.wicket.event.IEvent;
import org.apache.wicket.markup.ComponentTag;
Expand Down Expand Up @@ -1273,9 +1272,9 @@ protected void addDefaultSubmitButtonHandler(IHeaderResponse headerResponse)
String submitId = component.getMarkupId();

AppendingStringBuffer script = new AppendingStringBuffer();
script.append("var b = document.getElementById('").append(submitId).append("');");
script.append("if (event.target.tagName.toLowerCase() !== 'input' || event.which != 13) return;");
script.append("var b = document.getElementById('" + submitId + "');");
script.append("if (window.getComputedStyle(b).visibility === 'hidden') return;");
script.append("if (event.which == 13) {");
script.append("event.stopPropagation();");
script.append("event.preventDefault();");
script.append("if (b != null && b.onclick != null && typeof (b.onclick) != 'undefined') {");
Expand All @@ -1285,7 +1284,6 @@ protected void addDefaultSubmitButtonHandler(IHeaderResponse headerResponse)
script.append("b.click();");
script.append("}");
script.append("return false;");
script.append("}");

headerResponse.render(OnEventHeaderItem.forMarkupId(getMarkupId(), "keypress", script.toString()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,6 @@
</fieldset>
</form>

<div id="feedbackPanel">
<span wicket:id="feedback"/>
</div>

<h2 class="example-title"><wicket:message key="FormInput.formHierarchyExample"/></h2>

<form wicket:id="parentForm" class="parent-form">
Expand All @@ -107,6 +103,10 @@ <h3><wicket:message key="FormInput.parentForm"/></h3>
<div class="gap-1">
<label for="parentText"><wicket:message key="FormInput.parentTextLabel"/></label>
<input type="text" wicket:id="parentText" id="parentText"/>

<label for="parentTextarea"><wicket:message key="FormInput.parentTextareaLabel"/></label>
<textarea wicket:id="parentTextarea" id="parentTextarea"></textarea>

<input type="submit" wicket:id="parentSubmit" wicket:message="value:FormInput.parentSubmit"/>
</div>

Expand All @@ -116,6 +116,10 @@ <h3><wicket:message key="FormInput.childForm"/></h3>
<div class="gap-1">
<label for="childText"><wicket:message key="FormInput.childTextLabel"/></label>
<input type="text" wicket:id="childText" id="childText"/>

<label for="childTextarea"><wicket:message key="FormInput.childTextareaLabel"/></label>
<textarea wicket:id="childTextarea" id="childTextarea"></textarea>

<input type="submit" wicket:id="childSubmit" wicket:message="value:FormInput.childSubmit"/>
</div>
</form>
Expand All @@ -126,13 +130,25 @@ <h3><wicket:message key="FormInput.submittedData"/></h3>
<wicket:message key="FormInput.parentTextLabel"/>
<span wicket:id="parentData"/>
</div>
<div class="gap-1">
<wicket:message key="FormInput.parentTextareaLabel"/>
<span wicket:id="parentTextarea"/>
</div>
<div class="gap-1">
<wicket:message key="FormInput.childTextLabel"/>
<span wicket:id="childData"/>
</div>
<div class="gap-1">
<wicket:message key="FormInput.childTextareaLabel"/>
<span wicket:id="childTextarea"/>
</div>
</div>
</form>

<div id="feedbackPanel">
<span wicket:id="feedback"/>
</div>

</wicket:extend>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package org.apache.wicket.examples.forminput;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.core.request.handler.IPartialPageRequestHandler;
Expand Down Expand Up @@ -359,6 +358,9 @@ public ParentForm(String id, IModel<ParentFormData> model)
TextField<String> parentText = new TextField<>("parentText", new PropertyModel<>(model, "parentText"));
add(parentText);

TextArea<String> parentTextarea = new TextArea<>("parentTextarea", new PropertyModel<>(model, "parentTextarea"));
add(parentTextarea);

final ParentFormDataTable parentFormDataTable = new ParentFormDataTable("parentFormDataTable", model);
add(parentFormDataTable);

Expand All @@ -382,6 +384,9 @@ protected void onSubmit(AjaxRequestTarget target)
TextField<String> childText = new TextField<>("childText", new PropertyModel<>(model, "childText"));
childForm.add(childText);

TextArea<String> childTextarea = new TextArea<>("childTextarea", new PropertyModel<>(model, "childTextarea"));
childForm.add(childTextarea);

AjaxSubmitLink childSubmit = new AjaxSubmitLink("childSubmit")
{

Expand All @@ -408,7 +413,9 @@ public ParentFormDataTable(String id, IModel<ParentFormData> model)
setOutputMarkupId(true);

add(new Label("parentData", new PropertyModel<>(model, "parentText")));
add(new Label("parentTextarea", new PropertyModel<>(model, "parentTextarea")));
add(new Label("childData", new PropertyModel<>(model, "childText")));
add(new Label("childTextarea", new PropertyModel<>(model, "childTextarea")));
}
}

Expand All @@ -419,6 +426,10 @@ private static class ParentFormData implements Serializable

private String childText;

private String parentTextarea;

private String childTextarea;

public String getParentText()
{
return parentText;
Expand All @@ -438,5 +449,25 @@ public void setChildText(String childText)
{
this.childText = childText;
}

public String getChildTextarea()
{
return childTextarea;
}

public void setChildTextarea(String childTextarea)
{
this.childTextarea = childTextarea;
}

public String getParentTextarea()
{
return parentTextarea;
}

public void setParentTextarea(String parentTextarea)
{
this.parentTextarea = parentTextarea;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,6 @@ FormInput.childSubmit=Submit child form
FormInput.submittedData=Submitted form data
FormInput.formHierarchyExample=Example of form hierarchy with default submit buttons
FormInput.parentForm=Parent form
FormInput.childForm=Child form
FormInput.childForm=Child form
FormInput.parentTextareaLabel=Parent text area (no submit):
FormInput.childTextareaLabel=Child text area (no submit):

0 comments on commit 69f1eb7

Please sign in to comment.