diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-01-30 22:41:14 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-01-30 22:41:14 +0100 |
commit | 28798b5d949826551740fc893d06e6424b77aa6a (patch) | |
tree | e235a7f49164a06c4b76df49ca61b89998d4ed81 /client/src/app/shared/rest/rest-data-source.ts | |
parent | 13fc89f4a4b91b3da10493517de556240fb65463 (diff) | |
download | PeerTube-28798b5d949826551740fc893d06e6424b77aa6a.tar.gz PeerTube-28798b5d949826551740fc893d06e6424b77aa6a.tar.zst PeerTube-28798b5d949826551740fc893d06e6424b77aa6a.zip |
Client: replace simple tables by ng2 smart table component
Diffstat (limited to 'client/src/app/shared/rest/rest-data-source.ts')
-rw-r--r-- | client/src/app/shared/rest/rest-data-source.ts | 51 |
1 files changed, 51 insertions, 0 deletions
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 | } | ||