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';
17 import {CourseAssociateMaterialComponent
18 } from './course-associate-material.component';
20 import {CourseAssociateUsersComponent
21 } from './course-associate-users.component';
24 templateUrl: './course-list.component.html'
27 export class CourseListComponent implements OnInit {
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;
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[] = [];
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
66 this.grid.onRowActivate.subscribe((course:IdlObject) => {
67 let idToEdit = course.id();
68 this.navigateToCoursePage(idToEdit);
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.idl_class] = sort[0].name + ' ' + sort[0].dir;
81 } else if (this.sort_field) {
83 orderBy[this.idl_class] = this.sort_field;
90 return this.pcrud.retrieveAll(this.idl_class, 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
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(course => {
145 course.is_archived(true);
147 this.pcrud.update(course).subscribe(
149 console.debug('archived: ' + val);
150 this.archiveSuccessString.current()
151 .then(str => this.toast.success(str));
153 this.archiveFailedString.current()
154 .then(str => this.toast.danger(str));
162 deleteSelected(idl_object: IdlObject[]) {
163 this.courseSvc.disassociateMaterials(idl_object).then(res => {
164 idl_object.forEach(idl_object => {
165 idl_object.isdeleted(true)
167 this.pcrud.autoApply(idl_object).subscribe(
169 console.debug('deleted: ' + val);
170 this.deleteSuccessString.current()
171 .then(str => this.toast.success(str));
174 this.deleteFailedString.current()
175 .then(str => this.toast.danger(str));
177 () => this.grid.reload()