1 import {Component, Input, ViewChild, OnInit, TemplateRef} from '@angular/core';
2 import {Observable, Observer, of} from 'rxjs';
3 import {DialogComponent} from '@eg/share/dialog/dialog.component';
4 import {AuthService} from '@eg/core/auth.service';
5 import {NetService} from '@eg/core/net.service';
6 import {EventService} from '@eg/core/event.service';
7 import {OrgService} from '@eg/core/org.service';
8 import {PcrudService} from '@eg/core/pcrud.service';
9 import {Pager} from '@eg/share/util/pager';
10 import {NgbModal, NgbModalOptions} from '@ng-bootstrap/ng-bootstrap';
11 import {GridDataSource} from '@eg/share/grid/grid';
12 import {GridComponent} from '@eg/share/grid/grid.component';
13 import {IdlObject, IdlService} from '@eg/core/idl.service';
14 import {StringComponent} from '@eg/share/string/string.component';
15 import {ToastService} from '@eg/share/toast/toast.service';
16 import {CourseService} from '@eg/staff/share/course.service';
19 selector: 'eg-course-associate-material-dialog',
20 templateUrl: './course-associate-material.component.html'
23 export class CourseAssociateMaterialComponent extends DialogComponent {
25 @ViewChild('materialsGrid', {static: true}) materialsGrid: GridComponent;
26 @ViewChild('deleteFailedString', { static: true }) deleteFailedString: StringComponent;
27 @ViewChild('deleteSuccessString', { static: true }) deleteSuccessString: StringComponent;
28 @ViewChild('successString', { static: true }) successString: StringComponent;
29 @ViewChild('failedString', { static: true }) failedString: StringComponent;
30 @ViewChild('differentLibraryString', { static: true }) differentLibraryString: StringComponent;
31 @Input() table_name = "Course Materials";
32 @Input() barcodeInput: String;
33 @Input() relationshipInput: String;
34 @Input() tempCallNumber: String;
35 @Input() tempStatus: Number;
36 @Input() tempLocation: Number;
37 @Input() tempCircMod: String;
38 @Input() isModifyingStatus: Boolean;
39 @Input() isModifyingCircMod: Boolean;
40 @Input() isModifyingCallNumber: Boolean;
41 @Input() isModifyingLocation: Boolean;
42 currentCourse: IdlObject;
44 gridDataSource: GridDataSource;
47 private auth: AuthService,
48 private idl: IdlService,
49 private net: NetService,
50 private pcrud: PcrudService,
51 private org: OrgService,
52 private evt: EventService,
53 private modal: NgbModal,
54 private toast: ToastService,
55 private courseSvc: CourseService
58 this.gridDataSource = new GridDataSource();
62 this.gridDataSource.getRows = (pager: Pager, sort: any[]) => {
63 return this.fetchMaterials(pager);
67 deleteSelected(items) {
69 items.forEach(item => {
70 this.gridDataSource.data.splice(this.gridDataSource.data.indexOf(item, 0), 1);
71 item_ids.push(item.id())
73 this.pcrud.search('acmcm', {course: this.currentCourse.id(), item: item_ids}).subscribe(material => {
74 material.isdeleted(true);
75 this.pcrud.autoApply(material).subscribe(
77 this.courseSvc.resetItemFields(material, this.currentCourse.owning_lib());
78 console.debug('deleted: ' + val);
79 this.deleteSuccessString.current().then(str => this.toast.success(str));
82 this.deleteFailedString.current()
83 .then(str => this.toast.danger(str));
89 associateItem(barcode, relationship) {
93 relationship: relationship,
94 isModifyingCallNumber: this.isModifyingCallNumber,
95 isModifyingCircMod: this.isModifyingCircMod,
96 isModifyingLocation: this.isModifyingLocation,
97 isModifyingStatus: this.isModifyingStatus,
98 tempCircMod: this.tempCircMod,
99 tempLocation: this.tempLocation,
100 tempStatus: this.tempStatus,
101 currentCourse: this.currentCourse
103 this.barcodeInput = null;
105 this.pcrud.search('acp', {barcode: args.barcode}, {
106 flesh: 3, flesh_fields: {acp: ['call_number']}
107 }).subscribe(item => {
108 let associatedMaterial = this.courseSvc.associateMaterials(item, args);
109 associatedMaterial.material.then(res => {
110 item = associatedMaterial.item;
111 let new_cn = item.call_number().label();
112 if (this.tempCallNumber) new_cn = this.tempCallNumber;
113 this.courseSvc.updateItem(item,
114 this.currentCourse.owning_lib(),
115 new_cn, args.isModifyingCallNumber).then(resp => {
116 this.fetchItem(item.id(), args.relationship);
117 if (item.circ_lib() != this.currentCourse.owning_lib()) {
118 this.differentLibraryString.current().then(str => this.toast.warning(str));
120 this.successString.current().then(str => this.toast.success(str));
124 this.failedString.current().then(str => this.toast.danger(str));
130 fetchMaterials(pager: Pager): Observable<any> {
131 return new Observable<any>(observer => {
132 this.materials.forEach(material => {
133 this.fetchItem(material.item, material.relationship);
139 fetchItem(itemId, relationship): Promise<any> {
140 return new Promise((resolve, reject) => {
143 'open-ils.circ.copy_details.retrieve',
144 this.auth.token(), itemId
148 item.call_number(res.volume);
149 item.circ_lib(this.org.get(item.circ_lib()));
150 item._title = res.mvr.title();
151 item._relationship = relationship;
152 this.gridDataSource.data.push(item);
156 }, () => resolve(this.gridDataSource.data));