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.navigation.paging;
018
019import org.apache.wicket.ajax.AjaxRequestTarget;
020import org.apache.wicket.ajax.attributes.AjaxRequestAttributes;
021import org.apache.wicket.ajax.markup.html.IAjaxLink;
022import org.apache.wicket.markup.ComponentTag;
023import org.apache.wicket.markup.html.navigation.paging.IPageable;
024import org.apache.wicket.markup.html.navigation.paging.PagingNavigationLink;
025
026/**
027 * An Ajax version of a link to a page of a PageableListView.
028 * 
029 * @since 1.2
030 * 
031 * @author Martijn Dashorst
032 */
033public class AjaxPagingNavigationLink extends PagingNavigationLink<Void> implements IAjaxLink
034{
035        private static final long serialVersionUID = 1L;
036
037        /**
038         * Constructor.
039         * 
040         * @param id
041         *            See Component
042         * @param pageable
043         *            The pageable component for this page link
044         * @param pageNumber
045         *            The page number in the PageableListView that this link links to. Negative
046         *            pageNumbers are relative to the end of the list.
047         */
048        public AjaxPagingNavigationLink(final String id, final IPageable pageable, final long pageNumber)
049        {
050                super(id, pageable, pageNumber);
051
052                setOutputMarkupId(true);
053        }
054
055        @Override
056        protected void onInitialize()
057        {
058                super.onInitialize();
059                add(newAjaxPagingNavigationBehavior(pageable, "click"));
060        }
061
062        /**
063         * @param pageable
064         *            The pageable component the page links are referring to
065         * @param event
066         *            the name of the default event on which this link will listen to
067         * @return the ajax behavior which will be executed when the user clicks the link
068         */
069        protected AjaxPagingNavigationBehavior newAjaxPagingNavigationBehavior(IPageable pageable,
070                String event)
071        {
072                return new AjaxPagingNavigationBehavior(this, pageable, event)
073                {
074                        @Override
075                        protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
076                        {
077                                super.updateAjaxAttributes(attributes);
078                                attributes.setPreventDefault(true);
079                                AjaxPagingNavigationLink.this.updateAjaxAttributes(attributes);
080                        }
081                };
082        }
083
084        protected void updateAjaxAttributes(AjaxRequestAttributes attributes)
085        {
086        }
087
088        /**
089         * Fallback event listener, will redisplay the current page.
090         * 
091         * @see org.apache.wicket.markup.html.link.Link#onClick()
092         */
093        @Override
094        public void onClick()
095        {
096                onClick(null);
097        }
098
099        /**
100         * Performs the actual action of this component, performing a non-ajax fallback when there was
101         * no AjaxRequestTarget available.
102         * 
103         * @param target
104         *            the request target, when <code>null</code>, a full page refresh will be generated
105         */
106        @Override
107        public void onClick(AjaxRequestTarget target)
108        {
109                pageable.setCurrentPage(getPageNumber());
110        }
111
112        @Override
113        protected void onComponentTag(ComponentTag tag)
114        {
115                super.onComponentTag(tag);
116
117                // 'onclick' attribute would be set only if this component is attached
118                // to HTML element different than 'a'. This 'onclick' will break Ajax's
119                // event binding so here we remove it.
120                // AjaxFallback is supported only with 'a' HTML element. See WICKET-4862
121                tag.remove("onclick");
122        }
123}