1 import {Component, Input, OnInit, ViewChild} from '@angular/core';
2 import {Router, ActivatedRoute, ParamMap} from '@angular/router';
3 import {Observable} from 'rxjs/Observable';
4 import 'rxjs/add/observable/of';
5 import {map} from 'rxjs/operators/map';
6 import {Pager} from '@eg/share/util/pager';
7 import {GridComponent} from '@eg/share/grid/grid.component';
8 import {GridDataSource, GridColumn} from '@eg/share/grid/grid';
9 import {IdlObject} from '@eg/core/idl.service';
10 import {EventService} from '@eg/core/event.service';
11 import {NetService} from '@eg/core/net.service';
12 import {AuthService} from '@eg/core/auth.service';
13 import {PcrudService} from '@eg/core/pcrud.service';
14 import {BibRecordService, BibRecordSummary} from '@eg/share/catalog/bib-record.service';
15 import {VandelayService, VandelayImportSelection} from './vandelay.service';
18 selector: 'eg-queued-record-matches',
19 templateUrl: 'queued-record-matches.component.html'
21 export class QueuedRecordMatchesComponent implements OnInit {
23 @Input() queueType: string;
24 @Input() recordId: number;
25 @ViewChild('bibGrid') bibGrid: GridComponent;
26 @ViewChild('authGrid') authGrid: GridComponent;
28 queuedRecord: IdlObject;
29 bibDataSource: GridDataSource;
30 authDataSource: GridDataSource;
31 markOverlayTarget: (rows: any[]) => any;
32 matchRowClick: (row: any) => void;
33 matchMap: {[id: number]: IdlObject};
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);
53 this.authDataSource.getRows = (pager: Pager) => {
57 // Mark or un-mark as row as the merge target on row click
58 this.matchRowClick = (row: any) => {
59 this.toggleMergeTarget(row.id);
63 toggleMergeTarget(matchId: number) {
65 if (this.isOverlayTarget(matchId)) {
67 // clear selection on secondary click;
68 delete this.vandelay.importSelection.overlayMap[this.recordId];
72 // Start a new one if necessary, which will be adopted
73 // and completed by the queue UI before import.
75 let selection = this.vandelay.importSelection;
77 selection = new VandelayImportSelection();
78 this.vandelay.importSelection = selection;
80 const match = this.matchMap[matchId];
81 selection.overlayMap[this.recordId] = match.eg_record();
85 isOverlayTarget(matchId: number): boolean {
86 const selection = this.vandelay.importSelection;
88 const match = this.matchMap[matchId];
89 return selection.overlayMap[this.recordId] === match.eg_record();
96 // This thing is a nesty beast -- clean it up
97 getBibMatchRows(pager: Pager): Observable<any> {
99 return new Observable(observer => {
101 this.getQueuedRecord().then(() => {
103 const matches = this.queuedRecord.matches();
106 matches.forEach(m => {
107 this.matchMap[m.id()] = m;
108 if (!recIds.includes(m.eg_record())) {
109 recIds.push(m.eg_record());
113 const bibSummaries: {[id: number]: BibRecordSummary} = {};
114 this.bib.getBibSummary(recIds).subscribe(
115 summary => bibSummaries[summary.id] = summary,
118 this.bib.fleshBibUsers(
119 Object.values(bibSummaries).map(sum => sum.record)
121 matches.forEach(match => {
124 eg_record: match.eg_record(),
125 bre_quality: match.quality(),
126 vqbr_quality: this.queuedRecord.quality(),
127 match_score: match.match_score(),
128 bib_summary: bibSummaries[match.eg_record()]
142 getQueuedRecord(): Promise<any> {
143 if (this.queuedRecord) {
144 return Promise.resolve('');
146 let 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);