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) {
91 this.pcrud.search('acp', {barcode: barcode},
92 {flesh: 3, flesh_fields: {acp: ['call_number']}}).subscribe(item => {
93 let material = this.idl.create('acmcm');
94 material.item(item.id());
95 material.course(this.currentCourse.id());
96 if (relationship) material.relationship(relationship);
97 if (this.isModifyingStatus && this.tempStatus) {
98 material.original_status(item.status());
99 item.status(this.tempStatus);
101 if (this.isModifyingLocation && this.tempLocation) {
102 material.original_location(item.location());
103 item.location(this.tempLocation);
105 if (this.isModifyingCircMod) {
106 material.original_circ_modifier(item.circ_modifier());
107 item.circ_modifier(this.tempCircMod);
108 if (!this.tempCircMod) item.circ_modifier(null);
110 if (this.isModifyingCallNumber) {
111 material.original_callnumber(item.call_number());
113 this.pcrud.create(material).subscribe(
115 console.debug('created: ' + val);
116 let new_cn = item.call_number().label();
117 if (this.tempCallNumber) new_cn = this.tempCallNumber;
118 this.courseSvc.updateItem(item, this.currentCourse.owning_lib(), new_cn, this.isModifyingCallNumber).then(res => {
119 this.fetchItem(item.id(), relationship);
120 if (item.circ_lib() != this.currentCourse.owning_lib()) {
121 this.differentLibraryString.current().then(str => this.toast.warning(str));
123 this.successString.current().then(str => this.toast.success(str));
127 // Cleaning up inputs
128 this.barcodeInput = "";
129 this.relationshipInput = "";
130 this.tempStatus = null;
131 this.tempCircMod = null;
132 this.tempCallNumber = null;
133 this.tempLocation = null;
134 this.isModifyingCallNumber = false;
135 this.isModifyingCircMod = false;
136 this.isModifyingLocation = false;
137 this.isModifyingStatus = false;
139 this.failedString.current().then(str => this.toast.danger(str));
145 fetchMaterials(pager: Pager): Observable<any> {
146 return new Observable<any>(observer => {
147 this.materials.forEach(material => {
148 this.fetchItem(material.item, material.relationship);
154 fetchItem(itemId, relationship): Promise<any> {
155 return new Promise((resolve, reject) => {
158 'open-ils.circ.copy_details.retrieve',
159 this.auth.token(), itemId
163 item.call_number(res.volume);
164 item.circ_lib(this.org.get(item.circ_lib()));
165 item._title = res.mvr.title();
166 item._relationship = relationship;
167 this.gridDataSource.data.push(item);
171 }, () => resolve(this.gridDataSource.data));