page-link.ts
6.1 KB
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
169
170
171
172
173
174
175
176
177
178
179
///
/// Copyright © 2016-2020 The Thingsboard Authors
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
import { Direction, SortOrder } from '@shared/models/page/sort-order';
import { emptyPageData, PageData } from '@shared/models/page/page-data';
import { getDescendantProp, isObject } from '@core/utils';
import { SortDirection } from '@angular/material/sort';
export const MAX_SAFE_PAGE_SIZE = 2147483647;
export type PageLinkSearchFunction<T> = (entity: T, textSearch: string, searchProperty?: string) => boolean;
export function defaultPageLinkSearchFunction(searchProperty?: string): PageLinkSearchFunction<any> {
return (entity, textSearch) => defaultPageLinkSearch(entity, textSearch, searchProperty);
}
const defaultPageLinkSearch: PageLinkSearchFunction<any> =
(entity: any, textSearch: string, searchProperty?: string) => {
if (textSearch === null || !textSearch.length) {
return true;
}
const expected = ('' + textSearch).toLowerCase();
if (searchProperty && searchProperty.length) {
if (Object.prototype.hasOwnProperty.call(entity, searchProperty)) {
const val = entity[searchProperty];
if (val !== null) {
if (val !== Object(val)) {
const actual = ('' + val).toLowerCase();
if (actual.indexOf(expected) !== -1) {
return true;
}
}
}
}
} else {
for (const key of Object.keys(entity)) {
const val = entity[key];
if (val !== null) {
if (val !== Object(val)) {
const actual = ('' + val).toLowerCase();
if (actual.indexOf(expected) !== -1) {
return true;
}
} else if (isObject(val)) {
if (defaultPageLinkSearch(val, textSearch)) {
return true;
}
}
}
}
}
return false;
};
export class PageLink {
textSearch: string;
pageSize: number;
page: number;
sortOrder: SortOrder;
constructor(pageSize: number, page: number = 0, textSearch: string = null, sortOrder: SortOrder = null) {
this.textSearch = textSearch;
this.pageSize = pageSize;
this.page = page;
this.sortOrder = sortOrder;
}
public nextPageLink(): PageLink {
return new PageLink(this.pageSize, this.page + 1, this.textSearch, this.sortOrder);
}
public toQuery(): string {
let query = `?pageSize=${this.pageSize}&page=${this.page}`;
if (this.textSearch && this.textSearch.length) {
query += `&textSearch=${this.textSearch}`;
}
if (this.sortOrder) {
query += `&sortProperty=${this.sortOrder.property}&sortOrder=${this.sortOrder.direction}`;
}
return query;
}
public sort(item1: any, item2: any): number {
if (this.sortOrder) {
const property = this.sortOrder.property;
const item1Value = getDescendantProp(item1, property);
const item2Value = getDescendantProp(item2, property);
let result = 0;
if (item1Value !== item2Value) {
if (typeof item1Value === 'number' && typeof item2Value === 'number') {
result = item1Value - item2Value;
} else if (typeof item1Value === 'string' && typeof item2Value === 'string') {
result = item1Value.localeCompare(item2Value);
} else if (typeof item1Value === 'boolean' && typeof item2Value === 'boolean') {
if (item1Value && !item2Value) {
result = 1;
} else if (!item1Value && item2Value) {
result = -1;
}
} else if (typeof item1Value !== typeof item2Value) {
result = 1;
}
}
return this.sortOrder.direction === Direction.ASC ? result : result * -1;
}
return 0;
}
public filterData<T>(data: Array<T>,
searchFunction: PageLinkSearchFunction<T> = defaultPageLinkSearchFunction()): PageData<T> {
const pageData = emptyPageData<T>();
pageData.data = [...data];
if (this.textSearch && this.textSearch.length) {
pageData.data = pageData.data.filter((entity) => searchFunction(entity, this.textSearch));
}
pageData.totalElements = pageData.data.length;
pageData.totalPages = this.pageSize === Number.POSITIVE_INFINITY ? 1 : Math.ceil(pageData.totalElements / this.pageSize);
if (this.sortOrder) {
pageData.data = pageData.data.sort((a, b) => this.sort(a, b));
}
if (this.pageSize !== Number.POSITIVE_INFINITY) {
const startIndex = this.pageSize * this.page;
pageData.data = pageData.data.slice(startIndex, startIndex + this.pageSize);
pageData.hasNext = pageData.totalElements > startIndex + pageData.data.length;
}
return pageData;
}
public sortDirection(): SortDirection {
if (this.sortOrder) {
return (this.sortOrder.direction + '').toLowerCase() as SortDirection;
} else {
return '' as SortDirection;
}
}
}
export class TimePageLink extends PageLink {
startTime: number;
endTime: number;
constructor(pageSize: number, page: number = 0, textSearch: string = null, sortOrder: SortOrder = null,
startTime: number = null, endTime: number = null) {
super(pageSize, page, textSearch, sortOrder);
this.startTime = startTime;
this.endTime = endTime;
}
public nextPageLink(): TimePageLink {
return new TimePageLink(this.pageSize, this.page + 1, this.textSearch, this.sortOrder, this.startTime, this.endTime);
}
public toQuery(): string {
let query = super.toQuery();
if (this.startTime) {
query += `&startTime=${this.startTime}`;
}
if (this.endTime) {
query += `&endTime=${this.endTime}`;
}
return query;
}
}