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 {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
11 import {StringComponent} from '@eg/share/string/string.component';
12 import {ToastService} from '@eg/share/toast/toast.service';
13 import {LocaleService} from '@eg/core/locale.service';
14 import {AuthService} from '@eg/core/auth.service';
15 import {OrgService} from '@eg/core/org.service';
16 import {OrgFamily} from '@eg/share/org-family-select/org-family-select.component';
19 selector: 'eg-course-term-grid',
20 templateUrl: './course-term-grid.component.html'
23 export class TermListComponent implements OnInit, AfterViewInit {
25 @ViewChild('editDialog', { static: true }) editDialog: FmRecordEditorComponent;
26 @ViewChild('deleteLinkedTermWarning', { static: true }) deleteLinkedTermWarning: ConfirmDialogComponent;
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('deleteLinkedTermWarningString', { static: true }) deleteLinkedTermWarningString: StringComponent;
36 @Input() sortField: string;
37 @Input() idlClass = 'acmt';
38 @Input() dialog_size: 'sm' | 'lg' = 'lg';
39 @Input() tableName = 'Term';
40 grid_source: GridDataSource = new GridDataSource();
41 currentMaterials: any[] = [];
44 searchOrgs: OrgFamily;
45 defaultTerm: IdlObject;
50 private courseSvc: CourseService,
51 private locale: LocaleService,
52 private auth: AuthService,
53 private idl: IdlService,
54 private org: OrgService,
55 private pcrud: PcrudService,
56 private router: Router,
57 private toast: ToastService
62 this.defaultTerm = this.idl.create('acmt');
63 this.defaultOuId = this.auth.user().ws_ou() || this.org.root().id();
64 this.defaultTerm.owning_lib(this.defaultOuId);
65 this.searchOrgs = {primaryOrgId: this.defaultOuId};
69 this.grid.onRowActivate.subscribe((term: IdlObject) => {
70 const idToEdit = term.id();
71 this.editSelected([term]);
77 * Gets the data, specified by the class, that is available.
80 this.grid_source.getRows = (pager: Pager, sort: any[]) => {
81 const orderBy: any = {};
83 // Sort specified from grid
84 orderBy[this.idlClass] = sort[0].name + ' ' + sort[0].dir;
85 } else if (this.sortField) {
87 orderBy[this.idlClass] = this.sortField;
89 const search: any = new Array();
90 const orgFilter: any = {};
91 orgFilter['owning_lib'] =
92 this.searchOrgs.orgIds || [this.defaultOuId];
93 search.push(orgFilter);
99 return this.pcrud.search(this.idlClass, search, searchOps, {fleshSelectors: true});
104 this.editDialog.mode = 'create';
105 const course_module_term = this.idl.create('acmt');
106 course_module_term.owning_lib(this.auth.user().ws_ou());
107 this.editDialog.recordId = null;
108 this.editDialog.record = course_module_term;
109 this.editDialog.open({size: this.dialog_size}).subscribe(
111 this.createString.current()
112 .then(str => this.toast.success(str));
115 // eslint-disable-next-line rxjs/no-implicit-any-catch
116 (rejection: any) => {
117 if (!rejection.dismissed) {
118 this.createErrString.current()
119 .then(str => this.toast.danger(str));
125 editSelected(fields: IdlObject[]) {
126 this.editDialog.mode = 'update';
127 // Edit each IDL thing one at a time
128 const course_ids = [];
129 fields.forEach(field => {
130 this.editDialog.record = field;
131 this.editDialog.open({size: this.dialog_size}).subscribe(
133 this.createString.current()
134 .then(str => this.toast.success(str));
137 // eslint-disable-next-line rxjs/no-implicit-any-catch
138 (rejection: any) => {
139 if (!rejection.dismissed) {
140 this.createErrString.current()
141 .then(str => this.toast.danger(str));
148 deleteSelected(fields: IdlObject[]) {
149 console.log(this.deleteLinkedTermWarningString);
150 fields.forEach(field => {
151 let termHasLinkedCourses = false;
152 this.courseSvc.getTermMaps(field.id()).subscribe(map => {
154 termHasLinkedCourses = true;
156 }, (err: unknown) => {
159 if (termHasLinkedCourses) {
160 this.termToDelete = field.name();
161 this.deleteLinkedTermWarning.open().toPromise().then(yes => {
162 if (!yes) { return; }
163 this.doDelete(field);
166 this.doDelete(field);
173 doDelete(idlThing: IdlObject) {
174 idlThing.isdeleted(true);
175 this.pcrud.autoApply(idlThing).subscribe(
177 console.debug('deleted: ' + val);
178 this.deleteSuccessString.current()
179 .then(str => this.toast.success(str));
182 this.deleteFailedString.current()
183 .then(str => this.toast.danger(str));
185 () => this.grid.reload()