1 import {Component, Input, ViewChild} from '@angular/core';
2 import {Router, ActivatedRoute} from '@angular/router';
3 import {Observable} from 'rxjs';
4 import {Pager} from '@eg/share/util/pager';
5 import {GridComponent} from '@eg/share/grid/grid.component';
6 import {GridDataSource, GridCellTextGenerator} from '@eg/share/grid/grid';
7 import {IdlObject} from '@eg/core/idl.service';
8 import {EventService} from '@eg/core/event.service';
9 import {NetService} from '@eg/core/net.service';
10 import {AuthService} from '@eg/core/auth.service';
11 import {PcrudService} from '@eg/core/pcrud.service';
12 import {BibRecordService, BibRecordSummary} from '@eg/share/catalog/bib-record.service';
13 import {VandelayService, VandelayImportSelection} from './vandelay.service';
16 selector: 'eg-queued-record-matches',
17 templateUrl: 'queued-record-matches.component.html'
19 export class QueuedRecordMatchesComponent {
21 @Input() queueType: string;
22 @Input() recordId: number;
23 @ViewChild('bibGrid', { static: false }) bibGrid: GridComponent;
24 @ViewChild('authGrid', { static: false }) authGrid: GridComponent;
26 queuedRecord: IdlObject;
27 bibDataSource: GridDataSource;
28 authDataSource: GridDataSource;
29 markOverlayTarget: (rows: any[]) => any;
30 matchRowClick: (row: any) => void;
31 matchMap: {[id: number]: IdlObject};
33 cellTextGenerator: GridCellTextGenerator;
36 private router: Router,
37 private route: ActivatedRoute,
38 private evt: EventService,
39 private net: NetService,
40 private auth: AuthService,
41 private pcrud: PcrudService,
42 private bib: BibRecordService,
43 private vandelay: VandelayService) {
45 this.bibDataSource = new GridDataSource();
46 this.authDataSource = new GridDataSource();
48 this.bibDataSource.getRows = (pager: Pager) => {
49 return this.getBibMatchRows(pager);
52 this.cellTextGenerator = {
53 selected: row => this.isOverlayTarget(row.id) + '',
54 eg_record: row => row.eg_record + ''
59 this.authDataSource.getRows = (pager: Pager) => {
63 // Mark or un-mark as row as the merge target on row click
64 this.matchRowClick = (row: any) => {
65 this.toggleMergeTarget(row.id);
69 toggleMergeTarget(matchId: number) {
71 if (this.isOverlayTarget(matchId)) {
73 // clear selection on secondary click;
74 delete this.vandelay.importSelection.overlayMap[this.recordId];
78 // Start a new one if necessary, which will be adopted
79 // and completed by the queue UI before import.
81 let selection = this.vandelay.importSelection;
83 selection = new VandelayImportSelection();
84 this.vandelay.importSelection = selection;
86 const match = this.matchMap[matchId];
87 selection.overlayMap[this.recordId] = match.eg_record();
91 isOverlayTarget(matchId: number): boolean {
92 const selection = this.vandelay.importSelection;
94 const match = this.matchMap[matchId];
95 return selection.overlayMap[this.recordId] === match.eg_record();
100 // This thing is a nesty beast -- clean it up
101 getBibMatchRows(pager: Pager): Observable<any> {
103 return new Observable(observer => {
105 this.getQueuedRecord().then(() => {
107 const matches = this.queuedRecord.matches();
110 matches.forEach(m => {
111 this.matchMap[m.id()] = m;
112 if (!recIds.includes(m.eg_record())) {
113 recIds.push(m.eg_record());
117 const bibSummaries: {[id: number]: BibRecordSummary} = {};
118 this.bib.getBibSummaries(recIds).subscribe(
119 summary => bibSummaries[summary.id] = summary,
120 (err: unknown) => {},
122 matches.forEach(match => {
125 eg_record: match.eg_record(),
126 bre_quality: match.quality(),
127 vqbr_quality: this.queuedRecord.quality(),
128 match_score: match.match_score(),
129 bib_summary: bibSummaries[match.eg_record()]
142 getQueuedRecord(): Promise<any> {
143 if (this.queuedRecord) {
144 return Promise.resolve('');
146 const idlClass = this.queueType === 'bib' ? 'vqbr' : 'vqar';
147 const flesh = {flesh: 1, flesh_fields: {}};
148 flesh.flesh_fields[idlClass] = ['matches'];
149 return this.pcrud.retrieve(idlClass, this.recordId, flesh)
150 .toPromise().then(rec => this.queuedRecord = rec);