diff options
Diffstat (limited to 'client/src/app/shared/rest')
-rw-r--r-- | client/src/app/shared/rest/index.ts | 1 | ||||
-rw-r--r-- | client/src/app/shared/rest/rest-data-source.ts | 51 |
2 files changed, 52 insertions, 0 deletions
diff --git a/client/src/app/shared/rest/index.ts b/client/src/app/shared/rest/index.ts index 3c9509dc7..3cb123c3b 100644 --- a/client/src/app/shared/rest/index.ts +++ b/client/src/app/shared/rest/index.ts | |||
@@ -1,3 +1,4 @@ | |||
1 | export * from './rest-data-source'; | ||
1 | export * from './rest-extractor.service'; | 2 | export * from './rest-extractor.service'; |
2 | export * from './rest-pagination'; | 3 | export * from './rest-pagination'; |
3 | export * from './rest.service'; | 4 | export * from './rest.service'; |
diff --git a/client/src/app/shared/rest/rest-data-source.ts b/client/src/app/shared/rest/rest-data-source.ts new file mode 100644 index 000000000..847dd7c56 --- /dev/null +++ b/client/src/app/shared/rest/rest-data-source.ts | |||
@@ -0,0 +1,51 @@ | |||
1 | import { Http, RequestOptionsArgs, URLSearchParams, } from '@angular/http'; | ||
2 | |||
3 | import { ServerDataSource } from 'ng2-smart-table'; | ||
4 | |||
5 | export class RestDataSource extends ServerDataSource { | ||
6 | constructor(http: Http, endpoint: string) { | ||
7 | const options = { | ||
8 | endPoint: endpoint, | ||
9 | sortFieldKey: 'sort', | ||
10 | dataKey: 'data' | ||
11 | } | ||
12 | |||
13 | super(http, options); | ||
14 | } | ||
15 | |||
16 | protected extractTotalFromResponse(res) { | ||
17 | const rawData = res.json(); | ||
18 | return rawData ? parseInt(rawData.total): 0; | ||
19 | } | ||
20 | |||
21 | protected addSortRequestOptions(requestOptions: RequestOptionsArgs) { | ||
22 | let searchParams: URLSearchParams = <URLSearchParams> requestOptions.search; | ||
23 | |||
24 | if (this.sortConf) { | ||
25 | this.sortConf.forEach((fieldConf) => { | ||
26 | const sortPrefix = fieldConf.direction === 'desc' ? '-' : ''; | ||
27 | |||
28 | searchParams.set(this.conf.sortFieldKey, sortPrefix + fieldConf.field); | ||
29 | }); | ||
30 | } | ||
31 | |||
32 | return requestOptions; | ||
33 | } | ||
34 | |||
35 | protected addPagerRequestOptions(requestOptions: RequestOptionsArgs) { | ||
36 | let searchParams: URLSearchParams = <URLSearchParams> requestOptions.search; | ||
37 | |||
38 | if (this.pagingConf && this.pagingConf['page'] && this.pagingConf['perPage']) { | ||
39 | const perPage = this.pagingConf['perPage']; | ||
40 | const page = this.pagingConf['page']; | ||
41 | |||
42 | const start = (page - 1) * perPage; | ||
43 | const count = perPage; | ||
44 | |||
45 | searchParams.set('start', start.toString()); | ||
46 | searchParams.set('count', count.toString()); | ||
47 | } | ||
48 | |||
49 | return requestOptions; | ||
50 | } | ||
51 | } | ||