1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
38
39
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
47 @Parameter
48 private List<Object> source;
49
50
51 @Parameter
52 private int rowsPerPage;
53
54
55
56
57
58 @Parameter( "5" )
59 private int range;
60
61
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 }