9f0f26673574f47777dc0e9bc4ac7cf84f55a008
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / catalog / result / record.component.ts
1 import {Component, OnInit, OnDestroy, Input} from '@angular/core';
2 import {Subscription} from 'rxjs';
3 import {Router, ParamMap} from '@angular/router';
4 import {OrgService} from '@eg/core/org.service';
5 import {NetService} from '@eg/core/net.service';
6 import {IdlObject} from '@eg/core/idl.service';
7 import {CatalogService} from '@eg/share/catalog/catalog.service';
8 import {BibRecordService, BibRecordSummary} from '@eg/share/catalog/bib-record.service';
9 import {CatalogSearchContext} from '@eg/share/catalog/search-context';
10 import {CatalogUrlService} from '@eg/share/catalog/catalog-url.service';
11 import {StaffCatalogService} from '../catalog.service';
12 import {BasketService} from '@eg/share/catalog/basket.service';
13 import {CourseService} from '@eg/staff/share/course.service';
14
15 @Component({
16   selector: 'eg-catalog-result-record',
17   templateUrl: 'record.component.html',
18   styleUrls: ['record.component.css']
19 })
20 export class ResultRecordComponent implements OnInit, OnDestroy {
21
22     @Input() index: number;  // 0-index display row
23     @Input() summary: BibRecordSummary;
24
25     // Optional call number (acn) object to highlight
26     // Assumed prefix/suffix are fleshed
27     // Used by call number browse.
28     @Input() callNumber: IdlObject;
29
30     searchContext: CatalogSearchContext;
31     isRecordSelected: boolean;
32     basketSub: Subscription;
33     hasCourse = false;
34     courses: any[] = [];
35
36     constructor(
37         private router: Router,
38         private org: OrgService,
39         private net: NetService,
40         private bib: BibRecordService,
41         private cat: CatalogService,
42         private catUrl: CatalogUrlService,
43         private staffCat: StaffCatalogService,
44         private basket: BasketService,
45         private course: CourseService
46     ) {}
47
48     ngOnInit() {
49         this.searchContext = this.staffCat.searchContext;
50         this.loadCourseInformation(this.summary.id);
51         this.isRecordSelected = this.basket.hasRecordId(this.summary.id);
52
53         // Watch for basket changes caused by other components
54         this.basketSub = this.basket.onChange.subscribe(() => {
55             this.isRecordSelected = this.basket.hasRecordId(this.summary.id);
56         });
57     }
58
59     ngOnDestroy() {
60         this.basketSub.unsubscribe();
61     }
62
63     loadCourseInformation(recordId) {
64         this.course.isOptedIn().then(res => {
65             if (res) {
66                 this.course.fetchCoursesForRecord(recordId).then(course_list => {
67                     Object.keys(course_list).forEach(key => {
68                         this.courses.push(course_list[key]);
69                     });
70                     this.hasCourse = true;
71                 });
72             }
73         });
74     }
75
76     orgName(orgId: number): string {
77         return this.org.get(orgId).shortname();
78     }
79
80     iconFormatLabel(code: string): string {
81         return this.cat.iconFormatLabel(code);
82     }
83
84     placeHold(): void {
85         let holdType = 'T';
86         let holdTarget = this.summary.id;
87
88         const ts = this.searchContext.termSearch;
89         if (ts.isMetarecordSearch()) {
90             holdType = 'M';
91             holdTarget = this.summary.metabibId;
92         }
93
94         this.router.navigate([`/staff/catalog/hold/${holdType}`],
95             {queryParams: {target: holdTarget}});
96     }
97
98     addToList(): void {
99         alert('Adding to list for bib ' + this.summary.id);
100     }
101
102     // Params to genreate a new author search based on a reset
103     // clone of the current page params.
104     getAuthorSearchParams(summary: BibRecordSummary): any {
105         return this.staffCat.getAuthorSearchParams(summary);
106     }
107
108     // Returns the URL parameters for the current page plus the
109     // "fromMetarecord" param used for linking title links to
110     // MR constituent result records list.
111     appendFromMrParam(summary: BibRecordSummary): any {
112         const tmpContext = this.staffCat.cloneContext(this.searchContext);
113         tmpContext.termSearch.fromMetarecord = summary.metabibId;
114         return this.catUrl.toUrlParams(tmpContext);
115     }
116
117     // Returns true if the selected record summary is a metarecord summary
118     // and it links to more than one constituent bib record.
119     hasMrConstituentRecords(summary: BibRecordSummary): boolean {
120         return (
121             summary.metabibId && summary.metabibRecords.length > 1
122         );
123     }
124
125     currentParams(): any {
126         return this.catUrl.toUrlParams(this.searchContext);
127     }
128
129     toggleBasketEntry() {
130         if (this.isRecordSelected) {
131             return this.basket.addRecordIds([this.summary.id]);
132         } else {
133             return this.basket.removeRecordIds([this.summary.id]);
134         }
135     }
136 }
137
138