Mercurial Hosting > nabble
diff src/nabble/view/lib/HtmlViewUtils.java @ 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.java Thu Mar 21 19:15:52 2019 -0600 @@ -0,0 +1,200 @@ + +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) { + + 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:" ); + out.print( (margins) ); + out.print( ";font-weight:normal\">\r\n " ); + + 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) { + + out.print( "\r\n<span class=\"current-page page medium-border-color\">" ); + out.print( (i) ); + out.print( "</span>\r\n" ); + + } else { + int page = (i-1) * maxRows; + + out.print( "\r\n <span class=\"page\">\r\n <a href=\"" ); + out.print( (pagingPath.path(page)) ); + out.print( "\" title=\"page " ); + out.print( (i) ); + out.print( "\">" ); + out.print( (i) ); + out.print( "</a>\r\n </span>\r\n" ); + + } + } + + if (limit < lastPage) { + int page = (lastPage-1) * maxRows; + boolean atEnd = after == lastPage-1; + + out.print( "\r\n" ); + out.print( (atEnd? "" : "...") ); + out.print( "\r\n" ); + if (!hideLastPage || atEnd) { + out.print( "\r\n<span class=\"page\">\r\n <a href=\"" ); + out.print( (pagingPath.path(page)) ); + out.print( "\" title=\"page " ); + out.print( (lastPage) ); + out.print( "\"" ); + out.print( (atEnd? "" : " rel=\"nofollow\"") ); + out.print( ">" ); + out.print( (lastPage) ); + out.print( "</a>\r\n</span>\r\n" ); + } + out.print( "\r\n" ); + + } + } else if (before > 1 && after < lastPage) { + // In the middle pages + + out.print( "\r\n<span class=\"page\">\r\n <a href=\"" ); + out.print( (pagingPath.path(0)) ); + out.print( "\" title=\"page 1\">1</a>\r\n</span>\r\n" ); + out.print( (before == 2? "" : "...") ); + out.print( "\r\n" ); + + int limit = before + 2 * NEIGHBOR_PAGES; + for (int i = before; i <= limit; i++) { + if (i == currentPage) { + + out.print( "<span class=\"current-page page medium-border-color\">" ); + out.print( (i) ); + out.print( "</span>" ); + + } else { + int page = (i-1) * maxRows; + + out.print( "\r\n <span class=\"page\">\r\n <a href=\"" ); + out.print( (pagingPath.path(page)) ); + out.print( "\" title=\"page " ); + out.print( (i) ); + out.print( "\">" ); + out.print( (i) ); + out.print( "</a>\r\n </span>\r\n" ); + + } + } + + if (limit < lastPage) { + int page = (lastPage-1) * maxRows; + boolean atEnd = after == lastPage-1; + + out.print( "\r\n" ); + out.print( (atEnd? "" : "...") ); + out.print( "\r\n" ); + if (!hideLastPage || atEnd) { + out.print( "\r\n<span class=\"page\">\r\n <a href=\"" ); + out.print( (pagingPath.path(page)) ); + out.print( "\" title=\"page " ); + out.print( (lastPage) ); + out.print( "\"" ); + out.print( (atEnd? "" : " rel=\"nofollow\"") ); + out.print( ">" ); + out.print( (lastPage) ); + out.print( "</a>\r\n</span>\r\n" ); + } + out.print( "\r\n" ); + + } + } else { + // Among the last pages + + out.print( "\r\n<span class=\"page\">\r\n <a href=\"" ); + out.print( (pagingPath.path(0)) ); + out.print( "\" title=\"page 1\">1</a>\r\n</span>\r\n" ); + out.print( (before == 2? "" : "...") ); + out.print( "\r\n" ); + + for (int i = before; i <= lastPage; i++) { + if (i == currentPage) { + + out.print( "<span class=\"current-page page medium-border-color\">" ); + out.print( (i) ); + out.print( "</span>" ); + + } else { + int page = (i-1) * maxRows; + + out.print( "\r\n <span class=\"page\">\r\n <a href=\"" ); + out.print( (pagingPath.path(page)) ); + out.print( "\" title=\"page " ); + out.print( (i) ); + out.print( "\">" ); + out.print( (i) ); + out.print( "</a>\r\n </span>\r\n" ); + + } + } + } + + out.print( "\r\n</span>\r\n" ); + + } + } + + public static void googleAnalytics(PrintWriter out) { + + 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" ); + + } + +} +