Updated Branches: refs/heads/master dd76a815c -> 02b74f92c
WICKET-4797 fixed handling of checkboxes; extracted js for easier maintenance, using input name instead of markup id with semantics; fixed handling of event bubbling Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/02b74f92 Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/02b74f92 Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/02b74f92 Branch: refs/heads/master Commit: 02b74f92c8b890ad0c6f90682954a8a855279b88 Parents: dd76a81 Author: svenmeier <[email protected]> Authored: Tue Oct 2 22:32:51 2012 +0200 Committer: svenmeier <[email protected]> Committed: Tue Oct 2 22:32:51 2012 +0200 ---------------------------------------------------------------------- .../AjaxFormChoiceComponentUpdatingBehavior.java | 54 +++------- .../AjaxFormChoiceComponentUpdatingBehavior.js | 80 +++++++++++++++ 2 files changed, 95 insertions(+), 39 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/02b74f92/wicket-core/src/main/java/org/apache/wicket/ajax/form/AjaxFormChoiceComponentUpdatingBehavior.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/ajax/form/AjaxFormChoiceComponentUpdatingBehavior.java b/wicket-core/src/main/java/org/apache/wicket/ajax/form/AjaxFormChoiceComponentUpdatingBehavior.java index 143fd6f..14eca56 100644 --- a/wicket-core/src/main/java/org/apache/wicket/ajax/form/AjaxFormChoiceComponentUpdatingBehavior.java +++ b/wicket-core/src/main/java/org/apache/wicket/ajax/form/AjaxFormChoiceComponentUpdatingBehavior.java @@ -29,17 +29,14 @@ import org.apache.wicket.markup.html.form.CheckGroup; import org.apache.wicket.markup.html.form.FormComponent; import org.apache.wicket.markup.html.form.RadioChoice; import org.apache.wicket.markup.html.form.RadioGroup; -import org.apache.wicket.util.string.AppendingStringBuffer; +import org.apache.wicket.request.resource.JavaScriptResourceReference; +import org.apache.wicket.request.resource.ResourceReference; /** * This is a Ajax Component Update Behavior that is meant for choices/groups that are not one * component in the html but many. * <p> - * Use the normal {@link AjaxFormComponentUpdatingBehavior} for the normal single component - * fields - * <p> - * In order to be supported by this behavior the group components must output children with markup - * id in format of 'groupId-childId' + * Use the normal {@link AjaxFormComponentUpdatingBehavior} for the normal single component fields * * @author jcompagner * @@ -50,6 +47,9 @@ import org.apache.wicket.util.string.AppendingStringBuffer; */ public abstract class AjaxFormChoiceComponentUpdatingBehavior extends AbstractDefaultAjaxBehavior { + private static final ResourceReference CHOICE_JS = new JavaScriptResourceReference( + AjaxFormChoiceComponentUpdatingBehavior.class, "AjaxFormChoiceComponentUpdatingBehavior.js"); + private static final long serialVersionUID = 1L; @Override @@ -57,36 +57,12 @@ public abstract class AjaxFormChoiceComponentUpdatingBehavior extends AbstractDe { super.renderHead(component, response); - // a function that registers a click listener which dynamically - // extracts the currently selected checkboxes/radios on every click - AppendingStringBuffer asb = new AppendingStringBuffer(); - asb.append("function attachChoiceHandlers(markupId, attrs) {\n"); - asb.append(" function getInputValues(groupId, attributes) {\n"); - asb.append(" var result = [], srcElement = attributes.event.target;\n"); - - asb.append(" var inputNode = srcElement;\n"); - asb.append(" if (!inputNode.checked) return;\n"); - asb.append(" if (!inputNode.type) return;\n"); - asb.append(" if (!(inputNode.className.indexOf('wicket-'+markupId)>=0)&&!(inputNode.id.indexOf(markupId+'-')>=0)) return;\n"); - asb.append(" var inputType = inputNode.type.toLowerCase();\n"); - asb.append(" if (inputType === 'checkbox' || inputType === 'radio') {\n"); - asb.append(" var name = inputNode.name, value = inputNode.value;\n"); - asb.append(" result.push({ name: name, value: value });\n"); - asb.append(" }\n"); // if (checkbox or radio) - - asb.append(" return result;\n"); - asb.append(" }\n"); // function getInputValues() - asb.append(" attrs.pre = (attrs.pre || []).concat([ function(attributes) { return attributes.event.target.tagName.toLowerCase() === 'input'; } ]);\n"); - asb.append(" attrs.dep = (attrs.dep || []).concat([ function(attributes) { var deps = getInputValues(markupId, attributes); return deps; } ]);\n"); - asb.append(" Wicket.Ajax.post(attrs);\n"); - asb.append("}\n"); // function attachChoiceHandlers() - - response.render(JavaScriptHeaderItem.forScript(asb, "attachChoice-" + component.getMarkupId())); - - String onLoadScript = String.format("attachChoiceHandlers('%s', %s)", - component.getMarkupId(), renderAjaxAttributes(component)); - response.render(OnLoadHeaderItem.forScript(onLoadScript)); + response.render(JavaScriptHeaderItem.forReference(CHOICE_JS)); + String onLoadScript = String.format("Wicket.Choice.attach('%s', '%s', %s)", + component.getMarkupId(), getFormComponent().getInputName(), + renderAjaxAttributes(component)); + response.render(OnLoadHeaderItem.forScript(onLoadScript)); } @Override @@ -102,7 +78,7 @@ public abstract class AjaxFormChoiceComponentUpdatingBehavior extends AbstractDe * has been updated. * * @param target - * the current request handler + * the current request handler */ protected abstract void onUpdate(AjaxRequestTarget target); @@ -114,9 +90,9 @@ public abstract class AjaxFormChoiceComponentUpdatingBehavior extends AbstractDe * FormComponent * * @param target - * the current request handler + * the current request handler * @param e - * the error that occurred while updating the component + * the error that occurred while updating the component */ protected void onError(AjaxRequestTarget target, RuntimeException e) { @@ -191,7 +167,7 @@ public abstract class AjaxFormChoiceComponentUpdatingBehavior extends AbstractDe /** * @param component - * the component to check + * the component to check * @return if the component applies to the {@link AjaxFormChoiceComponentUpdatingBehavior} */ static boolean appliesTo(Component component) http://git-wip-us.apache.org/repos/asf/wicket/blob/02b74f92/wicket-core/src/main/java/org/apache/wicket/ajax/form/AjaxFormChoiceComponentUpdatingBehavior.js ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/ajax/form/AjaxFormChoiceComponentUpdatingBehavior.js b/wicket-core/src/main/java/org/apache/wicket/ajax/form/AjaxFormChoiceComponentUpdatingBehavior.js new file mode 100644 index 0000000..0c4e124 --- /dev/null +++ b/wicket-core/src/main/java/org/apache/wicket/ajax/form/AjaxFormChoiceComponentUpdatingBehavior.js @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/* + * Wicket Choice Ajax updates. + * + * @author svenmeier + */ + + +// introduce a namespace +if (typeof (Wicket.Choice) === "undefined") { + Wicket.Choice = {}; + + /** + * Is a change accepted. + * + * @param name input name of choice + * @param attrs ajax attributes + */ + Wicket.Choice.acceptInput = function(name, attrs) { + "use strict"; + + var srcElement = attrs.event.target; + + return srcElement.name == name; + }; + + /** + * Get all checked input values. + * + * @param markupId markup id of choice + * @param name input name of choice + * @param attrs ajax attributes + */ + Wicket.Choice.getInputValues = function(markupId, name, attrs) { + var result = [], srcElement = attrs.event.target; + + var inputNodes = Wicket.$(markupId).getElementsByTagName("input"); + for (var i = 0 ; i < inputNodes.length ; i ++) { + var inputNode = inputNodes[i]; + + if (inputNode.name != name) continue; + if (!inputNode.checked) continue; + + var value = inputNode.value; + result.push({ name: name, value: value }); + } + + return result; + }; + + /** + * Attach to a choice. + * + * @param markupId markup id of choice + * @param input name of choice + * @attrs ajax attributes + */ + Wicket.Choice.attach = function(markupId, name, attrs) { + "use strict"; + + attrs.pre = (attrs.pre || []).concat([ function(attributes) { var pre = Wicket.Choice.acceptInput(name, attributes); return pre; } ]); + attrs.dep = (attrs.dep || []).concat([ function(attributes) { var deps = Wicket.Choice.getInputValues(markupId, name, attributes); return deps; } ]); + Wicket.Ajax.post(attrs); + }; +}
