]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/cat/authority/manage.component.ts
LP#1879335: (follow-up) tweak sorting of bib list
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / cat / authority / manage.component.ts
1 import {Component, OnInit, ViewChild} from '@angular/core';
2 import {Router, ActivatedRoute, ParamMap} from '@angular/router';
3 import {Observable, empty} from 'rxjs';
4 import {map, switchMap} from 'rxjs/operators';
5 import {NgbTabset, NgbTabChangeEvent} from '@ng-bootstrap/ng-bootstrap';
6 import {IdlObject} from '@eg/core/idl.service';
7 import {Pager} from '@eg/share/util/pager';
8 import {NetService} from '@eg/core/net.service';
9 import {PcrudService} from '@eg/core/pcrud.service';
10 import {OrgService} from '@eg/core/org.service';
11 import {ComboboxEntry} from '@eg/share/combobox/combobox.component';
12
13 /* Find, merge, and edit authority records */
14
15 @Component({
16   templateUrl: 'manage.component.html'
17 })
18 export class ManageAuthorityComponent implements OnInit {
19
20     authId: number;
21     authTab = 'bibs';
22     authMeta: any;
23     linkedBibIdSource: (pager: Pager, sort: any) => Promise<number[]>;
24
25     constructor(
26         private router: Router,
27         private route: ActivatedRoute,
28         private net: NetService,
29         private org: OrgService,
30         private pcrud: PcrudService
31     ) {
32     }
33
34     ngOnInit() {
35         this.route.paramMap.subscribe((params: ParamMap) => {
36             this.authTab = params.get('tab') || 'bibs';
37             const id = +params.get('id');
38
39             if (id !== this.authId) {
40                 this.authId = id;
41
42                 this.net.request(
43                     'open-ils.search',
44                     'open-ils.search.authority.main_entry', this.authId
45                 ).subscribe(meta => this.authMeta = meta);
46             }
47         });
48
49         this.linkedBibIdSource = (pager: Pager, sort: any) => {
50             return this.getLinkedBibIds(pager, sort);
51         };
52     }
53
54     getLinkedBibIds(pager: Pager, sort: any): Promise<number[]> {
55         const orderBy: any = {};
56         if (sort.length && sort[0].name === 'id') {
57             orderBy.abl = 'bib ' + sort[0].dir;
58         }
59         return this.pcrud.search('abl',
60             {authority: this.authId},
61             {limit: pager.limit, offset: pager.offset, order_by: orderBy},
62             {atomic: true}
63         ).pipe(map(links => links.map(l => l.bib()))
64         ).toPromise();
65     }
66
67     // Changing a tab in the UI means changing the route.
68     // Changing the route ultimately results in changing the tab.
69     beforeTabChange(evt: NgbTabChangeEvent) {
70
71         // prevent tab changing until after route navigation
72         evt.preventDefault();
73
74         this.authTab = evt.nextId;
75         this.routeToTab();
76     }
77
78     routeToTab() {
79         const url =
80             `/staff/cat/authority/manage/${this.authId}/${this.authTab}`;
81         this.router.navigate([url]);
82     }
83 }
84
85