1 import {Component, Input, ViewChild, OnInit} from '@angular/core';
2 import {IdlObject} from '@eg/core/idl.service';
3 import {PcrudService} from '@eg/core/pcrud.service';
4 import {GridComponent} from '@eg/share/grid/grid.component';
5 import {Pager} from '@eg/share/util/pager';
6 import {GridDataSource, GridColumn, GridRowFlairEntry} from '@eg/share/grid/grid';
7 import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
8 import {StringComponent} from '@eg/share/string/string.component';
9 import {ToastService} from '@eg/share/toast/toast.service';
12 templateUrl: './course-list.component.html'
15 export class CourseListComponent implements OnInit {
17 @ViewChild('editDialog', { static: true }) editDialog: FmRecordEditorComponent;
18 @ViewChild('grid', { static: true }) grid: GridComponent;
19 @ViewChild('successString', { static: true }) successString: StringComponent;
20 @ViewChild('createString', { static: false }) createString: StringComponent;
21 @ViewChild('createErrString', { static: false }) createErrString: StringComponent;
22 @ViewChild('updateFailedString', { static: false }) updateFailedString: StringComponent;
23 @ViewChild('deleteFailedString', { static: true }) deleteFailedString: StringComponent;
24 @ViewChild('deleteSuccessString', { static: true }) deleteSuccessString: StringComponent;
25 @ViewChild('flairTooltip', { static: true }) private flairTooltip: StringComponent;
26 rowFlairCallback: (row: any) => GridRowFlairEntry;
27 @Input() sort_field: string;
28 @Input() idl_class = "acmc";
29 @Input() dialog_size: 'sm' | 'lg' = 'lg';
30 @Input() table_name = "Course";
31 grid_source: GridDataSource = new GridDataSource();
35 private pcrud: PcrudService,
36 private toast: ToastService,
45 * Gets the data, specified by the class, that is available.
48 this.grid_source.getRows = (pager: Pager, sort: any[]) => {
49 const orderBy: any = {};
51 // Sort specified from grid
52 orderBy[this.idl_class] = sort[0].name + ' ' + sort[0].dir;
53 } else if (this.sort_field) {
55 orderBy[this.idl_class] = this.sort_field;
62 return this.pcrud.retrieveAll(this.idl_class, searchOps, {fleshSelectors: true});
67 this.rowFlairCallback = (row: any): GridRowFlairEntry => {
68 const flair = {icon: null, title: null};
70 flair.icon = 'not_interested';
71 flair.title = this.flairTooltip.text;
77 gridCellClassCallback = (row: any, col: GridColumn): string => {
78 if (col.name === 'id' && row.a[0] < 100) {
84 showEditDialog(standingPenalty: IdlObject): Promise<any> {
85 this.editDialog.mode = 'update';
86 this.editDialog.recordId = standingPenalty['id']();
87 return new Promise((resolve, reject) => {
88 this.editDialog.open({size: this.dialog_size}).subscribe(
90 this.successString.current()
91 .then(str => this.toast.success(str));
96 this.updateFailedString.current()
97 .then(str => this.toast.danger(str));
105 this.editDialog.mode = 'create';
106 this.editDialog.recordId = null;
107 this.editDialog.record = null;
108 this.editDialog.open({size: this.dialog_size}).subscribe(
110 this.createString.current()
111 .then(str => this.toast.success(str));
115 if (!rejection.dismissed) {
116 this.createErrString.current()
117 .then(str => this.toast.danger(str));
123 editSelected(fields: IdlObject[]) {
124 // Edit each IDL thing one at a time
125 const editOneThing = (field_object: IdlObject) => {
126 if (!field_object) { return; }
127 this.showEditDialog(field_object).then(
128 () => editOneThing(fields.shift()));
130 editOneThing(fields.shift());
133 deleteSelected(idl_object: IdlObject[]) {
134 idl_object.forEach(idl_object => idl_object.isdeleted(true));
135 this.pcrud.autoApply(idl_object).subscribe(
137 console.debug('deleted: ' + val);
138 this.deleteSuccessString.current()
139 .then(str => this.toast.success(str));
142 this.deleteFailedString.current()
143 .then(str => this.toast.danger(str));
145 () => this.grid.reload()