diff src/nabble/view/web/template/PagingNamespace.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/web/template/PagingNamespace.java	Thu Mar 21 19:15:52 2019 -0600
@@ -0,0 +1,151 @@
+package nabble.view.web.template;
+
+import nabble.naml.compiler.Command;
+import nabble.naml.compiler.IPrintWriter;
+import nabble.naml.namespaces.IntegerNamespace;
+import nabble.naml.compiler.Interpreter;
+import nabble.naml.compiler.ScopedInterpreter;
+import nabble.naml.compiler.CommandSpec;
+import nabble.naml.compiler.Namespace;
+
+
+@Namespace (
+	name = "paging",
+	global = true
+)
+public final class PagingNamespace {
+	private final int totalRows;
+	private final int rowsPerPage;
+	private final int currentPage;
+	private final int lastPage;
+	private final int before;
+	private final int after;
+
+	PagingNamespace(int totalRows,int currentRow,int rowsPerPage,int neighborPages) {
+		this.totalRows = totalRows;
+		this.rowsPerPage = rowsPerPage;
+
+		this.currentPage = (currentRow / rowsPerPage) + 1;
+		this.lastPage = (totalRows + rowsPerPage - 1) / rowsPerPage;
+		neighborPages++;
+		int before = currentPage - neighborPages;
+		if( before > 1 )
+			before++;
+		this.before = before;
+		int after = currentPage + neighborPages;
+		if( after < lastPage )
+			after--;
+		this.after = after;
+	}
+
+
+	@Command public void rows_per_page(IPrintWriter out,Interpreter interp) {
+		out.print( rowsPerPage );
+	}
+
+	/* Prints the number of rows the current page is meant to display */
+	@Command public void current_page_rows(IPrintWriter out,Interpreter interp) {
+		boolean hasNextPage = currentPage < lastPage;
+		out.print( hasNextPage? rowsPerPage : totalRows % rowsPerPage);
+	}
+
+	@Command public void current_page_number(IPrintWriter out,Interpreter interp) {
+		out.print( currentPage );
+	}
+
+	@Command public void has_paging(IPrintWriter out,Interpreter interp) {
+		out.print( totalRows > rowsPerPage );
+	}
+
+	@Command public void is_at_beginning(IPrintWriter out,Interpreter interp) {
+		out.print( before <= 1 );
+	}
+
+	@Command public void is_at_end(IPrintWriter out,Interpreter interp) {
+		out.print( after >= lastPage );
+	}
+
+	public static final CommandSpec neighboring_pages = CommandSpec.DO;
+
+	@Command public void neighboring_pages(IPrintWriter out,ScopedInterpreter<Page> interp) {
+		final int start = Math.max(1,before);
+		final int limit = Math.min(after,lastPage);
+		for( int i = start; i <= limit; i++ ) {
+			out.print( interp.getArg(page(i),"do") );
+		}
+	}
+
+	public static final CommandSpec current_page = CommandSpec.DO;
+
+	@Command public void current_page(IPrintWriter out,ScopedInterpreter<Page> interp) {
+		out.print( interp.getArg(page(currentPage),"do") );
+	}
+
+	public static final CommandSpec first_page = CommandSpec.DO;
+
+	@Command public void first_page(IPrintWriter out,ScopedInterpreter<Page> interp) {
+		out.print( interp.getArg(page(1),"do") );
+	}
+
+	public static final CommandSpec last_page = CommandSpec.DO;
+
+	@Command public void last_page(IPrintWriter out,ScopedInterpreter<Page> interp) {
+		out.print( interp.getArg(page(lastPage),"do") );
+	}
+
+	@Command public void has_previous_page(IPrintWriter out,Interpreter interp) {
+		out.print( currentPage > 1 );
+	}
+
+	public static final CommandSpec previous_page = CommandSpec.DO;
+
+	@Command public void previous_page(IPrintWriter out,ScopedInterpreter<Page> interp) {
+		out.print( interp.getArg(page(currentPage-1),"do") );
+	}
+
+	@Command public void has_next_page(IPrintWriter out,Interpreter interp) {
+		out.print( currentPage < lastPage );
+	}
+
+	public static final CommandSpec next_page = CommandSpec.DO;
+
+	@Command public void next_page(IPrintWriter out,ScopedInterpreter<Page> interp) {
+		out.print( interp.getArg(page(currentPage+1),"do") );
+	}
+
+	private Page page(int number) {
+		return new Page( number, number==currentPage, (number-1) * rowsPerPage );
+	}
+
+	@Namespace (
+		name = "paging_page",
+		global = true,
+		transparent = true
+	)
+	public static final class Page {
+		private final int number;
+		private final boolean isCurrent;
+		private final int row;
+
+		private Page(int number,boolean isCurrent,int row) {
+			this.number = number;
+			this.isCurrent = isCurrent;
+			this.row = row;
+		}
+
+		@Command public void page_number(IPrintWriter out,Interpreter interp) {
+			out.print( number );
+		}
+
+		@Command public void is_current_page(IPrintWriter out,Interpreter interp) {
+			out.print( isCurrent );
+		}
+
+		public static final CommandSpec page_row = CommandSpec.DO;
+
+		@Command public void page_row(IPrintWriter out,ScopedInterpreter<IntegerNamespace> interp) {
+			out.print( interp.getArg(new IntegerNamespace(row),"do") );
+		}
+
+	}
+}