1 import {Component, Input, ViewChild, OnInit, AfterViewInit} from '@angular/core';
2 import {Router} from '@angular/router';
3 import {IdlObject, IdlService} 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';
13 import {AuthService} from '@eg/core/auth.service';
14 import {OrgService} from '@eg/core/org.service';
15 import {OrgFamily} from '@eg/share/org-family-select/org-family-select.component';
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, AfterViewInit {
29 @ViewChild('editDialog', { static: true }) editDialog: FmRecordEditorComponent;
30 @ViewChild('grid') grid: GridComponent;
31 @ViewChild('successString', { static: true }) successString: StringComponent;
32 @ViewChild('createString') createString: StringComponent;
33 @ViewChild('createErrString') createErrString: StringComponent;
34 @ViewChild('updateFailedString') 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() sortField: string;
45 @Input() idlClass = 'acmc';
46 @Input() dialog_size: 'sm' | 'lg' = 'lg';
47 @Input() tableName = 'Course';
48 grid_source: GridDataSource = new GridDataSource();
49 currentMaterials: any[] = [];
52 searchOrgs: OrgFamily;
53 defaultTerm: IdlObject;
57 private courseSvc: CourseService,
58 private locale: LocaleService,
59 private auth: AuthService,
60 private idl: IdlService,
61 private org: OrgService,
62 private pcrud: PcrudService,
63 private router: Router,
64 private toast: ToastService
69 this.defaultTerm = this.idl.create('acmt');
70 this.defaultOuId = this.auth.user().ws_ou() || this.org.root().id();
71 this.defaultTerm.owning_lib(this.defaultOuId);
72 this.searchOrgs = {primaryOrgId: this.defaultOuId};
76 this.grid.onRowActivate.subscribe((course: IdlObject) => {
77 const idToEdit = course.id();
78 this.navigateToCoursePage(idToEdit);
83 acmtcmQueryParams (row: any): {gridFilters: string} {
84 return {gridFilters: '{"course":' + row.id() + '}'};
89 * Gets the data, specified by the class, that is available.
92 this.grid_source.getRows = (pager: Pager, sort: any[]) => {
93 const orderBy: any = {};
95 // Sort specified from grid
96 orderBy[this.idlClass] = sort[0].name + ' ' + sort[0].dir;
97 } else if (this.sortField) {
99 orderBy[this.idlClass] = this.sortField;
101 const search: any = new Array();
102 const orgFilter: any = {};
103 orgFilter['owning_lib'] =
104 this.searchOrgs.orgIds || [this.defaultOuId];
105 search.push(orgFilter);
107 offset: pager.offset,
111 return this.pcrud.search(this.idlClass, search, searchOps, {fleshSelectors: true});
115 navigateToCoursePage(id_arr: IdlObject[]) {
116 if (typeof id_arr === 'number') { id_arr = [id_arr]; }
118 id_arr.forEach(id => {console.log(this.router.url);
119 urls.push([this.locale.currentLocaleCode() + this.router.url + '/' + id]);
121 if (id_arr.length === 1) {
122 this.router.navigate([this.router.url + '/' + id_arr[0]]);
124 urls.forEach(url => {
131 this.editDialog.mode = 'create';
132 this.editDialog.recordId = null;
133 this.editDialog.record = null;
134 this.editDialog.open({size: this.dialog_size}).subscribe(
136 this.createString.current()
137 .then(str => this.toast.success(str));
141 if (!rejection.dismissed) {
142 this.createErrString.current()
143 .then(str => this.toast.danger(str));
149 editSelected(fields: IdlObject[]) {
150 // Edit each IDL thing one at a time
151 const course_ids = [];
152 fields.forEach(field => {
153 if (typeof field['id'] === 'function') {
154 course_ids.push(field.id());
156 course_ids.push(field['id']);
159 this.navigateToCoursePage(course_ids);
162 archiveSelected(course: IdlObject[]) {
163 this.courseSvc.disassociateMaterials(course).then(res => {
164 course.forEach(courseToArchive => {
165 courseToArchive.is_archived(true);
167 this.pcrud.update(course).subscribe(
169 console.debug('archived: ' + val);
170 this.archiveSuccessString.current()
171 .then(str => this.toast.success(str));
173 this.archiveFailedString.current()
174 .then(str => this.toast.danger(str));
182 deleteSelected(idlObject: IdlObject[]) {
183 this.courseSvc.disassociateMaterials(idlObject).then(res => {
184 idlObject.forEach(object => {
185 object.isdeleted(true);
187 this.pcrud.autoApply(idlObject).subscribe(
189 console.debug('deleted: ' + val);
190 this.deleteSuccessString.current()
191 .then(str => this.toast.success(str));
194 this.deleteFailedString.current()
195 .then(str => this.toast.danger(str));
197 () => this.grid.reload()