1 import {Component, Input, ViewChild, OnInit} from '@angular/core';
2 import {IdlObject} from '@eg/core/idl.service';
3 import {PcrudService} from '@eg/core/pcrud.service';
4 import {AuthService} from '@eg/core/auth.service';
5 import {CourseService} from './course.service';
6 import {NetService} from '@eg/core/net.service';
7 import {OrgService} from '@eg/core/org.service';
8 import {GridComponent} from '@eg/share/grid/grid.component';
9 import {Pager} from '@eg/share/util/pager';
10 import {GridDataSource, GridColumn} from '@eg/share/grid/grid';
11 import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
12 import {StringComponent} from '@eg/share/string/string.component';
13 import {ToastService} from '@eg/share/toast/toast.service';
15 import {CourseAssociateMaterialComponent
16 } from './course-associate-material.component';
19 templateUrl: './course-list.component.html'
22 export class CourseListComponent implements OnInit {
24 @ViewChild('editDialog', { static: true }) editDialog: FmRecordEditorComponent;
25 @ViewChild('grid', { static: true }) grid: GridComponent;
26 @ViewChild('successString', { static: true }) successString: StringComponent;
27 @ViewChild('createString', { static: false }) createString: StringComponent;
28 @ViewChild('createErrString', { static: false }) createErrString: StringComponent;
29 @ViewChild('updateFailedString', { static: false }) updateFailedString: StringComponent;
30 @ViewChild('deleteFailedString', { static: true }) deleteFailedString: StringComponent;
31 @ViewChild('deleteSuccessString', { static: true }) deleteSuccessString: StringComponent;
32 @ViewChild('courseMaterialDialog', {static: true})
33 private courseMaterialDialog: CourseAssociateMaterialComponent;
34 @Input() sort_field: string;
35 @Input() idl_class = "acmc";
36 @Input() dialog_size: 'sm' | 'lg' = 'lg';
37 @Input() table_name = "Course";
38 grid_source: GridDataSource = new GridDataSource();
39 currentMaterials: any[] = [];
43 private auth: AuthService,
44 private courseSvc: CourseService,
45 private net: NetService,
46 private org: OrgService,
47 private pcrud: PcrudService,
48 private toast: ToastService,
56 * Gets the data, specified by the class, that is available.
59 this.grid_source.getRows = (pager: Pager, sort: any[]) => {
60 const orderBy: any = {};
62 // Sort specified from grid
63 orderBy[this.idl_class] = sort[0].name + ' ' + sort[0].dir;
64 } else if (this.sort_field) {
66 orderBy[this.idl_class] = this.sort_field;
73 return this.pcrud.retrieveAll(this.idl_class, searchOps, {fleshSelectors: true});
77 showEditDialog(standingPenalty: IdlObject): Promise<any> {
78 this.editDialog.mode = 'update';
79 this.editDialog.recordId = standingPenalty['id']();
80 return new Promise((resolve, reject) => {
81 this.editDialog.open({size: this.dialog_size}).subscribe(
83 this.successString.current()
84 .then(str => this.toast.success(str));
89 this.updateFailedString.current()
90 .then(str => this.toast.danger(str));
98 this.editDialog.mode = 'create';
99 this.editDialog.recordId = null;
100 this.editDialog.record = null;
101 this.editDialog.open({size: this.dialog_size}).subscribe(
103 this.createString.current()
104 .then(str => this.toast.success(str));
108 if (!rejection.dismissed) {
109 this.createErrString.current()
110 .then(str => this.toast.danger(str));
116 editSelected(fields: IdlObject[]) {
117 // Edit each IDL thing one at a time
118 const editOneThing = (field_object: IdlObject) => {
119 if (!field_object) { return; }
120 this.showEditDialog(field_object).then(
121 () => editOneThing(fields.shift()));
123 editOneThing(fields.shift());
126 deleteSelected(idl_object: IdlObject[]) {
127 this.courseSvc.disassociateMaterials(idl_object).then(res => {
129 idl_object.forEach(idl_object => {
130 idl_object.isdeleted(true)
132 this.pcrud.autoApply(idl_object).subscribe(
134 console.debug('deleted: ' + val);
135 this.deleteSuccessString.current()
136 .then(str => this.toast.success(str));
139 this.deleteFailedString.current()
140 .then(str => this.toast.danger(str));
142 () => this.grid.reload()
147 fetchCourseMaterials(course, currentMaterials): Promise<any> {
148 return new Promise((resolve, reject) => {
149 this.pcrud.search('acmcm', {course: course}).subscribe(res => {
150 if (res) this.fleshItemDetails(res.item(), res.relationship());
153 }, () => resolve(this.courseMaterialDialog.gridDataSource.data));
157 fleshItemDetails(itemId, relationship): Promise<any> {
158 return new Promise((resolve, reject) => {
161 'open-ils.circ.copy_details.retrieve',
162 this.auth.token(), itemId
166 item.call_number(res.volume);
167 item._title = res.mvr.title();
168 item.circ_lib(this.org.get(item.circ_lib()));
169 item._relationship = relationship;
170 this.courseMaterialDialog.gridDataSource.data.push(item);
174 }, () => resolve(this.courseMaterialDialog.gridDataSource.data));
178 openMaterialsDialog(course) {
179 let currentMaterials = []
180 this.courseMaterialDialog.gridDataSource.data = [];
181 this.fetchCourseMaterials(course[0].id(), currentMaterials).then(res => {
182 this.courseMaterialDialog.currentCourse = course[0];
183 this.courseMaterialDialog.materials = currentMaterials;
184 this.courseMaterialDialog.open({size: 'lg'}).subscribe(res => {