0
|
1
|
|
2 package nabble.view.lib;
|
|
3
|
|
4 import java.io.IOException;
|
|
5 import java.io.PrintWriter;
|
|
6 import javax.servlet.http.HttpServletRequest;
|
|
7 import javax.servlet.http.HttpServletResponse;
|
|
8
|
|
9
|
|
10 // used outside nabble, so do not use Init
|
|
11 public final class HtmlViewUtils {
|
|
12
|
|
13 private HtmlViewUtils() {} // never
|
|
14
|
|
15
|
|
16 /** Number of pages to be displayed before/after the current page*/
|
|
17 public static final int NEIGHBOR_PAGES = 3;
|
|
18
|
|
19 public static interface PagingPath {
|
|
20 public String path(int row);
|
|
21 }
|
|
22
|
|
23 public static final class GenericPagingPath implements PagingPath {
|
|
24 private final String url;
|
|
25
|
|
26 public GenericPagingPath(String url) {
|
|
27 this.url = url;
|
|
28 }
|
|
29
|
|
30 public String path(int row) {
|
|
31 return url + (row==0 ? "" : "&i=" + row);
|
|
32 }
|
|
33 }
|
|
34
|
|
35 public static void genericPaging(HttpServletRequest request,HttpServletResponse response, int count, int currentRecord, int maxRows, PagingPath pagingPath, String margins)
|
|
36 throws IOException
|
|
37 {
|
|
38 genericPaging(request, response, count, currentRecord, maxRows, pagingPath, margins, false, NEIGHBOR_PAGES);
|
|
39 }
|
|
40
|
|
41 public static void genericPaging(HttpServletRequest request,HttpServletResponse response, int count, int currentRecord, int maxRows, PagingPath pagingPath, String margins, boolean hideLastPage,int NEIGHBOR_PAGES)
|
|
42 throws IOException
|
|
43 {
|
|
44 PrintWriter out = response.getWriter();
|
|
45 if (count > maxRows) {
|
|
46
|
|
47 out.print( "\r\n<style type=\"text/css\">\r\n span.page { padding: .1em .3em }\r\n span.current-page { border-width:1px; border-style:solid; }\r\n</style>\r\n<span style=\"float:right;white-space:nowrap;padding:" );
|
|
48 out.print( (margins) );
|
|
49 out.print( ";font-weight:normal\">\r\n " );
|
|
50
|
|
51 int currentPage = (currentRecord / maxRows) + 1;
|
|
52 int lastPage = count / maxRows;
|
|
53 // If there are more items on the next page...
|
|
54 if (count % maxRows > 0)
|
|
55 lastPage++; // ... I increment the last page.
|
|
56
|
|
57 int before = currentPage - NEIGHBOR_PAGES;
|
|
58 int after = currentPage + NEIGHBOR_PAGES;
|
|
59 if (before <= 1) {
|
|
60 // Among the first pages
|
|
61 int limit = Math.min(currentPage + NEIGHBOR_PAGES, lastPage);
|
|
62
|
|
63 for (int i = 1; i <= limit; i++) {
|
|
64 if (i == currentPage) {
|
|
65
|
|
66 out.print( "\r\n<span class=\"current-page page medium-border-color\">" );
|
|
67 out.print( (i) );
|
|
68 out.print( "</span>\r\n" );
|
|
69
|
|
70 } else {
|
|
71 int page = (i-1) * maxRows;
|
|
72
|
|
73 out.print( "\r\n <span class=\"page\">\r\n <a href=\"" );
|
|
74 out.print( (pagingPath.path(page)) );
|
|
75 out.print( "\" title=\"page " );
|
|
76 out.print( (i) );
|
|
77 out.print( "\">" );
|
|
78 out.print( (i) );
|
|
79 out.print( "</a>\r\n </span>\r\n" );
|
|
80
|
|
81 }
|
|
82 }
|
|
83
|
|
84 if (limit < lastPage) {
|
|
85 int page = (lastPage-1) * maxRows;
|
|
86 boolean atEnd = after == lastPage-1;
|
|
87
|
|
88 out.print( "\r\n" );
|
|
89 out.print( (atEnd? "" : "...") );
|
|
90 out.print( "\r\n" );
|
|
91 if (!hideLastPage || atEnd) {
|
|
92 out.print( "\r\n<span class=\"page\">\r\n <a href=\"" );
|
|
93 out.print( (pagingPath.path(page)) );
|
|
94 out.print( "\" title=\"page " );
|
|
95 out.print( (lastPage) );
|
|
96 out.print( "\"" );
|
|
97 out.print( (atEnd? "" : " rel=\"nofollow\"") );
|
|
98 out.print( ">" );
|
|
99 out.print( (lastPage) );
|
|
100 out.print( "</a>\r\n</span>\r\n" );
|
|
101 }
|
|
102 out.print( "\r\n" );
|
|
103
|
|
104 }
|
|
105 } else if (before > 1 && after < lastPage) {
|
|
106 // In the middle pages
|
|
107
|
|
108 out.print( "\r\n<span class=\"page\">\r\n <a href=\"" );
|
|
109 out.print( (pagingPath.path(0)) );
|
|
110 out.print( "\" title=\"page 1\">1</a>\r\n</span>\r\n" );
|
|
111 out.print( (before == 2? "" : "...") );
|
|
112 out.print( "\r\n" );
|
|
113
|
|
114 int limit = before + 2 * NEIGHBOR_PAGES;
|
|
115 for (int i = before; i <= limit; i++) {
|
|
116 if (i == currentPage) {
|
|
117
|
|
118 out.print( "<span class=\"current-page page medium-border-color\">" );
|
|
119 out.print( (i) );
|
|
120 out.print( "</span>" );
|
|
121
|
|
122 } else {
|
|
123 int page = (i-1) * maxRows;
|
|
124
|
|
125 out.print( "\r\n <span class=\"page\">\r\n <a href=\"" );
|
|
126 out.print( (pagingPath.path(page)) );
|
|
127 out.print( "\" title=\"page " );
|
|
128 out.print( (i) );
|
|
129 out.print( "\">" );
|
|
130 out.print( (i) );
|
|
131 out.print( "</a>\r\n </span>\r\n" );
|
|
132
|
|
133 }
|
|
134 }
|
|
135
|
|
136 if (limit < lastPage) {
|
|
137 int page = (lastPage-1) * maxRows;
|
|
138 boolean atEnd = after == lastPage-1;
|
|
139
|
|
140 out.print( "\r\n" );
|
|
141 out.print( (atEnd? "" : "...") );
|
|
142 out.print( "\r\n" );
|
|
143 if (!hideLastPage || atEnd) {
|
|
144 out.print( "\r\n<span class=\"page\">\r\n <a href=\"" );
|
|
145 out.print( (pagingPath.path(page)) );
|
|
146 out.print( "\" title=\"page " );
|
|
147 out.print( (lastPage) );
|
|
148 out.print( "\"" );
|
|
149 out.print( (atEnd? "" : " rel=\"nofollow\"") );
|
|
150 out.print( ">" );
|
|
151 out.print( (lastPage) );
|
|
152 out.print( "</a>\r\n</span>\r\n" );
|
|
153 }
|
|
154 out.print( "\r\n" );
|
|
155
|
|
156 }
|
|
157 } else {
|
|
158 // Among the last pages
|
|
159
|
|
160 out.print( "\r\n<span class=\"page\">\r\n <a href=\"" );
|
|
161 out.print( (pagingPath.path(0)) );
|
|
162 out.print( "\" title=\"page 1\">1</a>\r\n</span>\r\n" );
|
|
163 out.print( (before == 2? "" : "...") );
|
|
164 out.print( "\r\n" );
|
|
165
|
|
166 for (int i = before; i <= lastPage; i++) {
|
|
167 if (i == currentPage) {
|
|
168
|
|
169 out.print( "<span class=\"current-page page medium-border-color\">" );
|
|
170 out.print( (i) );
|
|
171 out.print( "</span>" );
|
|
172
|
|
173 } else {
|
|
174 int page = (i-1) * maxRows;
|
|
175
|
|
176 out.print( "\r\n <span class=\"page\">\r\n <a href=\"" );
|
|
177 out.print( (pagingPath.path(page)) );
|
|
178 out.print( "\" title=\"page " );
|
|
179 out.print( (i) );
|
|
180 out.print( "\">" );
|
|
181 out.print( (i) );
|
|
182 out.print( "</a>\r\n </span>\r\n" );
|
|
183
|
|
184 }
|
|
185 }
|
|
186 }
|
|
187
|
|
188 out.print( "\r\n</span>\r\n" );
|
|
189
|
|
190 }
|
|
191 }
|
|
192
|
|
193 public static void googleAnalytics(PrintWriter out) {
|
|
194
|
|
195 out.print( "\r\n<script>\r\n (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){\r\n (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),\r\n m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)\r\n })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');\r\n \r\n ga('create', 'UA-91855-9', 'auto', 'nabble');\r\n ga('nabble.send', 'pageview');\r\n</script>\r\n" );
|
|
196
|
|
197 }
|
|
198
|
|
199 }
|
|
200
|