Skip to content

Commit f96f99e

Browse files
committed
- AjaxSubmitButton and AjaxSubmitLink now extends Button. This gives them the possibility to set DefaultFormProcessing.
- It's now possible to submit a form, using a Button which is outside the form. - Introduced a TagTester helper class. This helps testing the rendered markup without having to parse it yourself or compare it to some cached markup. git-svn-id: https://svn.apache.org/repos/asf/incubator/wicket/trunk@461888 13f79535-47bb-0310-9956-ffa450edef68
1 parent 8386393 commit f96f99e

17 files changed

+996
-120
lines changed

wicket/src/java/wicket/ajax/markup/html/form/AjaxSubmitButton.java

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import wicket.ajax.IAjaxCallDecorator;
2525
import wicket.ajax.form.AjaxFormSubmitBehavior;
2626
import wicket.markup.ComponentTag;
27-
import wicket.markup.html.WebComponent;
2827
import wicket.markup.html.form.Button;
2928
import wicket.markup.html.form.Form;
3029
import wicket.util.string.AppendingStringBuffer;
@@ -38,10 +37,12 @@
3837
*
3938
* @author Igor Vaynberg (ivaynberg)
4039
*/
41-
public abstract class AjaxSubmitButton extends WebComponent
40+
public abstract class AjaxSubmitButton extends Button
4241
{
4342
private static final long serialVersionUID = 1L;
4443

44+
private Form form;
45+
4546
/**
4647
* Construct.
4748
*
@@ -53,7 +54,8 @@ public abstract class AjaxSubmitButton extends WebComponent
5354
*/
5455
public AjaxSubmitButton(MarkupContainer parent, String id, final Form form)
5556
{
56-
super(parent, id);
57+
super(parent, id, form);
58+
this.form = form;
5759

5860
add(new AjaxFormSubmitBehavior(form, ClientEvent.CLICK)
5961
{
@@ -115,7 +117,28 @@ protected void onComponentTag(ComponentTag tag)
115117
+ "'");
116118
}
117119

118-
super.onComponentTag(tag);
120+
// super.onComponentTag(tag);
121+
}
122+
123+
/**
124+
*
125+
* @see wicket.markup.html.form.FormComponent#getForm()
126+
*/
127+
@Override
128+
public Form getForm()
129+
{
130+
return form;
131+
}
132+
133+
/**
134+
* Final implementation of the Button's onSubmit. AjaxSubmitButtons have
135+
* there own onSubmit which is called.
136+
*
137+
* @see wicket.markup.html.form.Button#onSubmit()
138+
*/
139+
@Override
140+
protected final void onSubmit()
141+
{
119142
}
120143

121144
/**

wicket/src/java/wicket/ajax/markup/html/form/AjaxSubmitLink.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
import wicket.ajax.IAjaxCallDecorator;
2525
import wicket.ajax.form.AjaxFormSubmitBehavior;
2626
import wicket.markup.ComponentTag;
27-
import wicket.markup.html.WebMarkupContainer;
27+
import wicket.markup.html.form.Button;
2828
import wicket.markup.html.form.Form;
2929
import wicket.util.string.AppendingStringBuffer;
3030

@@ -37,7 +37,7 @@
3737
*
3838
* @author Igor Vaynberg (ivaynberg)
3939
*/
40-
public abstract class AjaxSubmitLink extends WebMarkupContainer
40+
public abstract class AjaxSubmitLink extends Button
4141
{
4242
private static final long serialVersionUID = 1L;
4343

@@ -52,7 +52,7 @@ public abstract class AjaxSubmitLink extends WebMarkupContainer
5252
*/
5353
public AjaxSubmitLink(MarkupContainer parent, String id, final Form form)
5454
{
55-
super(parent, id);
55+
super(parent, id, form);
5656

5757
add(new AjaxFormSubmitBehavior(form, ClientEvent.CLICK)
5858
{
@@ -107,6 +107,17 @@ protected void onComponentTag(ComponentTag tag)
107107
tag.put("href", "#");
108108
}
109109

110+
/**
111+
* Final implementation of the Button's onSubmit. AjaxSubmitLinks have
112+
* there own onSubmit which is called.
113+
*
114+
* @see wicket.markup.html.form.Button#onSubmit()
115+
*/
116+
@Override
117+
protected final void onSubmit()
118+
{
119+
}
120+
110121
/**
111122
* Listener method invoked on form submit
112123
*

wicket/src/java/wicket/markup/html/form/Button.java

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
package wicket.markup.html.form;
1919

2020
import wicket.MarkupContainer;
21+
import wicket.ajax.ClientEvent;
2122
import wicket.markup.ComponentTag;
2223
import wicket.model.IModel;
24+
import wicket.util.string.Strings;
2325
import wicket.version.undo.Change;
2426

2527
/**
@@ -67,6 +69,8 @@ public abstract class Button<T> extends FormComponent<T>
6769
*/
6870
private boolean defaultFormProcessing = true;
6971

72+
private Form form;
73+
7074
/**
7175
* Constructor without a model. Buttons without models leave the markup
7276
* attribute &quot;value&quot;. Provide a model if you want to set the
@@ -78,6 +82,18 @@ public Button(MarkupContainer parent, String id)
7882
{
7983
super(parent, id);
8084
}
85+
86+
/**
87+
* Construct.
88+
*
89+
* @param parent
90+
* @param id
91+
* @param form
92+
*/
93+
public Button(MarkupContainer parent, String id, Form form)
94+
{
95+
this(parent, id, null, form);
96+
}
8197

8298
/**
8399
* Constructor taking an model for rendering the 'label' of the button (the
@@ -100,6 +116,19 @@ public Button(MarkupContainer parent, final String id, final IModel<T> model)
100116
{
101117
super(parent, id, model);
102118
}
119+
120+
/**
121+
* Construct.
122+
* @param parent
123+
* @param id
124+
* @param model
125+
* @param form
126+
*/
127+
public Button(MarkupContainer parent, final String id, final IModel<T> model, final Form form)
128+
{
129+
super(parent, id, model);
130+
this.form = form;
131+
}
103132

104133
/**
105134
* Override of the default initModel behaviour. This component <strong>will
@@ -223,6 +252,48 @@ protected void onComponentTag(final ComponentTag tag)
223252
{
224253
tag.put("onclick", onClickJavaScript);
225254
}
255+
// If the button isn't added to the form's hierarchy
256+
// we use javascript to submit the form instead
257+
else
258+
{
259+
Form parentForm = findParent(Form.class);
260+
if (parentForm == null && form != null)
261+
{
262+
// Lets see if we should add some on click javascript
263+
String triggerJavaScript = getTriggerJavaScript();
264+
if (Strings.isEmpty(triggerJavaScript) == false)
265+
{
266+
tag.put(ClientEvent.CLICK.getEvent(), triggerJavaScript);
267+
}
268+
}
269+
}
270+
}
271+
272+
/**
273+
* Controls whether or not clicking on this link will invoke form's
274+
* javascript onsubmit handler. True by default.
275+
*
276+
* @return true if form's javascript onsubmit handler should be invoked,
277+
* false otherwise
278+
*/
279+
protected boolean shouldInvokeJavascriptFormOnsubmit()
280+
{
281+
return true;
282+
}
283+
284+
285+
286+
@Override
287+
public Form getForm()
288+
{
289+
if (this.form != null)
290+
{
291+
return this.form;
292+
}
293+
else
294+
{
295+
return findParent(Form.class);
296+
}
226297
}
227298

228299
/**
@@ -231,4 +302,31 @@ protected void onComponentTag(final ComponentTag tag)
231302
*/
232303
protected abstract void onSubmit();
233304

305+
/**
306+
* The javascript which trigges this link
307+
*
308+
* @return The javascript
309+
*/
310+
protected final String getTriggerJavaScript()
311+
{
312+
if (getForm() != null) {
313+
StringBuffer sb = new StringBuffer(100);
314+
sb.append("var e=document.getElementById('");
315+
sb.append(getForm().getHiddenFieldId(Form.HIDDEN_FIELD_FAKE_SUBMIT));
316+
sb.append("'); e.name=\'");
317+
sb.append(getInputName());
318+
sb.append("'; e.value='x';");
319+
sb.append("var f=document.getElementById('");
320+
sb.append(getForm().getMarkupId());
321+
sb.append("');");
322+
if (shouldInvokeJavascriptFormOnsubmit())
323+
{
324+
sb.append("if (f.onsubmit != undefined) { if (f.onsubmit()==false) return false; }");
325+
}
326+
sb.append("f.submit();return false;");
327+
return sb.toString();
328+
} else {
329+
return null;
330+
}
331+
}
234332
}

wicket/src/java/wicket/markup/html/form/Form.java

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -602,16 +602,17 @@ protected void delegateSubmit(Button submittingButton)
602602
*/
603603
protected final Button findSubmittingButton()
604604
{
605-
Button button = (Button)visitChildren(Button.class, new IVisitor()
605+
Button button = (Button)getPage().visitChildren(Button.class, new IVisitor()
606606
{
607607
public Object component(final Component component)
608608
{
609609
// Get button
610610
final Button button = (Button)component;
611611

612612
// Check for button-name or button-name.x request string
613-
if (getRequest().getParameter(button.getInputName()) != null
614-
|| getRequest().getParameter(button.getInputName() + ".x") != null)
613+
if (button.getForm() == Form.this
614+
&& (getRequest().getParameter(button.getInputName()) != null
615+
|| getRequest().getParameter(button.getInputName() + ".x") != null))
615616
{
616617
if (!button.isVisible())
617618
{
@@ -623,31 +624,7 @@ public Object component(final Component component)
623624
return CONTINUE_TRAVERSAL;
624625
}
625626
});
626-
627-
if (button == null)
628-
{
629-
button = (Button)getPage().visitChildren(SubmitLink.class, new IVisitor()
630-
{
631-
public Object component(final Component component)
632-
{
633-
// Get button
634-
final SubmitLink button = (SubmitLink)component;
635-
636-
// Check for button-name or button-name.x request string
637-
if (button.getForm() == Form.this
638-
&& (getRequest().getParameter(button.getInputName()) != null || getRequest()
639-
.getParameter(button.getInputName() + ".x") != null))
640-
{
641-
if (!button.isVisible())
642-
{
643-
throw new WicketRuntimeException("Submit Button is not visible");
644-
}
645-
return button;
646-
}
647-
return CONTINUE_TRAVERSAL;
648-
}
649-
});
650-
}
627+
651628
return button;
652629
}
653630

0 commit comments

Comments
 (0)