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 */
017package org.apache.wicket.ajax.form;
018
019import org.apache.wicket.Component;
020import org.apache.wicket.ajax.AjaxRequestTarget;
021import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
022import org.apache.wicket.markup.html.form.FormComponent;
023import org.apache.wicket.markup.html.form.TextArea;
024import org.apache.wicket.markup.html.form.TextField;
025import org.apache.wicket.util.lang.Args;
026import org.danekja.java.util.function.serializable.SerializableConsumer;
027
028/**
029 * A behavior that updates the hosting {@link FormComponent}'s model via Ajax when value
030 * of the component is changed.
031 * <p>
032 * For {@link TextField} and {@link TextArea} form components this behavior uses JavaScript
033 * <em>input</em> and <em>change</em> events, for other form component types only JavaScript
034 * <em>change</em> event is used.
035 * <br/>
036 * <strong>Note</strong>: JavaScript <em>change</em> event is being fired by the browser
037 * also when the HTML form element losses the focus, i.e. an Ajax call will be made even
038 * if there is no actual change of the form element's value!
039 * </p>
040 * 
041 * @author Janne Hietam&auml;ki (janne)
042 * 
043 * @since 1.3
044 * @see org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior#onUpdate(org.apache.wicket.ajax.AjaxRequestTarget)
045 * @see org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior#onError(org.apache.wicket.ajax.AjaxRequestTarget, RuntimeException)
046 */
047public abstract class OnChangeAjaxBehavior extends AjaxFormComponentUpdatingBehavior
048{
049        private static final long serialVersionUID = 1L;
050
051        /**
052         * 'input' and 'change' used as a fallback for all other form component types.
053         */
054        public static final String EVENT_NAME = "input change";
055
056        /**
057         * the change event
058         */
059        public static final String EVENT_CHANGE = "change";
060
061        /**
062         * Constructor.
063         */
064        public OnChangeAjaxBehavior()
065        {
066                super(EVENT_NAME);
067        }
068
069        @Override
070        protected void updateAjaxAttributes(AjaxRequestAttributes attributes) 
071        {
072                super.updateAjaxAttributes(attributes);
073
074                Component component = getComponent();
075
076                // textfiels and textareas will trigger this behavior with either 'input' or 'change' events
077                // all the other components will use just 'change'
078                if (!(component instanceof TextField || component instanceof TextArea))
079                {
080                        attributes.setEventNames(EVENT_CHANGE);
081                }
082        }
083
084        /**
085         * Creates an {@link OnChangeAjaxBehavior} based on lambda expressions
086         * 
087         * @param onChange
088         *            the {@code SerializableConsumer} which accepts the {@link AjaxRequestTarget}
089         * @return the {@link OnChangeAjaxBehavior}
090         */
091        public static OnChangeAjaxBehavior onChange(SerializableConsumer<AjaxRequestTarget> onChange)
092        {
093                Args.notNull(onChange, "onChange");
094
095                return new OnChangeAjaxBehavior()
096                {
097                        private static final long serialVersionUID = 1L;
098
099                        @Override
100                        protected void onUpdate(AjaxRequestTarget target)
101                        {
102                                onChange.accept(target);
103                        }
104                };
105        }
106}