Mercurial Hosting > nabble
diff src/nabble/view/lib/HtmlViewUtils.jtp @ 0:7ecd1a4ef557
add content
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 21 Mar 2019 19:15:52 -0600 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/nabble/view/lib/HtmlViewUtils.jtp Thu Mar 21 19:15:52 2019 -0600 @@ -0,0 +1,168 @@ +<% +package nabble.view.lib; + +import java.io.IOException; +import java.io.PrintWriter; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + + +// used outside nabble, so do not use Init +public final class HtmlViewUtils { + + private HtmlViewUtils() {} // never + + + /** Number of pages to be displayed before/after the current page*/ + public static final int NEIGHBOR_PAGES = 3; + + public static interface PagingPath { + public String path(int row); + } + + public static final class GenericPagingPath implements PagingPath { + private final String url; + + public GenericPagingPath(String url) { + this.url = url; + } + + public String path(int row) { + return url + (row==0 ? "" : "&i=" + row); + } + } + + public static void genericPaging(HttpServletRequest request,HttpServletResponse response, int count, int currentRecord, int maxRows, PagingPath pagingPath, String margins) + throws IOException + { + genericPaging(request, response, count, currentRecord, maxRows, pagingPath, margins, false, NEIGHBOR_PAGES); + } + + public static void genericPaging(HttpServletRequest request,HttpServletResponse response, int count, int currentRecord, int maxRows, PagingPath pagingPath, String margins, boolean hideLastPage,int NEIGHBOR_PAGES) + throws IOException + { + PrintWriter out = response.getWriter(); + if (count > maxRows) { + %> + <style type="text/css"> + span.page { padding: .1em .3em } + span.current-page { border-width:1px; border-style:solid; } + </style> + <span style="float:right;white-space:nowrap;padding:<%=margins%>;font-weight:normal"> + <% + int currentPage = (currentRecord / maxRows) + 1; + int lastPage = count / maxRows; + // If there are more items on the next page... + if (count % maxRows > 0) + lastPage++; // ... I increment the last page. + + int before = currentPage - NEIGHBOR_PAGES; + int after = currentPage + NEIGHBOR_PAGES; + if (before <= 1) { + // Among the first pages + int limit = Math.min(currentPage + NEIGHBOR_PAGES, lastPage); + + for (int i = 1; i <= limit; i++) { + if (i == currentPage) { + %> + <span class="current-page page medium-border-color"><%=i%></span> + <% + } else { + int page = (i-1) * maxRows; + %> + <span class="page"> + <a href="<%=pagingPath.path(page)%>" title="page <%=i%>"><%=i%></a> + </span> + <% + } + } + + if (limit < lastPage) { + int page = (lastPage-1) * maxRows; + boolean atEnd = after == lastPage-1; + %> + <%=atEnd? "" : "..."%> + <% if (!hideLastPage || atEnd) { %> + <span class="page"> + <a href="<%=pagingPath.path(page)%>" title="page <%=lastPage%>"<%=atEnd? "" : " rel=\"nofollow\""%>><%=lastPage%></a> + </span> + <% } %> + <% + } + } else if (before > 1 && after < lastPage) { + // In the middle pages + %> + <span class="page"> + <a href="<%=pagingPath.path(0)%>" title="page 1">1</a> + </span> + <%=before == 2? "" : "..."%> + <% + int limit = before + 2 * NEIGHBOR_PAGES; + for (int i = before; i <= limit; i++) { + if (i == currentPage) { + %><span class="current-page page medium-border-color"><%=i%></span><% + } else { + int page = (i-1) * maxRows; + %> + <span class="page"> + <a href="<%=pagingPath.path(page)%>" title="page <%=i%>"><%=i%></a> + </span> + <% + } + } + + if (limit < lastPage) { + int page = (lastPage-1) * maxRows; + boolean atEnd = after == lastPage-1; + %> + <%=atEnd? "" : "..."%> + <% if (!hideLastPage || atEnd) { %> + <span class="page"> + <a href="<%=pagingPath.path(page)%>" title="page <%=lastPage%>"<%=atEnd? "" : " rel=\"nofollow\""%>><%=lastPage%></a> + </span> + <% } %> + <% + } + } else { + // Among the last pages + %> + <span class="page"> + <a href="<%=pagingPath.path(0)%>" title="page 1">1</a> + </span> + <%=before == 2? "" : "..."%> + <% + for (int i = before; i <= lastPage; i++) { + if (i == currentPage) { + %><span class="current-page page medium-border-color"><%=i%></span><% + } else { + int page = (i-1) * maxRows; + %> + <span class="page"> + <a href="<%=pagingPath.path(page)%>" title="page <%=i%>"><%=i%></a> + </span> + <% + } + } + } + %> + </span> + <% + } + } + + public static void googleAnalytics(PrintWriter out) { + %> + <script> + (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ + (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), + m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) + })(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); + + ga('create', 'UA-91855-9', 'auto', 'nabble'); + ga('nabble.send', 'pageview'); + </script> + <% + } + +} +%>