]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/admin/local/course-reserves/course-list.component.ts
LP1849212: Angular Course Page improvements, OPAC course search
[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, ActivatedRoute }    from '@angular/router';
3 import {IdlObject} from '@eg/core/idl.service';
4 import {PcrudService} from '@eg/core/pcrud.service';
5 import {AuthService} from '@eg/core/auth.service';
6 import {CourseService} from '@eg/staff/share/course.service';
7 import {NetService} from '@eg/core/net.service';
8 import {OrgService} from '@eg/core/org.service';
9 import {GridComponent} from '@eg/share/grid/grid.component';
10 import {Pager} from '@eg/share/util/pager';
11 import {GridDataSource, GridColumn} from '@eg/share/grid/grid';
12 import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
13 import {StringComponent} from '@eg/share/string/string.component';
14 import {ToastService} from '@eg/share/toast/toast.service';
15 import {LocaleService} from '@eg/core/locale.service';
16
17 import {CourseAssociateMaterialComponent
18     } from './course-associate-material.component';
19
20 import {CourseAssociateUsersComponent
21     } from './course-associate-users.component';
22
23 @Component({
24     templateUrl: './course-list.component.html'
25 })
26
27 export class CourseListComponent implements OnInit { 
28  
29     @ViewChild('editDialog', { static: true }) editDialog: FmRecordEditorComponent;
30     @ViewChild('grid', { static: true }) grid: GridComponent;
31     @ViewChild('successString', { static: true }) successString: StringComponent;
32     @ViewChild('createString', { static: false }) createString: StringComponent;
33     @ViewChild('createErrString', { static: false }) createErrString: StringComponent;
34     @ViewChild('updateFailedString', { static: false }) updateFailedString: StringComponent;
35     @ViewChild('deleteFailedString', { static: true }) deleteFailedString: StringComponent;
36     @ViewChild('deleteSuccessString', { static: true }) deleteSuccessString: StringComponent;
37     @ViewChild('archiveFailedString', { static: true }) archiveFailedString: StringComponent;
38     @ViewChild('archiveSuccessString', { static: true }) archiveSuccessString: StringComponent;
39     @ViewChild('courseMaterialDialog', {static: true})
40         private courseMaterialDialog: CourseAssociateMaterialComponent;
41     @ViewChild('courseUserDialog', {static: true})
42         private courseUserDialog: CourseAssociateUsersComponent;
43
44     @Input() sort_field: string;
45     @Input() idl_class = "acmc";
46     @Input() dialog_size: 'sm' | 'lg' = 'lg';
47     @Input() table_name = "Course";
48     grid_source: GridDataSource = new GridDataSource();
49     currentMaterials: any[] = [];
50     search_value = '';
51
52     constructor(
53         private auth: AuthService,
54         private courseSvc: CourseService,
55         private locale: LocaleService,
56         private net: NetService,
57         private org: OrgService,
58         private pcrud: PcrudService,
59         private route: ActivatedRoute,
60         private router: Router,
61         private toast: ToastService
62     ){}
63
64     ngOnInit() {
65         this.getSource();
66         this.grid.onRowActivate.subscribe((course:IdlObject) => {
67             let idToEdit = course.id();
68             this.navigateToCoursePage(idToEdit);
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.idl_class] = sort[0].name + ' ' + sort[0].dir;
81             } else if (this.sort_field) {
82                 // Default sort field
83                 orderBy[this.idl_class] = this.sort_field;
84             }
85             const searchOps = {
86                 offset: pager.offset,
87                 limit: pager.limit,
88                 order_by: orderBy
89             };
90             return this.pcrud.retrieveAll(this.idl_class, searchOps, {fleshSelectors: true})
91         };
92     }
93
94     navigateToCoursePage(id_arr: IdlObject[]) {
95         if (typeof id_arr == 'number') id_arr = [id_arr];
96         let 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         let 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(course => {
144                 console.log(course);
145                 course.is_archived(true);
146             });
147             this.pcrud.update(course).subscribe(
148                 val => {
149                     console.debug('archived: ' + val);
150                     this.archiveSuccessString.current()
151                         .then(str => this.toast.success(str));
152                 }, err => {
153                     this.archiveFailedString.current()
154                         .then(str => this.toast.danger(str));
155                 }, () => {
156                     this.grid.reload();
157                 }
158             );
159         });
160     }
161
162     deleteSelected(idl_object: IdlObject[]) {
163         this.courseSvc.disassociateMaterials(idl_object).then(res => {
164             idl_object.forEach(idl_object => {
165                 idl_object.isdeleted(true)
166             });
167             this.pcrud.autoApply(idl_object).subscribe(
168                 val => {
169                     console.debug('deleted: ' + val);
170                     this.deleteSuccessString.current()
171                         .then(str => this.toast.success(str));
172                 },
173                 err => {
174                     this.deleteFailedString.current()
175                         .then(str => this.toast.danger(str));
176                 },
177                 () => this.grid.reload()
178             );
179         });
180     };
181 }
182