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 '@eg/staff/share/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('archiveFailedString', { static: true }) archiveFailedString: StringComponent;
33 @ViewChild('archiveSuccessString', { static: true }) archiveSuccessString: StringComponent;
34 @ViewChild('courseMaterialDialog', {static: true})
35 private courseMaterialDialog: CourseAssociateMaterialComponent;
36 @Input() sort_field: string;
37 @Input() idl_class = "acmc";
38 @Input() dialog_size: 'sm' | 'lg' = 'lg';
39 @Input() table_name = "Course";
40 grid_source: GridDataSource = new GridDataSource();
41 currentMaterials: any[] = [];
45 private auth: AuthService,
46 private courseSvc: CourseService,
47 private net: NetService,
48 private org: OrgService,
49 private pcrud: PcrudService,
50 private toast: ToastService,
58 * Gets the data, specified by the class, that is available.
61 this.grid_source.getRows = (pager: Pager, sort: any[]) => {
62 const orderBy: any = {};
64 // Sort specified from grid
65 orderBy[this.idl_class] = sort[0].name + ' ' + sort[0].dir;
66 } else if (this.sort_field) {
68 orderBy[this.idl_class] = this.sort_field;
75 return this.pcrud.retrieveAll(this.idl_class, searchOps, {fleshSelectors: true});
79 showEditDialog(standingPenalty: IdlObject): Promise<any> {
80 this.editDialog.mode = 'update';
81 this.editDialog.recordId = standingPenalty['id']();
82 return new Promise((resolve, reject) => {
83 this.editDialog.open({size: this.dialog_size}).subscribe(
85 this.successString.current()
86 .then(str => this.toast.success(str));
91 this.updateFailedString.current()
92 .then(str => this.toast.danger(str));
100 this.editDialog.mode = 'create';
101 this.editDialog.recordId = null;
102 this.editDialog.record = null;
103 this.editDialog.open({size: this.dialog_size}).subscribe(
105 this.createString.current()
106 .then(str => this.toast.success(str));
110 if (!rejection.dismissed) {
111 this.createErrString.current()
112 .then(str => this.toast.danger(str));
118 editSelected(fields: IdlObject[]) {
119 // Edit each IDL thing one at a time
120 const editOneThing = (field_object: IdlObject) => {
121 if (!field_object) { return; }
122 this.showEditDialog(field_object).then(
123 () => editOneThing(fields.shift()));
125 editOneThing(fields.shift());
128 archiveSelected(course: IdlObject[]) {
129 this.courseSvc.disassociateMaterials(course).then(res => {
130 course.forEach(course => {
132 course.is_archived(true);
134 this.pcrud.update(course).subscribe(
136 console.debug('archived: ' + val);
137 this.archiveSuccessString.current()
138 .then(str => this.toast.success(str));
140 this.archiveFailedString.current()
141 .then(str => this.toast.danger(str));
149 deleteSelected(idl_object: IdlObject[]) {
150 this.courseSvc.disassociateMaterials(idl_object).then(res => {
151 idl_object.forEach(idl_object => {
152 idl_object.isdeleted(true)
154 this.pcrud.autoApply(idl_object).subscribe(
156 console.debug('deleted: ' + val);
157 this.deleteSuccessString.current()
158 .then(str => this.toast.success(str));
161 this.deleteFailedString.current()
162 .then(str => this.toast.danger(str));
164 () => this.grid.reload()
169 fetchCourseMaterials(course, currentMaterials): Promise<any> {
170 return new Promise((resolve, reject) => {
171 this.pcrud.search('acmcm', {course: course}).subscribe(res => {
172 if (res) this.fleshItemDetails(res.item(), res.relationship());
175 }, () => resolve(this.courseMaterialDialog.gridDataSource.data));
179 fleshItemDetails(itemId, relationship): Promise<any> {
180 return new Promise((resolve, reject) => {
183 'open-ils.circ.copy_details.retrieve',
184 this.auth.token(), itemId
188 item.call_number(res.volume);
189 item._title = res.mvr.title();
190 item.circ_lib(this.org.get(item.circ_lib()));
191 item._relationship = relationship;
192 this.courseMaterialDialog.gridDataSource.data.push(item);
196 }, () => resolve(this.courseMaterialDialog.gridDataSource.data));
200 openMaterialsDialog(course) {
201 let currentMaterials = []
202 this.courseMaterialDialog.gridDataSource.data = [];
203 this.fetchCourseMaterials(course[0].id(), currentMaterials).then(res => {
204 this.courseMaterialDialog.currentCourse = course[0];
205 this.courseMaterialDialog.materials = currentMaterials;
206 this.courseMaterialDialog.open({size: 'lg'}).subscribe(res => {