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 <style type="text/css">
|
|
48 span.page { padding: .1em .3em }
|
|
49 span.current-page { border-width:1px; border-style:solid; }
|
|
50 </style>
|
|
51 <span style="float:right;white-space:nowrap;padding:<%=margins%>;font-weight:normal">
|
|
52 <%
|
|
53 int currentPage = (currentRecord / maxRows) + 1;
|
|
54 int lastPage = count / maxRows;
|
|
55 // If there are more items on the next page...
|
|
56 if (count % maxRows > 0)
|
|
57 lastPage++; // ... I increment the last page.
|
|
58
|
|
59 int before = currentPage - NEIGHBOR_PAGES;
|
|
60 int after = currentPage + NEIGHBOR_PAGES;
|
|
61 if (before <= 1) {
|
|
62 // Among the first pages
|
|
63 int limit = Math.min(currentPage + NEIGHBOR_PAGES, lastPage);
|
|
64
|
|
65 for (int i = 1; i <= limit; i++) {
|
|
66 if (i == currentPage) {
|
|
67 %>
|
|
68 <span class="current-page page medium-border-color"><%=i%></span>
|
|
69 <%
|
|
70 } else {
|
|
71 int page = (i-1) * maxRows;
|
|
72 %>
|
|
73 <span class="page">
|
|
74 <a href="<%=pagingPath.path(page)%>" title="page <%=i%>"><%=i%></a>
|
|
75 </span>
|
|
76 <%
|
|
77 }
|
|
78 }
|
|
79
|
|
80 if (limit < lastPage) {
|
|
81 int page = (lastPage-1) * maxRows;
|
|
82 boolean atEnd = after == lastPage-1;
|
|
83 %>
|
|
84 <%=atEnd? "" : "..."%>
|
|
85 <% if (!hideLastPage || atEnd) { %>
|
|
86 <span class="page">
|
|
87 <a href="<%=pagingPath.path(page)%>" title="page <%=lastPage%>"<%=atEnd? "" : " rel=\"nofollow\""%>><%=lastPage%></a>
|
|
88 </span>
|
|
89 <% } %>
|
|
90 <%
|
|
91 }
|
|
92 } else if (before > 1 && after < lastPage) {
|
|
93 // In the middle pages
|
|
94 %>
|
|
95 <span class="page">
|
|
96 <a href="<%=pagingPath.path(0)%>" title="page 1">1</a>
|
|
97 </span>
|
|
98 <%=before == 2? "" : "..."%>
|
|
99 <%
|
|
100 int limit = before + 2 * NEIGHBOR_PAGES;
|
|
101 for (int i = before; i <= limit; i++) {
|
|
102 if (i == currentPage) {
|
|
103 %><span class="current-page page medium-border-color"><%=i%></span><%
|
|
104 } else {
|
|
105 int page = (i-1) * maxRows;
|
|
106 %>
|
|
107 <span class="page">
|
|
108 <a href="<%=pagingPath.path(page)%>" title="page <%=i%>"><%=i%></a>
|
|
109 </span>
|
|
110 <%
|
|
111 }
|
|
112 }
|
|
113
|
|
114 if (limit < lastPage) {
|
|
115 int page = (lastPage-1) * maxRows;
|
|
116 boolean atEnd = after == lastPage-1;
|
|
117 %>
|
|
118 <%=atEnd? "" : "..."%>
|
|
119 <% if (!hideLastPage || atEnd) { %>
|
|
120 <span class="page">
|
|
121 <a href="<%=pagingPath.path(page)%>" title="page <%=lastPage%>"<%=atEnd? "" : " rel=\"nofollow\""%>><%=lastPage%></a>
|
|
122 </span>
|
|
123 <% } %>
|
|
124 <%
|
|
125 }
|
|
126 } else {
|
|
127 // Among the last pages
|
|
128 %>
|
|
129 <span class="page">
|
|
130 <a href="<%=pagingPath.path(0)%>" title="page 1">1</a>
|
|
131 </span>
|
|
132 <%=before == 2? "" : "..."%>
|
|
133 <%
|
|
134 for (int i = before; i <= lastPage; i++) {
|
|
135 if (i == currentPage) {
|
|
136 %><span class="current-page page medium-border-color"><%=i%></span><%
|
|
137 } else {
|
|
138 int page = (i-1) * maxRows;
|
|
139 %>
|
|
140 <span class="page">
|
|
141 <a href="<%=pagingPath.path(page)%>" title="page <%=i%>"><%=i%></a>
|
|
142 </span>
|
|
143 <%
|
|
144 }
|
|
145 }
|
|
146 }
|
|
147 %>
|
|
148 </span>
|
|
149 <%
|
|
150 }
|
|
151 }
|
|
152
|
|
153 public static void googleAnalytics(PrintWriter out) {
|
|
154 %>
|
|
155 <script>
|
|
156 (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
|
157 (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
|
158 m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
|
159 })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
|
|
160
|
|
161 ga('create', 'UA-91855-9', 'auto', 'nabble');
|
|
162 ga('nabble.send', 'pageview');
|
|
163 </script>
|
|
164 <%
|
|
165 }
|
|
166
|
|
167 }
|
|
168 %>
|