/
slickpager.ts
168 lines (135 loc) · 5.72 KB
/
slickpager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { Fluent, localText } from "../../base";
import { PagerOptions } from "../../slick";
import { Decorators } from "../../types/decorators";
import { Widget, WidgetProps } from "../widgets/widget";
@Decorators.registerClass("Serenity.SlickPager")
export class SlickPager<P extends PagerOptions = PagerOptions> extends Widget<P> {
private currentPage: Fluent<HTMLInputElement>;
private totalPages: Fluent<HTMLSpanElement>;
private pageSize: Fluent<HTMLSelectElement>;
private stat: Fluent<HTMLSpanElement>;
constructor(props: WidgetProps<P>) {
super(props);
let opt = this.options;
opt.showRowsPerPage ??= true;
opt.rowsPerPageOptions ??= [20, 100, 500, 2000];
var v = opt.view; if (!v) throw "SlickPager requires view option to be set!";
let p = "slick-pg-";
let grp = (t: string) => Fluent("div").class(`${p}grp ${p}grp-${t}`);
let btn = (key: string) => Fluent("div").class(`${p}${key} ${p}btn`).append(Fluent("span").class(`${p}btn-span`));
let nav = (key: string) => btn(key).on("click", () => this._changePage(key));
let el = this.element.addClass("s-SlickPager slick-pg");
this.currentPage = Fluent("input")
.class(`${p}current mx-1`)
.attr("type", "text")
.attr("size", 4).attr("value", 1)
.on("keydown", e => { if (e.key === "Enter") this._changePage("input"); });
this.totalPages = Fluent("span").class(`${p}total`).text("1");
let control = grp("control").append(Fluent("span")
.class(`${p}control`)
.append(Fluent("span").class(`${p}pagetext`).text(localText("Controls.Pager.Page")))
.append(this.currentPage)
.append(Fluent("span").class(`${p}pagesep px-1`).text("/"))
.append(this.totalPages)
);
this.stat = Fluent("span").class(`${p}stat`);
let inner = Fluent("div").class(p + "in").appendTo(el)
.append(grp("firstprev")
.append(nav("first"))
.append(nav("prev")))
.append(control)
.append(grp("nextlast")
.append(nav("next"))
.append(nav("last")))
.append(grp("reload")
.append(btn("reload")
.on("click", () => v.populate())))
.append(grp("stat")
.append(this.stat));
if (this.options.showRowsPerPage) {
this.pageSize = Fluent("select")
.class(`${p}size`)
.attr("name", "rp")
.appendTo(grp("size").prependTo(inner))
.on("change", () => {
if (opt.onRowsPerPageChange)
opt.onRowsPerPageChange(+this.pageSize.val());
else {
v["newp"] = 1;
v.setPagingOptions({
page: 1,
rowsPerPage: +this.pageSize.val()
});
}
});
for (var rowsPerPage of opt.rowsPerPageOptions) {
Fluent("option")
.attr("value", rowsPerPage)
.attr("selected", v.rowsPerPage == rowsPerPage && "selected")
.text("" + rowsPerPage)
.appendTo(this.pageSize);
}
}
v.onPagingInfoChanged.subscribe(() => this._updatePager());
}
_changePage(ctype: string) { //change page
var view = this.options.view;
if (!view || view.loading)
return true;
var info = view.getPagingInfo();
var pages = (!info.rowsPerPage || !info.totalCount) ? 1 : Math.ceil(info.totalCount / info.rowsPerPage);
var newp: number;
switch (ctype) {
case 'first': newp = 1; break;
case 'prev': if (info.page > 1) newp = parseInt(info.page as any) - 1; break;
case 'next': if (info.page < pages) newp = parseInt(info.page as any) + 1; break;
case 'last': newp = pages; break;
case 'input':
var nv = parseInt(this.currentPage.val());
if (isNaN(nv))
nv = 1;
else if (nv < 1)
nv = 1;
else if (nv > pages)
nv = pages;
this.currentPage.val("" + nv);
newp = nv;
break;
}
if (newp == info.page)
return false;
if (this.options.onChangePage)
this.options.onChangePage(newp);
else {
view.setPagingOptions({ page: newp });
}
}
_updatePager() {
var view = this.options.view;
var info = view.getPagingInfo();
var pages = (!info.rowsPerPage || !info.totalCount) ? 1 : Math.ceil(info.totalCount / info.rowsPerPage);
this.currentPage.val(info.page);
this.totalPages.text("" + pages);
var r1 = (info.page - 1) * info.rowsPerPage + 1;
var r2 = r1 + info.rowsPerPage - 1;
if (info.totalCount < r2)
r2 = info.totalCount;
var stat: string;
if (info.loading) {
stat = localText("Controls.Pager.LoadingStatus");
}
else if (info.error) {
stat = info.error;
}
else if (info.totalCount > 0) {
stat = localText("Controls.Pager.PageStatus");
stat = stat.replace(/{from}/, <any>r1);
stat = stat.replace(/{to}/, <any>r2);
stat = stat.replace(/{total}/, <any>info.totalCount);
}
else
stat = localText("Controls.Pager.NoRowStatus");
this.stat.text(stat);
this.pageSize?.val((info.rowsPerPage || 0).toString());
}
}