View Javadoc

1   /**
2    * This file is part of the equanda project.
3    *
4    * The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at http://www.mozilla.org/MPL/
7    *
8    * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
9    * ANY KIND, either express or implied. See the License for the specific language governing rights and
10   * limitations under the License.
11   *
12   * Alternatively, the contents of this file may be used under the terms of
13   * either the GNU General Public License Version 2 or later (the "GPL"), or
14   * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
15   * in which case the provisions of the GPL or the LGPL are applicable instead
16   * of those above. If you wish to allow use of your version of this file only
17   * under the terms of either the GPL or the LGPL, and not to allow others to
18   * use your version of this file under the terms of the MPL, indicate your
19   * decision by deleting the provisions above and replace them with the notice
20   * and other provisions required by the GPL or the LGPL. If you do not delete
21   * the provisions above, a recipient may use your version of this file under
22   * the terms of any one of the MPL, the GPL or the LGPL.
23   */
24  
25  package org.equanda.tapestry5.components;
26  
27  import org.apache.tapestry5.MarkupWriter;
28  import org.apache.tapestry5.RenderSupport;
29  import org.apache.tapestry5.annotations.*;
30  import org.apache.tapestry5.ioc.annotations.Inject;
31  import org.apache.tapestry5.services.Environment;
32  import org.equanda.tapestry5.base.PagerConfig;
33  
34  import java.util.List;
35  
36  /**
37   * Equanda modification of {@link org.apache.tapestry5.corelib.components.GridPager}
38   * 
39   * @author <a href="mailto:vladimir.tkachenko@gmail.com">Vladimir Tkachenko</a>
40   */
41  @IncludeStylesheet( value = { "classpath:/org/equanda/tapestry5/resources/jspager.css" } )
42  @IncludeJavaScriptLibrary( { "${tapestry.scriptaculous}/prototype.js",
43                               "classpath:/org/equanda/tapestry5/resources/t5equanda.js" } )
44  public class JSPager
45  {
46      /** The source of the data displayed by the JSPagedLoop */
47      @Parameter
48      private List<Object> source;
49  
50      /** The number of rows displayed per page. */
51      @Parameter
52      private int rowsPerPage;
53  
54      /**
55       * Number of pages before and after the current page in the range. The pager always displays links for 2 * range + 1
56       * pages, unless that's more than the total number of available pages.
57       */
58      @Parameter( "5" )
59      private int range;
60  
61      // private int maxPages;
62  
63      @Environmental
64      private RenderSupport renderSupport;
65  
66      @Inject
67      private Environment environment;
68  
69      public PagerConfig getConfig()
70      {
71          JSPagedLoop pagedLoop = environment.peek( JSPagedLoop.class );
72  
73          int availableRows = source.size() - pagedLoop.getAddedRowCount();
74  
75          String clientId = pagedLoop.getClientId().toLowerCase();
76  
77          int maxPages = ( ( availableRows - 1 ) / rowsPerPage ) + 1;
78  
79          return new PagerConfig( range, maxPages, rowsPerPage, 0, pagedLoop.getCurrentPage(), source.size(), pagedLoop
80              .getAddedRowCount(), pagedLoop.getAdditionalRowCount(), pagedLoop.getCurrentPageFieldName(), pagedLoop
81              .getAddedRowFieldName(), "eqpl_" + clientId, "eqplr_" + clientId, "eqplal_" + clientId, "eqplab_"
82              + clientId, clientId );
83      }
84  
85      @BeginRender
86      void render( MarkupWriter writer )
87      {
88          JSPagedLoop pagedLoop = environment.peek( JSPagedLoop.class );
89  
90          if ( pagedLoop != null )
91          {
92              String clientId = pagedLoop.getClientId().toLowerCase();
93  
94              writer.element( "div", "class", "eqplp eqpl_" + clientId );
95              
96              PagerConfig config = getConfig();
97              
98              showPage( pagedLoop.getCurrentPage(), config, writer );
99              writer.end();
100 
101             renderSupport.addScript( "eqJsplC.%s = %s;", clientId, config.getJSONObject() );
102             renderSupport.addScript( "eqChAL( eqJsplC.%s );", clientId );
103         }
104     }
105 
106     private void showPage( int pageIdx, PagerConfig config, MarkupWriter writer )
107     {
108         if ( pageIdx > 0 )
109         {
110             config.setCurrentPage( pageIdx );
111         }
112 
113         if ( config.getCurrentPage() == 0 )
114         {
115             config.setCurrentPage( 1 );
116         }
117 
118         int availableRows = config.getRowCount() - config.getAdditionalRowCount() + config.getAddedRowCount();
119 
120         config.setMaxPages( ( ( availableRows - 1 ) / config.getRowsPerPage() ) + 1 );
121 
122         if ( config.getMaxPages() > 0 && config.getCurrentPage() > config.getMaxPages() )
123         {
124             config.setCurrentPage( config.getMaxPages() );
125         }
126 
127         if ( config.getMaxPages() < 2 )
128         {
129             return;
130         }
131 
132         config.setLastIndex( 0 );
133 
134         for ( int i = 1; i <= 2; i++ )
135         {
136             equandaWritePageLink( i, config, writer );
137         }
138 
139         int low = config.getCurrentPage() - config.getRange();
140         int high = config.getCurrentPage() + config.getRange();
141 
142         if ( low < 1 )
143         {
144             low = 1;
145             high = 2 * config.getRange() + 1;
146         }
147         else
148         {
149             if ( high > config.getMaxPages() )
150             {
151                 high = config.getMaxPages();
152                 low = high - 2 * config.getRange();
153             }
154         }
155 
156         for ( int i = low; i <= high; i++ )
157         {
158             equandaWritePageLink( i, config, writer );
159         }
160 
161         for ( int i = config.getMaxPages() - 1; i <= config.getMaxPages(); i++ )
162         {
163             equandaWritePageLink( i, config, writer );
164         }
165     }
166 
167     private void equandaWritePageLink( int pageIndex, PagerConfig config, MarkupWriter writer )
168     {
169         if ( pageIndex < 1 || pageIndex > config.getMaxPages() )
170             return;
171 
172         if ( pageIndex <= config.getLastIndex() )
173             return;
174 
175         if ( pageIndex != config.getLastIndex() + 1 )
176         {
177             writer.element( "div" );
178             writer.write( "..." );
179             writer.end();
180         }
181 
182         config.setLastIndex( pageIndex );
183 
184         if ( pageIndex == config.getCurrentPage() )
185         {
186             writer.element( "span", "class", "eqplc" );
187             writer.write( "" + pageIndex );
188             writer.end();
189             return;
190         }
191 
192         writer.element( "a", "href", "javascript:;", "onclick", String.format( "eqShwPg( %s, %s )", pageIndex,
193             "eqJsplC." + config.getName() ), "title", "Goto Page " + pageIndex );
194         writer.write( "" + pageIndex );
195         writer.end();
196     }
197 
198 }