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