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[] = [];
51 private courseSvc: CourseService,
52 private locale: LocaleService,
53 private pcrud: PcrudService,
54 private router: Router,
55 private toast: ToastService
60 this.grid.onRowActivate.subscribe((course: IdlObject) => {
61 const idToEdit = course.id();
62 this.navigateToCoursePage(idToEdit);
67 acmtcmQueryParams (row: any): {gridFilters: string} {
68 return {gridFilters: '{"course":' + row.id() + '}'};
73 * Gets the data, specified by the class, that is available.
76 this.grid_source.getRows = (pager: Pager, sort: any[]) => {
77 const orderBy: any = {};
79 // Sort specified from grid
80 orderBy[this.idlClass] = sort[0].name + ' ' + sort[0].dir;
81 } else if (this.sortField) {
83 orderBy[this.idlClass] = this.sortField;
90 return this.pcrud.retrieveAll(this.idlClass, searchOps, {fleshSelectors: true});
94 navigateToCoursePage(id_arr: IdlObject[]) {
95 if (typeof id_arr === 'number') { id_arr = [id_arr]; }
97 id_arr.forEach(id => {console.log(this.router.url);
98 urls.push([this.locale.currentLocaleCode() + this.router.url + '/' + id]);
100 if (id_arr.length === 1) {
101 this.router.navigate([this.router.url + '/' + id_arr[0]]);
103 urls.forEach(url => {
110 this.editDialog.mode = 'create';
111 this.editDialog.recordId = null;
112 this.editDialog.record = null;
113 this.editDialog.open({size: this.dialog_size}).subscribe(
115 this.createString.current()
116 .then(str => this.toast.success(str));
120 if (!rejection.dismissed) {
121 this.createErrString.current()
122 .then(str => this.toast.danger(str));
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());
135 course_ids.push(field['id']);
138 this.navigateToCoursePage(course_ids);
141 archiveSelected(course: IdlObject[]) {
142 this.courseSvc.disassociateMaterials(course).then(res => {
143 course.forEach(courseToArchive => {
144 courseToArchive.is_archived(true);
146 this.pcrud.update(course).subscribe(
148 console.debug('archived: ' + val);
149 this.archiveSuccessString.current()
150 .then(str => this.toast.success(str));
152 this.archiveFailedString.current()
153 .then(str => this.toast.danger(str));
161 deleteSelected(idlObject: IdlObject[]) {
162 this.courseSvc.disassociateMaterials(idlObject).then(res => {
163 idlObject.forEach(object => {
164 object.isdeleted(true);
166 this.pcrud.autoApply(idlObject).subscribe(
168 console.debug('deleted: ' + val);
169 this.deleteSuccessString.current()
170 .then(str => this.toast.success(str));
173 this.deleteFailedString.current()
174 .then(str => this.toast.danger(str));
176 () => this.grid.reload()