1 import {Component, OnInit, OnDestroy, Input} from '@angular/core';
2 import {Subscription} from 'rxjs';
3 import {Router} from '@angular/router';
4 import {OrgService} from '@eg/core/org.service';
5 import {IdlObject} from '@eg/core/idl.service';
6 import {CatalogService} from '@eg/share/catalog/catalog.service';
7 import {BibRecordSummary, HoldingsSummary} from '@eg/share/catalog/bib-record.service';
8 import {CatalogSearchContext} from '@eg/share/catalog/search-context';
9 import {CatalogUrlService} from '@eg/share/catalog/catalog-url.service';
10 import {StaffCatalogService} from '../catalog.service';
11 import {BasketService} from '@eg/share/catalog/basket.service';
12 import {CourseService} from '@eg/staff/share/course.service';
15 selector: 'eg-catalog-result-record',
16 templateUrl: 'record.component.html',
17 styleUrls: ['record.component.css']
19 export class ResultRecordComponent implements OnInit, OnDestroy {
21 @Input() index: number; // 0-index display row
22 @Input() summary: BibRecordSummary;
24 // Optional call number (acn) object to highlight
25 // Assumed prefix/suffix are fleshed
26 // Used by call number browse.
27 @Input() callNumber: IdlObject;
29 searchContext: CatalogSearchContext;
30 isRecordSelected: boolean;
31 basketSub: Subscription;
36 private router: Router,
37 private org: OrgService,
38 private cat: CatalogService,
39 private catUrl: CatalogUrlService,
40 private staffCat: StaffCatalogService,
41 private basket: BasketService,
42 private course: CourseService
46 this.searchContext = this.staffCat.searchContext;
47 this.loadCourseInformation(this.summary.id);
48 this.isRecordSelected = this.basket.hasRecordId(this.summary.id);
50 // Watch for basket changes caused by other components
51 this.basketSub = this.basket.onChange.subscribe(() => {
52 this.isRecordSelected = this.basket.hasRecordId(this.summary.id);
57 this.basketSub.unsubscribe();
60 loadCourseInformation(recordId) {
61 this.course.isOptedIn().then(res => {
63 this.course.fetchCoursesForRecord(recordId).then(course_list => {
65 Object.keys(course_list).forEach(key => {
66 this.courses.push(course_list[key]);
68 this.hasCourse = true;
75 orgName(orgId: number): string {
76 return this.org.get(orgId).shortname();
79 iconFormatLabel(code: string): string {
80 return this.cat.iconFormatLabel(code);
85 let holdTarget = this.summary.id;
87 const ts = this.searchContext.termSearch;
88 if (ts.isMetarecordSearch()) {
90 holdTarget = this.summary.metabibId;
93 this.router.navigate([`/staff/catalog/hold/${holdType}`],
94 {queryParams: {target: holdTarget}});
98 alert('Adding to list for bib ' + this.summary.id);
101 // Params to genreate a new author search based on a reset
102 // clone of the current page params.
103 getAuthorSearchParams(summary: BibRecordSummary): any {
104 return this.staffCat.getAuthorSearchParams(summary);
107 // Returns the URL parameters for the current page plus the
108 // "fromMetarecord" param used for linking title links to
109 // MR constituent result records list.
110 appendFromMrParam(summary: BibRecordSummary): any {
111 const tmpContext = this.staffCat.cloneContext(this.searchContext);
112 tmpContext.termSearch.fromMetarecord = summary.metabibId;
113 return this.catUrl.toUrlParams(tmpContext);
116 // Returns true if the selected record summary is a metarecord summary
117 // and it links to more than one constituent bib record.
118 hasMrConstituentRecords(summary: BibRecordSummary): boolean {
120 summary.metabibId && summary.metabibRecords.length > 1
124 currentParams(): any {
125 return this.catUrl.toUrlParams(this.searchContext);
128 toggleBasketEntry() {
129 if (this.isRecordSelected) {
130 return this.basket.addRecordIds([this.summary.id]);
132 return this.basket.removeRecordIds([this.summary.id]);
136 getHoldingsSummaries(): HoldingsSummary[] {
137 if (!this.summary.prefOuHoldingsSummary) {
138 return this.summary.holdingsSummary;
142 this.summary.holdingsSummary.some(sum => {
143 if (Number(sum.org_unit) === Number(this.staffCat.prefOrg.id())) {
149 // Holdings summary for the pref ou is included in the
150 // record-level holdings summaries.
151 return this.summary.holdingsSummary;
154 return this.summary.holdingsSummary
155 .concat(this.summary.prefOuHoldingsSummary);