-
Notifications
You must be signed in to change notification settings - Fork 18
/
AngularExample.ts
108 lines (96 loc) · 3.37 KB
/
AngularExample.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
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Subject } from 'rxjs/Subject';
import { ReplaySubject } from 'rxjs/ReplaySubject';
import { PaginationResult } from '../models/pagination-result';
import { DataSource } from '@angular/cdk';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/switchMap';
@Injectable()
export abstract class PaginationBaseService<T> extends DataSource<T> {
constructor(protected http: Http) {
super();
this.requestUrl
.distinctUntilChanged()
.switchMap((url: string) => {
return this.http.get(url);
})
.map((response: Response) => response.json() as PaginationResult<T>)
.subscribe(result => {
this.paginationResultSource.next(result);
this.lastPaginationResult = result;
});
}
protected paginationResultSource = new ReplaySubject<PaginationResult<T>>(1);
protected lastPaginationResult: PaginationResult<T>;
paginationResult = this.paginationResultSource.asObservable();
private requestUrl = new Subject<string>();
connect(): Observable<T[]> {
return this.paginationResult
.map(r => r.data);
}
disconnect(){ }
private _baseUrl: string = null;
get baseUrl(): string { return this._baseUrl; }
set baseUrl(value: string) {
if (value !== this._baseUrl) {
this._baseUrl = value;
this.refresh();
}
}
private _page: number = 1;
get page(): number { return this._page; }
set page(value: number) {
if (value !== this._page) {
this._page = value;
this.refresh();
}
}
private _pageSize: number = 20;
get pageSize(): number { return this._pageSize; }
set pageSize(value: number) {
if (value !== this._pageSize) {
this._pageSize = value;
this.refresh();
}
}
private _sort?: { propertyName: string, isDescending: boolean };
get sort(): { propertyName: string, isDescending: boolean } | null {
return this._sort;
}
set sort(value: { propertyName: string, isDescending: boolean }) {
this._sort = value;
this.refresh();
}
private _additionalQueryParams: { [parameter: string]: string } = {};
setQueryParameter(name: string, value: string) {
this._additionalQueryParams[name] = value;
this.refresh();
}
getQueryParameter(parameterName: string): string | null {
return this._additionalQueryParams[parameterName] || null;
}
private refresh() {
if (!this.baseUrl) {
return;
}
var url = this.buildUrl();
this.requestUrl.next(url);
}
private buildUrl(): string {
var url = `${this.baseUrl}?page=${this.page}&pageSize=${this.pageSize}`;
if (this.sort) {
var sortParam = `sort=${this.sort.propertyName} ${this.sort.isDescending ? 'desc' : 'asc'}`;
url += `&${sortParam}`;
}
for (var name in this._additionalQueryParams) {
if (this._additionalQueryParams.hasOwnProperty(name)) {
var value = this._additionalQueryParams[name];
if (value) {
url += `&${name}=${value}`;
}
}
}
return url;
}
}