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';
14 import {CourseAssociateMaterialComponent
15 } from './course-associate-material.component';
17 import {CourseAssociateUsersComponent
18 } from './course-associate-users.component';
21 templateUrl: './course-list.component.html'
24 export class CourseListComponent implements OnInit {
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;
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[] = [];
50 private courseSvc: CourseService,
51 private locale: LocaleService,
52 private pcrud: PcrudService,
53 private router: Router,
54 private toast: ToastService
59 this.grid.onRowActivate.subscribe((course: IdlObject) => {
60 const idToEdit = course.id();
61 this.navigateToCoursePage(idToEdit);
66 * Gets the data, specified by the class, that is available.
69 this.grid_source.getRows = (pager: Pager, sort: any[]) => {
70 const orderBy: any = {};
72 // Sort specified from grid
73 orderBy[this.idlClass] = sort[0].name + ' ' + sort[0].dir;
74 } else if (this.sortField) {
76 orderBy[this.idlClass] = this.sortField;
83 return this.pcrud.retrieveAll(this.idlClass, searchOps, {fleshSelectors: true});
87 navigateToCoursePage(id_arr: IdlObject[]) {
88 if (typeof id_arr === 'number') { id_arr = [id_arr]; }
90 id_arr.forEach(id => {console.log(this.router.url);
91 urls.push([this.locale.currentLocaleCode() + this.router.url + '/' + id]);
93 if (id_arr.length === 1) {
94 this.router.navigate([this.router.url + '/' + id_arr[0]]);
103 this.editDialog.mode = 'create';
104 this.editDialog.recordId = null;
105 this.editDialog.record = null;
106 this.editDialog.open({size: this.dialog_size}).subscribe(
108 this.createString.current()
109 .then(str => this.toast.success(str));
113 if (!rejection.dismissed) {
114 this.createErrString.current()
115 .then(str => this.toast.danger(str));
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());
128 course_ids.push(field['id']);
131 this.navigateToCoursePage(course_ids);
134 archiveSelected(course: IdlObject[]) {
135 this.courseSvc.disassociateMaterials(course).then(res => {
136 course.forEach(courseToArchive => {
137 courseToArchive.is_archived(true);
139 this.pcrud.update(course).subscribe(
141 console.debug('archived: ' + val);
142 this.archiveSuccessString.current()
143 .then(str => this.toast.success(str));
145 this.archiveFailedString.current()
146 .then(str => this.toast.danger(str));
154 deleteSelected(idlObject: IdlObject[]) {
155 this.courseSvc.disassociateMaterials(idlObject).then(res => {
156 idlObject.forEach(object => {
157 object.isdeleted(true);
159 this.pcrud.autoApply(idlObject).subscribe(
161 console.debug('deleted: ' + val);
162 this.deleteSuccessString.current()
163 .then(str => this.toast.success(str));
166 this.deleteFailedString.current()
167 .then(str => this.toast.danger(str));
169 () => this.grid.reload()