]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/share/bib-list/bib-list.component.ts
LP1879335 Retrieve linked bibs on demand
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / share / bib-list / bib-list.component.ts
1 import {Component, Input, OnInit, ViewChild} from '@angular/core';
2 import {Observable, empty, from} from 'rxjs';
3 import {map, tap, switchMap} from 'rxjs/operators';
4 import {IdlObject} from '@eg/core/idl.service';
5 import {Pager} from '@eg/share/util/pager';
6 import {NetService} from '@eg/core/net.service';
7 import {PcrudService} from '@eg/core/pcrud.service';
8 import {OrgService} from '@eg/core/org.service';
9 import {GridComponent} from '@eg/share/grid/grid.component';
10 import {GridContext, GridDataSource, GridCellTextGenerator} from '@eg/share/grid/grid';
11 import {ComboboxEntry} from '@eg/share/combobox/combobox.component';
12
13
14 /* Grid of bib records and associated actions. */
15
16 @Component({
17   templateUrl: 'bib-list.component.html',
18   selector: 'eg-bib-list'
19 })
20 export class BibListComponent implements OnInit {
21
22     // Static source of bib record IDs
23     @Input() bibIds: number[];
24     // Dynamic source of bib record IDs
25     @Input() bibIdSource: (pager: Pager, sort: any) => Promise<number[]>;
26     @Input() gridPersistKey: string;
27
28     dataSource: GridDataSource;
29     cellTextGenerator: GridCellTextGenerator;
30
31     @ViewChild('grid', {static: false}) grid: GridComponent;
32
33     constructor(
34         private net: NetService,
35         private org: OrgService,
36         private pcrud: PcrudService
37     ) {
38     }
39
40     ngOnInit() {
41         this.dataSource = new GridDataSource();
42
43         this.dataSource.getRows = (pager: Pager, sort: any): Observable<any> => {
44
45             if (this.bibIds || this.bibIdSource) {
46                 return this.loadIds(pager, sort);
47             }
48
49             return empty();
50         };
51
52         this.cellTextGenerator = {
53             title: row => row.title
54         };
55     }
56
57     loadIds(pager: Pager, sort: any): Observable<any> {
58
59         let promise: Promise<number[]>;
60
61         if (this.bibIdSource) {
62             promise = this.bibIdSource(pager, sort);
63
64         } else if (this.bibIds && this.bibIds.length > 0) {
65             promise = Promise.resolve(
66               this.bibIds.slice(pager.offset, pager.offset + pager.limit));
67
68         } else {
69             return empty();
70         }
71
72         return from(promise).pipe(switchMap(bibIds => {
73
74             if (bibIds.length === 0) { return empty(); }
75
76             return this.pcrud.search('rmsr', {id: bibIds}, {
77                 order_by: {rmsr: 'id'},
78                 flesh: 2,
79                 flesh_fields: {
80                     rmsr: ['biblio_record'],
81                     bre: ['creator', 'editor']
82                 }
83             });
84         }));
85     }
86 }
87
88