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