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';
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, AfterViewInit {
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;
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
63 this.grid.onRowActivate.subscribe((course: IdlObject) => {
64 const idToEdit = course.id();
65 this.navigateToCoursePage(idToEdit);
70 acmtcmQueryParams (row: any): {gridFilters: string} {
71 return {gridFilters: '{"course":' + row.id() + '}'};
76 * Gets the data, specified by the class, that is available.
79 this.grid_source.getRows = (pager: Pager, sort: any[]) => {
80 const orderBy: any = {};
82 // Sort specified from grid
83 orderBy[this.idlClass] = sort[0].name + ' ' + sort[0].dir;
84 } else if (this.sortField) {
86 orderBy[this.idlClass] = this.sortField;
93 return this.pcrud.retrieveAll(this.idlClass, searchOps, {fleshSelectors: true});
97 navigateToCoursePage(id_arr: IdlObject[]) {
98 if (typeof id_arr === 'number') { id_arr = [id_arr]; }
100 id_arr.forEach(id => {console.log(this.router.url);
101 urls.push([this.locale.currentLocaleCode() + this.router.url + '/' + id]);
103 if (id_arr.length === 1) {
104 this.router.navigate([this.router.url + '/' + id_arr[0]]);
106 urls.forEach(url => {
113 this.editDialog.mode = 'create';
114 this.editDialog.recordId = null;
115 this.editDialog.record = null;
116 this.editDialog.open({size: this.dialog_size}).subscribe(
118 this.createString.current()
119 .then(str => this.toast.success(str));
123 if (!rejection.dismissed) {
124 this.createErrString.current()
125 .then(str => this.toast.danger(str));
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());
138 course_ids.push(field['id']);
141 this.navigateToCoursePage(course_ids);
144 archiveSelected(course: IdlObject[]) {
145 this.courseSvc.disassociateMaterials(course).then(res => {
146 course.forEach(courseToArchive => {
147 courseToArchive.is_archived(true);
149 this.pcrud.update(course).subscribe(
151 console.debug('archived: ' + val);
152 this.archiveSuccessString.current()
153 .then(str => this.toast.success(str));
155 this.archiveFailedString.current()
156 .then(str => this.toast.danger(str));
164 deleteSelected(idlObject: IdlObject[]) {
165 this.courseSvc.disassociateMaterials(idlObject).then(res => {
166 idlObject.forEach(object => {
167 object.isdeleted(true);
169 this.pcrud.autoApply(idlObject).subscribe(
171 console.debug('deleted: ' + val);
172 this.deleteSuccessString.current()
173 .then(str => this.toast.success(str));
176 this.deleteFailedString.current()
177 .then(str => this.toast.danger(str));
179 () => this.grid.reload()