]>
Commit | Line | Data |
---|---|---|
1 | import { SortMeta } from 'primeng/api' | |
2 | import { HttpParams } from '@angular/common/http' | |
3 | import { Injectable } from '@angular/core' | |
4 | import { ComponentPaginationLight } from './component-pagination.model' | |
5 | import { RestPagination } from './rest-pagination' | |
6 | ||
7 | interface QueryStringFilterPrefixes { | |
8 | [key: string]: { | |
9 | prefix: string | |
10 | handler?: (v: string) => string | number | |
11 | multiple?: boolean | |
12 | isBoolean?: boolean | |
13 | } | |
14 | } | |
15 | ||
16 | type ParseQueryStringFilterResult = { | |
17 | [key: string]: string | number | boolean | (string | number | boolean)[] | |
18 | } | |
19 | ||
20 | @Injectable() | |
21 | export class RestService { | |
22 | ||
23 | addRestGetParams (params: HttpParams, pagination?: RestPagination, sort?: SortMeta | string) { | |
24 | let newParams = params | |
25 | ||
26 | if (pagination !== undefined) { | |
27 | newParams = newParams.set('start', pagination.start.toString()) | |
28 | .set('count', pagination.count.toString()) | |
29 | } | |
30 | ||
31 | if (sort !== undefined) { | |
32 | let sortString = '' | |
33 | ||
34 | if (typeof sort === 'string') { | |
35 | sortString = sort | |
36 | } else { | |
37 | const sortPrefix = sort.order === 1 ? '' : '-' | |
38 | sortString = sortPrefix + sort.field | |
39 | } | |
40 | ||
41 | newParams = newParams.set('sort', sortString) | |
42 | } | |
43 | ||
44 | return newParams | |
45 | } | |
46 | ||
47 | addObjectParams (params: HttpParams, object: { [ name: string ]: any }) { | |
48 | for (const name of Object.keys(object)) { | |
49 | const value = object[name] | |
50 | if (value === undefined || value === null) continue | |
51 | ||
52 | if (Array.isArray(value)) { | |
53 | for (const v of value) params = params.append(name, v) | |
54 | } else { | |
55 | params = params.append(name, value) | |
56 | } | |
57 | } | |
58 | ||
59 | return params | |
60 | } | |
61 | ||
62 | componentPaginationToRestPagination (componentPagination: ComponentPaginationLight): RestPagination { | |
63 | const start: number = (componentPagination.currentPage - 1) * componentPagination.itemsPerPage | |
64 | const count: number = componentPagination.itemsPerPage | |
65 | ||
66 | return { start, count } | |
67 | } | |
68 | ||
69 | parseQueryStringFilter (q: string, prefixes: QueryStringFilterPrefixes): ParseQueryStringFilterResult { | |
70 | if (!q) return {} | |
71 | ||
72 | // Tokenize the strings using spaces that are not in quotes | |
73 | const tokens = q.match(/(?:[^\s"]+|"[^"]*")+/g) | |
74 | .filter(token => !!token) | |
75 | ||
76 | // Build prefix array | |
77 | const prefixeStrings = Object.values(prefixes) | |
78 | .map(p => p.prefix) | |
79 | ||
80 | // Search is the querystring minus defined filters | |
81 | const searchTokens = tokens.filter(t => { | |
82 | return prefixeStrings.every(prefixString => t.startsWith(prefixString) === false) | |
83 | }) | |
84 | ||
85 | const additionalFilters: ParseQueryStringFilterResult = {} | |
86 | ||
87 | for (const prefixKey of Object.keys(prefixes)) { | |
88 | const prefixObj = prefixes[prefixKey] | |
89 | const prefix = prefixObj.prefix | |
90 | ||
91 | const matchedTokens = tokens.filter(t => t.startsWith(prefix)) | |
92 | .map(t => t.slice(prefix.length)) // Keep the value filter | |
93 | .map(t => t.replace(/^"|"$/g, '')) | |
94 | .map(t => { | |
95 | if (prefixObj.handler) return prefixObj.handler(t) | |
96 | ||
97 | return t | |
98 | }) | |
99 | .filter(t => !!t || t === 0) | |
100 | .map(t => prefixObj.isBoolean ? t === 'true' : t) | |
101 | ||
102 | if (matchedTokens.length === 0) continue | |
103 | ||
104 | additionalFilters[prefixKey] = prefixObj.multiple === true | |
105 | ? matchedTokens | |
106 | : matchedTokens[0] | |
107 | } | |
108 | ||
109 | return { | |
110 | search: searchTokens.join(' ') || undefined, | |
111 | ||
112 | ...additionalFilters | |
113 | } | |
114 | } | |
115 | } |