1 import {Component, Input, OnInit, ViewChild} from '@angular/core';
2 import {Observable} from 'rxjs';
3 import {map, switchMap} from 'rxjs/operators';
4 import {PcrudService} from '@eg/core/pcrud.service';
5 import {Pager} from '@eg/share/util/pager';
6 import {GridDataSource} from '@eg/share/grid/grid';
7 import {GridComponent} from '@eg/share/grid/grid.component';
8 import {ComboboxEntry} from '@eg/share/combobox/combobox.component';
9 import {IdlObject, IdlService} from '@eg/core/idl.service';
10 import {OrgService} from '@eg/core/org.service';
11 import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
14 selector: 'eg-course-term-map-grid',
15 templateUrl: './course-term-map-grid.component.html',
17 export class CourseTermMapGridComponent implements OnInit {
18 @Input() courseId: number;
19 @ViewChild('grid') private grid: GridComponent;
20 @ViewChild('editDialog') private editDialog: FmRecordEditorComponent;
22 readonlyFields: string;
24 defaultNewAcmtcm: IdlObject;
25 gridDataSource: GridDataSource;
26 createNew: () => void;
27 deleteSelected: (rows: IdlObject[]) => void;
28 termEntryGenerator: (course: number) => (query: string) => Observable<ComboboxEntry>;
29 termEntries: (query: string) => Observable<ComboboxEntry>;
32 private idl: IdlService,
33 private org: OrgService,
34 private pcrud: PcrudService,
36 this.gridDataSource = new GridDataSource();
37 this.defaultNewAcmtcm = this.idl.create('acmtcm');
43 this.defaultNewAcmtcm.course(this.courseId);
44 this.readonlyFields = 'course';
47 this.gridDataSource.getRows = (pager: Pager, sort: any[]) => {
48 const orderBy: any = {};
56 const criteria = this.courseId ? {course: this.courseId} : {};
58 return this.pcrud.search('acmtcm',
59 criteria, searchOps, {fleshSelectors: true});
62 // Produce a bespoke callback for the combobox search, which
63 // limits the results to course terms that make sense for the
64 // selected course. This prevents users from associating a
65 // course at their library to a term from a completely different
67 this.termEntryGenerator = (courseId: number) => {
68 return (query: string) => {
69 return this.pcrud.retrieve('acmc', courseId).pipe(switchMap(fullCourseObject => {
70 return this.pcrud.search(
72 name: {'ilike': `%${query}`},
73 owning_lib: this.org.ancestors(fullCourseObject.owning_lib(), true)
75 {order_by: {'acmt': 'name'}}
77 }), map(courseTerm => {
78 return {id: courseTerm.id(), label: courseTerm.name()};
83 this.createNew = () => {
84 this.editDialog.mode = 'create';
85 this.editDialog.open({size: 'lg'})
86 .subscribe(() => this.grid.reload());
89 this.deleteSelected = (termMaps: IdlObject[]) => {
90 termMaps.forEach(termMap => termMap.isdeleted(true));
91 this.pcrud.autoApply(termMaps).subscribe(
92 val => console.debug('deleted: ' + val),
94 () => this.grid.reload()