]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/cat/authority/browse.service.ts
LP1879335 Manage Authorities Angular Port
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / cat / authority / browse.service.ts
1 import {Injectable} from '@angular/core';
2 import {Observable, empty} from 'rxjs';
3 import {map, 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 {ComboboxEntry} from '@eg/share/combobox/combobox.component';
10
11 /* Browse APIS and state maintenance */
12
13 @Injectable()
14 export class BrowseService {
15
16     // Grid paging is disabled in this UI to support browsing in
17     // both directions.  Define our own paging trackers.
18     pageSize = 15;
19     searchOffset = 0;
20
21     searchTerm: string;
22     authorityAxis: string;
23     authorityAxes: ComboboxEntry[];
24     markedForMerge: {[id: number]: boolean} = {};
25
26     constructor(
27         private net: NetService,
28         private org: OrgService,
29         private pcrud: PcrudService
30     ) {}
31
32     fetchAxes(): Promise<any> {
33         if (this.authorityAxes) {
34             return Promise.resolve(this.authorityAxes);
35         }
36
37         this.pcrud.retrieveAll('aba', {}, {atomic: true})
38         .pipe(map(axes => {
39                 this.authorityAxes = axes
40                     .map(axis => ({id: axis.code(), label: axis.name()}))
41                     .sort((a1, a2) => a1.label < a2.label ? -1 : 1);
42         })).toPromise();
43
44     }
45
46     loadAuthorities(): Observable<any> {
47
48         if (!this.searchTerm || !this.authorityAxis) {
49             return empty();
50         }
51
52         return this.net.request(
53             'open-ils.supercat',
54             'open-ils.supercat.authority.browse.by_axis',
55             this.authorityAxis, this.searchTerm,
56             this.pageSize, this.searchOffset
57
58         ).pipe(switchMap(authIds => {
59
60             return this.net.request(
61                 'open-ils.search',
62                 'open-ils.search.authority.main_entry', authIds
63             );
64
65         })).pipe(map(authMeta => {
66
67             const oOrg = this.org.get(authMeta.authority.owner());
68
69             return {
70                 authority: authMeta.authority,
71                 link_count: authMeta.linked_bibs.length,
72                 heading: authMeta.heading,
73                 thesaurus: authMeta.thesaurus,
74                 thesaurus_code: authMeta.thesaurus_code,
75                 owner: oOrg ? oOrg.shortname() : ''
76             };
77         }));
78     }
79 }
80
81