|
| 1 | +import { Injectable } from '@angular/core'; |
| 2 | +import { |
| 3 | + XRepositoryAbstract, |
| 4 | + XQuery, |
| 5 | + XResultList, |
| 6 | + XGroupItem, |
| 7 | + XFilter, |
| 8 | + XChunk, |
| 9 | + XGroupBy, |
| 10 | + XSort, |
| 11 | + XId, |
| 12 | + XOrderBy |
| 13 | +} from '@ng-nest/ui/core'; |
| 14 | +import { Observable } from 'rxjs'; |
| 15 | + |
| 16 | +@Injectable() |
| 17 | +export class InnerHTMLService extends XRepositoryAbstract { |
| 18 | + organizations = ['Manufacturing Center', 'R&D Center', 'Finance Center', 'Marketing Center', 'Administrative Center']; |
| 19 | + positions = ['Technician', 'Sales', 'Manager', 'Director', 'Production']; |
| 20 | + users: User[] = Array.from({ length: 123456 }).map((_x, i) => { |
| 21 | + i++; |
| 22 | + return { |
| 23 | + id: i, |
| 24 | + name: `<span style="color: red">name${i}</span>`, |
| 25 | + position: this.positions[Math.floor(Math.random() * 10 + 1) % 5], |
| 26 | + email: `<span>email${i}</span>`, |
| 27 | + phone: 'phone' + i, |
| 28 | + organization: this.organizations[Math.floor(Math.random() * 10 + 1) % 5] |
| 29 | + }; |
| 30 | + }); |
| 31 | + |
| 32 | + getList(index: number, size: number, query?: XQuery): Observable<XResultList<User | XGroupItem>> { |
| 33 | + return new Observable((x) => { |
| 34 | + let data: User[] | XGroupItem[] = []; |
| 35 | + data = this.setFilter(this.users, query?.filter as XFilter[]); |
| 36 | + if (query?.group) { |
| 37 | + data = this.setGroup(data, query.group); |
| 38 | + } |
| 39 | + if (query?.sort) { |
| 40 | + data = this.setSort(data, query.sort); |
| 41 | + } |
| 42 | + let chunks = XChunk(data, size); |
| 43 | + if ((index as number) <= chunks.length) { |
| 44 | + x.next({ total: data.length, list: chunks[index - 1] }); |
| 45 | + } else { |
| 46 | + x.next({ total: data.length, list: [] }); |
| 47 | + } |
| 48 | + x.complete(); |
| 49 | + }); |
| 50 | + } |
| 51 | + get(_id: number | string): Observable<User> { |
| 52 | + return new Observable(); |
| 53 | + } |
| 54 | + post(_entity: User): Observable<User> { |
| 55 | + return new Observable(); |
| 56 | + } |
| 57 | + put(_entity: User): Observable<User> { |
| 58 | + return new Observable(); |
| 59 | + } |
| 60 | + delete(_id: number | string): Observable<boolean> { |
| 61 | + return new Observable(); |
| 62 | + } |
| 63 | + |
| 64 | + private setFilter(data: User[], filters: XFilter[]): User[] { |
| 65 | + let result = data; |
| 66 | + if (filters && filters.length > 0) { |
| 67 | + filters.forEach((x) => { |
| 68 | + result = result.filter((y) => y[x.field!].indexOf(x.value) >= 0); |
| 69 | + }); |
| 70 | + } |
| 71 | + return result; |
| 72 | + } |
| 73 | + |
| 74 | + private setGroup(data: User[], group: string): XGroupItem[] { |
| 75 | + return XGroupBy(data, group).map((value, key) => { |
| 76 | + let groupItem: XGroupItem = { id: key, count: value.length }; |
| 77 | + groupItem[group] = key; |
| 78 | + return groupItem; |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + private setSort(data: User[] | XGroupItem[], sort: XSort[]): User[] | XGroupItem[] { |
| 83 | + return XOrderBy( |
| 84 | + data, |
| 85 | + sort.map((x) => x.field!), |
| 86 | + sort.map((x) => x.value) as ('desc' | 'asc')[] |
| 87 | + ) as User[] | XGroupItem[]; |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +export interface User extends XId { |
| 92 | + name?: string; |
| 93 | + account?: string; |
| 94 | + password?: string; |
| 95 | + email?: string; |
| 96 | + phone?: string; |
| 97 | + organization?: string; |
| 98 | + [property: string]: any; |
| 99 | +} |
0 commit comments