001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.wicket.markup.html.form;
018:
019: import org.apache.wicket.markup.ComponentTag;
020: import org.apache.wicket.model.IModel;
021:
022: /**
023: * A link which can be used exactly like a Button to submit a Form. The href of
024: * the link will use JavaScript to submit the form.
025: *
026: * <p>
027: * You can use this class 2 ways. First with the constructor without a Form
028: * object then this Link must be inside a Form so that it knows what form to
029: * submit to. Second way is to use the Form constructor then that form will be
030: * used to submit to.
031: * </p>
032: * <p>
033: * <pre>
034: * Form f = new Form("linkForm", new CompoundPropertyModel(mod));
035: * f.add(new TextField("value1"));
036: * f.add(new SubmitLink("link1") {
037: * protected void onSubmit() {
038: * System.out.println("Link1 was clicked, value1 is: "
039: * + mod.getValue1());
040: * };
041: * });
042: * add(new SubmitLink("link2",f) {
043: * protected void onSubmit() {
044: * System.out.println("Link2 was clicked, value1 is: "
045: * + mod.getValue1());
046: * };
047: * });
048: *
049: * <form wicket:id="linkForm" >
050: * <input wicket:id="value1" type="text" size="30"/>
051: * <a wicket:id="link1">Press link1 to submit</a>
052: * <input type="submit" value="Send"/>
053: * </form>
054: * <a wicket:id="link2">Press link 2 to submit</a>
055: * </pre>
056: * </p>
057: * <p>
058: * If this link is not placed in a form or given a form to cooperate with, it will
059: * fall back to a normal link behavior, meaning that {@link #onSubmit()} will be called
060: * without any other consequences.
061: * </p>
062: *
063: * @author chris
064: * @author jcompagner
065: * @author Igor Vaynberg (ivaynberg)
066: * @author Eelco Hillenius
067: */
068: public class SubmitLink extends AbstractSubmitLink {
069: private static final long serialVersionUID = 1L;
070:
071: /**
072: * With this constructor the SubmitLink must be inside a Form.
073: *
074: * @param id
075: * The id of the submitlink.
076: */
077: public SubmitLink(String id) {
078: super (id);
079: }
080:
081: /**
082: * With this constructor the SubmitLink will submit the {@link Form} that is
083: * given when the link is clicked on.
084: *
085: * The SubmitLink doesn't have to be in inside the {@link Form}. But
086: * currently if it is outside the {@link Form} and the SubmitLink will be
087: * rendered first. Then the {@link Form} will have a generated
088: * javascript/css id. The markup javascript/css id that can exist will be
089: * overridden.
090: *
091: * @param id
092: * The id of the submitlink.
093: * @param form
094: * The form which this submitlink must submit.
095: */
096: public SubmitLink(String id, Form form) {
097: super (id, form);
098: }
099:
100: /**
101: * With this constructor the SubmitLink must be inside a Form.
102: *
103: * @param id
104: * The id of the submitlink.
105: * @param model
106: * The model for this submitlink, It won't be used by the submit
107: * link itself, but it can be used for keeping state
108: */
109: public SubmitLink(String id, IModel model) {
110: super (id, model);
111: }
112:
113: /**
114: * With this constructor the SubmitLink will submit the {@link Form} that is
115: * given when the link is clicked on.
116: *
117: * The SubmitLink doesn't have to be in inside the {@link Form}. But
118: * currently if it is outside the {@link Form} and the SubmitLink will be
119: * rendered first. Then the {@link Form} will have a generated
120: * javascript/css id. The markup javascript/css id that can exist will be
121: * overridden.
122: *
123: * @param id
124: * The id of the submitlink.
125: * @param model
126: * The model for this submitlink, It won't be used by the submit
127: * link itself, but it can be used for keeping state
128: * @param form
129: * The form which this submitlink must submit.
130: */
131: public SubmitLink(String id, IModel model, Form form) {
132: super (id, model, form);
133: }
134:
135: /**
136: * This method is here as a means to fall back on normal link
137: * behavior when this link is not nested in a form. Not intended
138: * to be called by clients directly.
139: * @see org.apache.wicket.markup.html.link.ILinkListener#onLinkClicked()
140: */
141: public final void onLinkClicked() {
142: onSubmit();
143: }
144:
145: /**
146: * @inheritDoc
147: * @see org.apache.wicket.Component#onComponentTag(org.apache.wicket.markup.ComponentTag)
148: */
149: protected void onComponentTag(ComponentTag tag) {
150: super .onComponentTag(tag);
151: // If we're disabled
152: if (!isLinkEnabled()) {
153: disableLink(tag);
154: } else {
155: if (tag.getName().equalsIgnoreCase("a")) {
156: tag.put("href", "#");
157: }
158: tag.put("onclick", getTriggerJavaScript());
159: }
160: }
161:
162: /**
163: * Controls whether or not clicking on this link will invoke form's
164: * javascript onsubmit handler. True by default.
165: *
166: * @return true if form's javascript onsubmit handler should be invoked,
167: * false otherwise
168: */
169: protected boolean shouldInvokeJavascriptFormOnsubmit() {
170: return true;
171: }
172:
173: /**
174: * The javascript which trigges this link.
175: *
176: * TODO: This is a copy & paste from Button
177: *
178: * @return The javascript
179: */
180: protected final String getTriggerJavaScript() {
181: if (getForm() != null) {
182: // find the root form - the one we are really going to submit
183: Form root = getForm().getRootForm();
184: StringBuffer sb = new StringBuffer(100);
185: sb.append("var e=document.getElementById('");
186: sb.append(root.getHiddenFieldId());
187: sb.append("'); e.name=\'");
188: sb.append(getInputName());
189: sb.append("'; e.value='x';");
190: sb.append("var f=document.getElementById('");
191: sb.append(root.getMarkupId());
192: sb.append("');");
193: if (shouldInvokeJavascriptFormOnsubmit()) {
194: if (getForm() != root) {
195: sb.append("var ff=document.getElementById('");
196: sb.append(getForm().getMarkupId());
197: sb.append("');");
198: } else {
199: sb.append("var ff=f;");
200: }
201: sb
202: .append("if (ff.onsubmit != undefined) { if (ff.onsubmit()==false) return false; }");
203: }
204: sb.append("f.submit();e.value='';e.name='';return false;");
205: return sb.toString();
206: } else {
207: return null;
208: }
209: }
210:
211: /**
212: * @see org.apache.wicket.markup.html.form.IFormSubmittingComponent#onSubmit()
213: */
214: public void onSubmit() {
215: }
216:
217: }
|