]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/admin/local/course-reserves/course-list.component.ts
4bcca821a4b50a9f3dfbdd54144dcb7f0a4dca1d
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / admin / local / course-reserves / course-list.component.ts
1 import {Component, Input, ViewChild, OnInit, AfterViewInit} 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, AfterViewInit {
25
26     @ViewChild('editDialog', { static: true }) editDialog: FmRecordEditorComponent;
27     @ViewChild('grid') grid: GridComponent;
28     @ViewChild('successString', { static: true }) successString: StringComponent;
29     @ViewChild('createString') createString: StringComponent;
30     @ViewChild('createErrString') createErrString: StringComponent;
31     @ViewChild('updateFailedString') 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
50     constructor(
51         private courseSvc: CourseService,
52         private locale: LocaleService,
53         private pcrud: PcrudService,
54         private router: Router,
55         private toast: ToastService
56     ) {}
57
58     ngOnInit() {
59         this.getSource();
60     }
61
62     ngAfterViewInit() {
63         this.grid.onRowActivate.subscribe((course: IdlObject) => {
64             const idToEdit = course.id();
65             this.navigateToCoursePage(idToEdit);
66         });
67
68     }
69
70     acmtcmQueryParams (row: any): {gridFilters: string} {
71         return {gridFilters: '{"course":' + row.id() + '}'};
72     }
73
74
75     /**
76      * Gets the data, specified by the class, that is available.
77      */
78     getSource() {
79         this.grid_source.getRows = (pager: Pager, sort: any[]) => {
80             const orderBy: any = {};
81             if (sort.length) {
82                 // Sort specified from grid
83                 orderBy[this.idlClass] = sort[0].name + ' ' + sort[0].dir;
84             } else if (this.sortField) {
85                 // Default sort field
86                 orderBy[this.idlClass] = this.sortField;
87             }
88             const searchOps = {
89                 offset: pager.offset,
90                 limit: pager.limit,
91                 order_by: orderBy
92             };
93             return this.pcrud.retrieveAll(this.idlClass, searchOps, {fleshSelectors: true});
94         };
95     }
96
97     navigateToCoursePage(id_arr: IdlObject[]) {
98         if (typeof id_arr === 'number') { id_arr = [id_arr]; }
99         const urls = [];
100         id_arr.forEach(id => {console.log(this.router.url);
101             urls.push([this.locale.currentLocaleCode() + this.router.url + '/' +  id]);
102         });
103         if (id_arr.length === 1) {
104         this.router.navigate([this.router.url + '/' + id_arr[0]]);
105         } else {
106             urls.forEach(url => {
107                 window.open(url);
108             });
109         }
110     }
111
112     createNew() {
113         this.editDialog.mode = 'create';
114         this.editDialog.recordId = null;
115         this.editDialog.record = null;
116         this.editDialog.open({size: this.dialog_size}).subscribe(
117             ok => {
118                 this.createString.current()
119                     .then(str => this.toast.success(str));
120                 this.grid.reload();
121             },
122             rejection => {
123                 if (!rejection.dismissed) {
124                     this.createErrString.current()
125                         .then(str => this.toast.danger(str));
126                 }
127             }
128         );
129     }
130
131     editSelected(fields: IdlObject[]) {
132         // Edit each IDL thing one at a time
133         const course_ids = [];
134         fields.forEach(field => {
135             if (typeof field['id'] === 'function') {
136                 course_ids.push(field.id());
137             } else {
138                 course_ids.push(field['id']);
139             }
140         });
141         this.navigateToCoursePage(course_ids);
142     }
143
144     archiveSelected(course: IdlObject[]) {
145         this.courseSvc.disassociateMaterials(course).then(res => {
146             course.forEach(courseToArchive => {
147                 courseToArchive.is_archived(true);
148             });
149             this.pcrud.update(course).subscribe(
150                 val => {
151                     console.debug('archived: ' + val);
152                     this.archiveSuccessString.current()
153                         .then(str => this.toast.success(str));
154                 }, err => {
155                     this.archiveFailedString.current()
156                         .then(str => this.toast.danger(str));
157                 }, () => {
158                     this.grid.reload();
159                 }
160             );
161         });
162     }
163
164     deleteSelected(idlObject: IdlObject[]) {
165         this.courseSvc.disassociateMaterials(idlObject).then(res => {
166             idlObject.forEach(object => {
167                 object.isdeleted(true);
168             });
169             this.pcrud.autoApply(idlObject).subscribe(
170                 val => {
171                     console.debug('deleted: ' + val);
172                     this.deleteSuccessString.current()
173                         .then(str => this.toast.success(str));
174                 },
175                 err => {
176                     this.deleteFailedString.current()
177                         .then(str => this.toast.danger(str));
178                 },
179                 () => this.grid.reload()
180             );
181         });
182     }
183 }
184