]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/catalog/result/record.component.ts
fec6a9ee58fcaa1c208dad88b832f0ec88f9068c
[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     has_course: boolean;
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         console.log("Entering loadCourseInformation");
65         this.course.isOptedIn().then(res => {
66             if (res) {
67                 this.course.fetchCopiesInCourseFromRecord(recordId).then(course_list => {
68                     Object.keys(course_list).forEach(key => {
69                         this.courses.push(course_list[key]);
70                     });
71                     this.has_course = true;
72                 });
73             } else {
74                 this.has_course = false;
75             }
76         });
77     }
78
79     orgName(orgId: number): string {
80         return this.org.get(orgId).shortname();
81     }
82
83     iconFormatLabel(code: string): string {
84         return this.cat.iconFormatLabel(code);
85     }
86
87     placeHold(): void {
88         let holdType = 'T';
89         let holdTarget = this.summary.id;
90
91         const ts = this.searchContext.termSearch;
92         if (ts.isMetarecordSearch()) {
93             holdType = 'M';
94             holdTarget = this.summary.metabibId;
95         }
96
97         this.router.navigate([`/staff/catalog/hold/${holdType}`],
98             {queryParams: {target: holdTarget}});
99     }
100
101     addToList(): void {
102         alert('Adding to list for bib ' + this.summary.id);
103     }
104
105     // Params to genreate a new author search based on a reset
106     // clone of the current page params.
107     getAuthorSearchParams(summary: BibRecordSummary): any {
108         return this.staffCat.getAuthorSearchParams(summary);
109     }
110
111     // Returns the URL parameters for the current page plus the
112     // "fromMetarecord" param used for linking title links to
113     // MR constituent result records list.
114     appendFromMrParam(summary: BibRecordSummary): any {
115         const tmpContext = this.staffCat.cloneContext(this.searchContext);
116         tmpContext.termSearch.fromMetarecord = summary.metabibId;
117         return this.catUrl.toUrlParams(tmpContext);
118     }
119
120     // Returns true if the selected record summary is a metarecord summary
121     // and it links to more than one constituent bib record.
122     hasMrConstituentRecords(summary: BibRecordSummary): boolean {
123         return (
124             summary.metabibId && summary.metabibRecords.length > 1
125         );
126     }
127
128     currentParams(): any {
129         return this.catUrl.toUrlParams(this.searchContext);
130     }
131
132     toggleBasketEntry() {
133         if (this.isRecordSelected) {
134             return this.basket.addRecordIds([this.summary.id]);
135         } else {
136             return this.basket.removeRecordIds([this.summary.id]);
137         }
138     }
139 }
140
141