|
1 |
| -import { ChangeDetectorRef, Component, OnInit } from '@angular/core'; |
| 1 | +import { Component } from '@angular/core'; |
2 | 2 | import { ActivatedRoute } from '@angular/router';
|
3 |
| -import { Observable } from 'rxjs'; |
4 |
| -import { map, switchMap, tap } from 'rxjs/operators'; |
5 |
| - |
6 |
| -import { LuCoreDataService } from '../../services'; |
7 |
| -import { DB_ObjectRef_ByType } from '../../../defs/cdclient'; |
8 |
| - |
9 |
| -interface ObjectsByTypeEmbedded { |
10 |
| - objects: {[key: number]: {id: number, name: number}}; |
11 |
| -} |
12 |
| - |
13 |
| -interface ObjectsByType { |
14 |
| - object_ids: number[]; |
15 |
| - _embedded: ObjectsByTypeEmbedded; |
16 |
| -} |
17 | 3 |
|
18 | 4 | @Component({
|
19 | 5 | selector: 'app-by-type',
|
20 | 6 | templateUrl: './by-type.component.html',
|
21 | 7 | styleUrls: ['./by-type.component.css']
|
22 | 8 | })
|
23 |
| -export class ObjectsByTypeComponent implements OnInit { |
24 |
| - |
25 |
| - page_size: number = 100; |
26 |
| - page: number = -1; |
27 |
| - page_count: number = -1; |
| 9 | +export class ObjectsByTypeComponent { |
28 | 10 | type: string = "";
|
29 |
| - objects: Observable<DB_ObjectRef_ByType[]>; |
30 |
| - count: Observable<number[]>; |
31 |
| - |
32 |
| - constructor( |
33 |
| - private luCoreData: LuCoreDataService, |
34 |
| - private route: ActivatedRoute, |
35 |
| - private cd: ChangeDetectorRef, |
36 |
| - ) { } |
37 |
| - |
38 |
| - ngOnInit() { |
39 |
| - this.objects = this.route.paramMap |
40 |
| - .pipe(map(this.mapRouteInfo), tap(this.tapRef.bind(this)), switchMap(this.loadDataObservable.bind(this))) |
41 |
| - this.objects.subscribe(x => this.cd.detectChanges()); |
42 |
| - } |
43 |
| - |
44 |
| - mapRouteInfo(map) { |
45 |
| - let type = map.get('type'); |
46 |
| - if (map.has('page')) { |
47 |
| - let page = map.get('page'); |
48 |
| - return { type: type, page: +page }; |
49 |
| - } else { |
50 |
| - return { type: type, page: 0 }; |
51 |
| - } |
52 |
| - } |
| 11 | + page: number = 0; |
53 | 12 |
|
54 |
| - tapRef(ref) { |
55 |
| - this.type = ref.type; |
56 |
| - this.page = ref.page; |
57 |
| - this.cd.detectChanges(); |
| 13 | + constructor(private route: ActivatedRoute) { |
| 14 | + this.route.params.subscribe(p => { |
| 15 | + this.type = p.type; |
| 16 | + this.page = Number(p.page); |
| 17 | + }); |
58 | 18 | }
|
59 |
| - |
60 |
| - loadDataObservable(ref) { |
61 |
| - return this.luCoreData.getRevEntry<ObjectsByType>('object_types', ref.type) |
62 |
| - .pipe( |
63 |
| - map((v: ObjectsByType) => v.object_ids.map(id => { |
64 |
| - return { id, name: v._embedded.objects[id].name }; |
65 |
| - })), |
66 |
| - tap(this.setPageCounters.bind(this, ref)), |
67 |
| - map(this.processData.bind(this, ref)) |
68 |
| - ); |
69 |
| - } |
70 |
| - |
71 |
| - setPageCounters(ref, data) { |
72 |
| - this.page_count = Math.ceil(data.length / this.page_size); |
73 |
| - } |
74 |
| - |
75 |
| - processData(ref, data) { |
76 |
| - let sorted = data.sort(this.sortObjectTypeRefs); |
77 |
| - let from = this.page_size * ref.page; |
78 |
| - let to = from + this.page_size; |
79 |
| - let page = sorted.slice(from, to); |
80 |
| - return page; |
81 |
| - } |
82 |
| - |
83 |
| - sortObjectTypeRefs(a, b) { |
84 |
| - return a.id - b.id; |
85 |
| - } |
86 |
| - |
87 |
| - |
88 |
| - |
89 | 19 | }
|
0 commit comments