1 import {Component, Input, OnInit, ViewChild} from '@angular/core';
2 import {Router, ActivatedRoute, ParamMap} from '@angular/router';
3 import {Observable, of} from 'rxjs';
4 import {map} from 'rxjs/operators';
5 import {Pager} from '@eg/share/util/pager';
6 import {GridComponent} from '@eg/share/grid/grid.component';
7 import {GridDataSource, GridColumn, GridCellTextGenerator} from '@eg/share/grid/grid';
8 import {IdlObject} from '@eg/core/idl.service';
9 import {EventService} from '@eg/core/event.service';
10 import {NetService} from '@eg/core/net.service';
11 import {AuthService} from '@eg/core/auth.service';
12 import {PcrudService} from '@eg/core/pcrud.service';
13 import {BibRecordService, BibRecordSummary} from '@eg/share/catalog/bib-record.service';
14 import {VandelayService, VandelayImportSelection} from './vandelay.service';
17 selector: 'eg-queued-record-matches',
18 templateUrl: 'queued-record-matches.component.html'
20 export class QueuedRecordMatchesComponent implements OnInit {
22 @Input() queueType: string;
23 @Input() recordId: number;
24 @ViewChild('bibGrid', { static: false }) bibGrid: GridComponent;
25 @ViewChild('authGrid', { static: false }) authGrid: GridComponent;
27 queuedRecord: IdlObject;
28 bibDataSource: GridDataSource;
29 authDataSource: GridDataSource;
30 markOverlayTarget: (rows: any[]) => any;
31 matchRowClick: (row: any) => void;
32 matchMap: {[id: number]: IdlObject};
34 cellTextGenerator: GridCellTextGenerator;
37 private router: Router,
38 private route: ActivatedRoute,
39 private evt: EventService,
40 private net: NetService,
41 private auth: AuthService,
42 private pcrud: PcrudService,
43 private bib: BibRecordService,
44 private vandelay: VandelayService) {
46 this.bibDataSource = new GridDataSource();
47 this.authDataSource = new GridDataSource();
49 this.bibDataSource.getRows = (pager: Pager) => {
50 return this.getBibMatchRows(pager);
53 this.cellTextGenerator = {
54 selected: row => this.isOverlayTarget(row.id) + '',
55 eg_record: row => row.eg_record + ''
60 this.authDataSource.getRows = (pager: Pager) => {
64 // Mark or un-mark as row as the merge target on row click
65 this.matchRowClick = (row: any) => {
66 this.toggleMergeTarget(row.id);
70 toggleMergeTarget(matchId: number) {
72 if (this.isOverlayTarget(matchId)) {
74 // clear selection on secondary click;
75 delete this.vandelay.importSelection.overlayMap[this.recordId];
79 // Start a new one if necessary, which will be adopted
80 // and completed by the queue UI before import.
82 let selection = this.vandelay.importSelection;
84 selection = new VandelayImportSelection();
85 this.vandelay.importSelection = selection;
87 const match = this.matchMap[matchId];
88 selection.overlayMap[this.recordId] = match.eg_record();
92 isOverlayTarget(matchId: number): boolean {
93 const selection = this.vandelay.importSelection;
95 const match = this.matchMap[matchId];
96 return selection.overlayMap[this.recordId] === match.eg_record();
103 // This thing is a nesty beast -- clean it up
104 getBibMatchRows(pager: Pager): Observable<any> {
106 return new Observable(observer => {
108 this.getQueuedRecord().then(() => {
110 const matches = this.queuedRecord.matches();
113 matches.forEach(m => {
114 this.matchMap[m.id()] = m;
115 if (!recIds.includes(m.eg_record())) {
116 recIds.push(m.eg_record());
120 const bibSummaries: {[id: number]: BibRecordSummary} = {};
121 this.bib.getBibSummary(recIds).subscribe(
122 summary => bibSummaries[summary.id] = summary,
125 this.bib.fleshBibUsers(
126 Object.values(bibSummaries).map(sum => sum.record)
128 matches.forEach(match => {
131 eg_record: match.eg_record(),
132 bre_quality: match.quality(),
133 vqbr_quality: this.queuedRecord.quality(),
134 match_score: match.match_score(),
135 bib_summary: bibSummaries[match.eg_record()]
149 getQueuedRecord(): Promise<any> {
150 if (this.queuedRecord) {
151 return Promise.resolve('');
153 const idlClass = this.queueType === 'bib' ? 'vqbr' : 'vqar';
154 const flesh = {flesh: 1, flesh_fields: {}};
155 flesh.flesh_fields[idlClass] = ['matches'];
156 return this.pcrud.retrieve(idlClass, this.recordId, flesh)
157 .toPromise().then(rec => this.queuedRecord = rec);