comparison 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
comparison
equal deleted inserted replaced
-1:000000000000 0:7ecd1a4ef557
1 package nabble.view.web.template;
2
3 import nabble.naml.compiler.Command;
4 import nabble.naml.compiler.IPrintWriter;
5 import nabble.naml.namespaces.IntegerNamespace;
6 import nabble.naml.compiler.Interpreter;
7 import nabble.naml.compiler.ScopedInterpreter;
8 import nabble.naml.compiler.CommandSpec;
9 import nabble.naml.compiler.Namespace;
10
11
12 @Namespace (
13 name = "paging",
14 global = true
15 )
16 public final class PagingNamespace {
17 private final int totalRows;
18 private final int rowsPerPage;
19 private final int currentPage;
20 private final int lastPage;
21 private final int before;
22 private final int after;
23
24 PagingNamespace(int totalRows,int currentRow,int rowsPerPage,int neighborPages) {
25 this.totalRows = totalRows;
26 this.rowsPerPage = rowsPerPage;
27
28 this.currentPage = (currentRow / rowsPerPage) + 1;
29 this.lastPage = (totalRows + rowsPerPage - 1) / rowsPerPage;
30 neighborPages++;
31 int before = currentPage - neighborPages;
32 if( before > 1 )
33 before++;
34 this.before = before;
35 int after = currentPage + neighborPages;
36 if( after < lastPage )
37 after--;
38 this.after = after;
39 }
40
41
42 @Command public void rows_per_page(IPrintWriter out,Interpreter interp) {
43 out.print( rowsPerPage );
44 }
45
46 /* Prints the number of rows the current page is meant to display */
47 @Command public void current_page_rows(IPrintWriter out,Interpreter interp) {
48 boolean hasNextPage = currentPage < lastPage;
49 out.print( hasNextPage? rowsPerPage : totalRows % rowsPerPage);
50 }
51
52 @Command public void current_page_number(IPrintWriter out,Interpreter interp) {
53 out.print( currentPage );
54 }
55
56 @Command public void has_paging(IPrintWriter out,Interpreter interp) {
57 out.print( totalRows > rowsPerPage );
58 }
59
60 @Command public void is_at_beginning(IPrintWriter out,Interpreter interp) {
61 out.print( before <= 1 );
62 }
63
64 @Command public void is_at_end(IPrintWriter out,Interpreter interp) {
65 out.print( after >= lastPage );
66 }
67
68 public static final CommandSpec neighboring_pages = CommandSpec.DO;
69
70 @Command public void neighboring_pages(IPrintWriter out,ScopedInterpreter<Page> interp) {
71 final int start = Math.max(1,before);
72 final int limit = Math.min(after,lastPage);
73 for( int i = start; i <= limit; i++ ) {
74 out.print( interp.getArg(page(i),"do") );
75 }
76 }
77
78 public static final CommandSpec current_page = CommandSpec.DO;
79
80 @Command public void current_page(IPrintWriter out,ScopedInterpreter<Page> interp) {
81 out.print( interp.getArg(page(currentPage),"do") );
82 }
83
84 public static final CommandSpec first_page = CommandSpec.DO;
85
86 @Command public void first_page(IPrintWriter out,ScopedInterpreter<Page> interp) {
87 out.print( interp.getArg(page(1),"do") );
88 }
89
90 public static final CommandSpec last_page = CommandSpec.DO;
91
92 @Command public void last_page(IPrintWriter out,ScopedInterpreter<Page> interp) {
93 out.print( interp.getArg(page(lastPage),"do") );
94 }
95
96 @Command public void has_previous_page(IPrintWriter out,Interpreter interp) {
97 out.print( currentPage > 1 );
98 }
99
100 public static final CommandSpec previous_page = CommandSpec.DO;
101
102 @Command public void previous_page(IPrintWriter out,ScopedInterpreter<Page> interp) {
103 out.print( interp.getArg(page(currentPage-1),"do") );
104 }
105
106 @Command public void has_next_page(IPrintWriter out,Interpreter interp) {
107 out.print( currentPage < lastPage );
108 }
109
110 public static final CommandSpec next_page = CommandSpec.DO;
111
112 @Command public void next_page(IPrintWriter out,ScopedInterpreter<Page> interp) {
113 out.print( interp.getArg(page(currentPage+1),"do") );
114 }
115
116 private Page page(int number) {
117 return new Page( number, number==currentPage, (number-1) * rowsPerPage );
118 }
119
120 @Namespace (
121 name = "paging_page",
122 global = true,
123 transparent = true
124 )
125 public static final class Page {
126 private final int number;
127 private final boolean isCurrent;
128 private final int row;
129
130 private Page(int number,boolean isCurrent,int row) {
131 this.number = number;
132 this.isCurrent = isCurrent;
133 this.row = row;
134 }
135
136 @Command public void page_number(IPrintWriter out,Interpreter interp) {
137 out.print( number );
138 }
139
140 @Command public void is_current_page(IPrintWriter out,Interpreter interp) {
141 out.print( isCurrent );
142 }
143
144 public static final CommandSpec page_row = CommandSpec.DO;
145
146 @Command public void page_row(IPrintWriter out,ScopedInterpreter<IntegerNamespace> interp) {
147 out.print( interp.getArg(new IntegerNamespace(row),"do") );
148 }
149
150 }
151 }