]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/admin/local/course-reserves/course-list.component.ts
bc439a40185b8eecf6fa84d92bb9501b5187c8ae
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / admin / local / course-reserves / course-list.component.ts
1 import {Component, Input, ViewChild, OnInit} from '@angular/core';
2 import {Router} from '@angular/router';
3 import {IdlObject} from '@eg/core/idl.service';
4 import {PcrudService} from '@eg/core/pcrud.service';
5 import {CourseService} from '@eg/staff/share/course.service';
6 import {GridComponent} from '@eg/share/grid/grid.component';
7 import {Pager} from '@eg/share/util/pager';
8 import {GridDataSource, GridColumn} from '@eg/share/grid/grid';
9 import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
10 import {StringComponent} from '@eg/share/string/string.component';
11 import {ToastService} from '@eg/share/toast/toast.service';
12 import {LocaleService} from '@eg/core/locale.service';
13
14 import {CourseAssociateMaterialComponent
15     } from './course-associate-material.component';
16
17 import {CourseAssociateUsersComponent
18     } from './course-associate-users.component';
19
20 @Component({
21     templateUrl: './course-list.component.html'
22 })
23
24 export class CourseListComponent implements OnInit {
25
26     @ViewChild('editDialog', { static: true }) editDialog: FmRecordEditorComponent;
27     @ViewChild('grid', { static: true }) grid: GridComponent;
28     @ViewChild('successString', { static: true }) successString: StringComponent;
29     @ViewChild('createString', { static: false }) createString: StringComponent;
30     @ViewChild('createErrString', { static: false }) createErrString: StringComponent;
31     @ViewChild('updateFailedString', { static: false }) updateFailedString: StringComponent;
32     @ViewChild('deleteFailedString', { static: true }) deleteFailedString: StringComponent;
33     @ViewChild('deleteSuccessString', { static: true }) deleteSuccessString: StringComponent;
34     @ViewChild('archiveFailedString', { static: true }) archiveFailedString: StringComponent;
35     @ViewChild('archiveSuccessString', { static: true }) archiveSuccessString: StringComponent;
36     @ViewChild('courseMaterialDialog', {static: true})
37         private courseMaterialDialog: CourseAssociateMaterialComponent;
38     @ViewChild('courseUserDialog', {static: true})
39         private courseUserDialog: CourseAssociateUsersComponent;
40
41     @Input() sortField: string;
42     @Input() idlClass = 'acmc';
43     @Input() dialog_size: 'sm' | 'lg' = 'lg';
44     @Input() tableName = 'Course';
45     grid_source: GridDataSource = new GridDataSource();
46     currentMaterials: any[] = [];
47     search_value = '';
48
49     constructor(
50         private courseSvc: CourseService,
51         private locale: LocaleService,
52         private pcrud: PcrudService,
53         private router: Router,
54         private toast: ToastService
55     ) {}
56
57     ngOnInit() {
58         this.getSource();
59         this.grid.onRowActivate.subscribe((course: IdlObject) => {
60             const idToEdit = course.id();
61             this.navigateToCoursePage(idToEdit);
62         });
63     }
64
65     /**
66      * Gets the data, specified by the class, that is available.
67      */
68     getSource() {
69         this.grid_source.getRows = (pager: Pager, sort: any[]) => {
70             const orderBy: any = {};
71             if (sort.length) {
72                 // Sort specified from grid
73                 orderBy[this.idlClass] = sort[0].name + ' ' + sort[0].dir;
74             } else if (this.sortField) {
75                 // Default sort field
76                 orderBy[this.idlClass] = this.sortField;
77             }
78             const searchOps = {
79                 offset: pager.offset,
80                 limit: pager.limit,
81                 order_by: orderBy
82             };
83             return this.pcrud.retrieveAll(this.idlClass, searchOps, {fleshSelectors: true});
84         };
85     }
86
87     navigateToCoursePage(id_arr: IdlObject[]) {
88         if (typeof id_arr === 'number') { id_arr = [id_arr]; }
89         const urls = [];
90         id_arr.forEach(id => {console.log(this.router.url);
91             urls.push([this.locale.currentLocaleCode() + this.router.url + '/' +  id]);
92         });
93         if (id_arr.length === 1) {
94         this.router.navigate([this.router.url + '/' + id_arr[0]]);
95         } else {
96             urls.forEach(url => {
97                 window.open(url);
98             });
99         }
100     }
101
102     createNew() {
103         this.editDialog.mode = 'create';
104         this.editDialog.recordId = null;
105         this.editDialog.record = null;
106         this.editDialog.open({size: this.dialog_size}).subscribe(
107             ok => {
108                 this.createString.current()
109                     .then(str => this.toast.success(str));
110                 this.grid.reload();
111             },
112             rejection => {
113                 if (!rejection.dismissed) {
114                     this.createErrString.current()
115                         .then(str => this.toast.danger(str));
116                 }
117             }
118         );
119     }
120
121     editSelected(fields: IdlObject[]) {
122         // Edit each IDL thing one at a time
123         const course_ids = [];
124         fields.forEach(field => {
125             if (typeof field['id'] === 'function') {
126                 course_ids.push(field.id());
127             } else {
128                 course_ids.push(field['id']);
129             }
130         });
131         this.navigateToCoursePage(course_ids);
132     }
133
134     archiveSelected(course: IdlObject[]) {
135         this.courseSvc.disassociateMaterials(course).then(res => {
136             course.forEach(courseToArchive => {
137                 courseToArchive.is_archived(true);
138             });
139             this.pcrud.update(course).subscribe(
140                 val => {
141                     console.debug('archived: ' + val);
142                     this.archiveSuccessString.current()
143                         .then(str => this.toast.success(str));
144                 }, err => {
145                     this.archiveFailedString.current()
146                         .then(str => this.toast.danger(str));
147                 }, () => {
148                     this.grid.reload();
149                 }
150             );
151         });
152     }
153
154     deleteSelected(idlObject: IdlObject[]) {
155         this.courseSvc.disassociateMaterials(idlObject).then(res => {
156             idlObject.forEach(object => {
157                 object.isdeleted(true);
158             });
159             this.pcrud.autoApply(idlObject).subscribe(
160                 val => {
161                     console.debug('deleted: ' + val);
162                     this.deleteSuccessString.current()
163                         .then(str => this.toast.success(str));
164                 },
165                 err => {
166                     this.deleteFailedString.current()
167                         .then(str => this.toast.danger(str));
168                 },
169                 () => this.grid.reload()
170             );
171         });
172     }
173 }
174