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.markup.html;
018
019import org.apache.wicket.Component;
020import org.apache.wicket.IGenericComponent;
021import org.apache.wicket.ajax.AjaxEventBehavior;
022import org.apache.wicket.ajax.AjaxRequestTarget;
023import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
024import org.apache.wicket.markup.ComponentTag;
025import org.apache.wicket.markup.html.link.AbstractLink;
026import org.apache.wicket.model.IModel;
027
028/**
029 * A component that allows a trigger request to be triggered via html anchor tag
030 * 
031 * @since 1.2
032 * 
033 * @author Igor Vaynberg (ivaynberg)
034 * @param <T>
035 *            type of model object
036 * 
037 */
038public abstract class AjaxLink<T> extends AbstractLink implements IAjaxLink, IGenericComponent<T, AjaxLink<T>>
039{
040        private static final long serialVersionUID = 1L;
041
042        /**
043         * Construct.
044         * 
045         * @param id
046         */
047        public AjaxLink(final String id)
048        {
049                this(id, null);
050        }
051
052        /**
053         * Construct.
054         * 
055         * @param id
056         * @param model
057         */
058        public AjaxLink(final String id, final IModel<T> model)
059        {
060                super(id, model);
061        }
062
063
064        @Override
065        protected void onInitialize()
066        {
067                super.onInitialize();
068                add(newAjaxEventBehavior("click"));
069        }
070
071        /**
072         * @param event
073         *            the name of the default event on which this link will listen to
074         * @return the ajax behavior which will be executed when the user clicks the link
075         */
076        protected AjaxEventBehavior newAjaxEventBehavior(String event)
077        {
078                return new AjaxEventBehavior(event)
079                {
080                        private static final long serialVersionUID = 1L;
081
082                        @Override
083                        protected void onEvent(AjaxRequestTarget target)
084                        {
085                                onClick(target);
086                        }
087
088                        @Override
089                        protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
090                        {
091                                attributes.setPreventDefault(true);
092                                super.updateAjaxAttributes(attributes);
093                                AjaxLink.this.updateAjaxAttributes(attributes);
094                        }
095                        
096                        @Override
097                        public boolean getStatelessHint(Component component)
098                        {
099                                return AjaxLink.this.getStatelessHint();
100                        }
101                };
102        }
103
104        protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
105        {
106        }
107
108        @Override
109        protected void onComponentTag(ComponentTag tag)
110        {
111                super.onComponentTag(tag);
112
113                if (isEnabledInHierarchy())
114                {
115                        String tagName = tag.getName();
116                        
117                        if (tagName.equalsIgnoreCase("a") || tagName.equalsIgnoreCase("link") ||
118                                tagName.equalsIgnoreCase("area"))
119                        {
120                                // disable any href attr in markup
121                                tag.put("href", "#");
122                        }
123                        else if (tagName.equalsIgnoreCase("button"))
124                        {
125                                // WICKET-5597 prevent submit
126                                tag.put("type", "button");
127                        }
128                }
129                else
130                {
131                        disableLink(tag);
132                }
133
134        }
135
136        /**
137         * Listener method invoked on the ajax request generated when the user clicks the link
138         * 
139         * @param target
140         */
141        @Override
142        public abstract void onClick(final AjaxRequestTarget target);
143
144        @Override
145        protected boolean getStatelessHint()
146        {
147                return false;
148        }
149}