]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/admin/server/course-reserves/course-list.component.ts
LP#1849212 Course List Ui
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / admin / server / course-reserves / course-list.component.ts
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';
10
11 @Component({
12     templateUrl: './course-list.component.html'
13 })
14
15 export class CourseListComponent implements OnInit { 
16  
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();
32     search_value = '';
33
34     constructor(
35             private pcrud: PcrudService,
36             private toast: ToastService,
37     ){}
38
39     ngOnInit() {
40         this.getSource();
41         this.rowFlair();
42     }
43
44     /**
45      * Gets the data, specified by the class, that is available.
46      */
47     getSource() {
48         this.grid_source.getRows = (pager: Pager, sort: any[]) => {
49             const orderBy: any = {};
50             if (sort.length) {
51                 // Sort specified from grid
52                 orderBy[this.idl_class] = sort[0].name + ' ' + sort[0].dir;
53             } else if (this.sort_field) {
54                 // Default sort field
55                 orderBy[this.idl_class] = this.sort_field;
56             }
57             const searchOps = {
58                 offset: pager.offset,
59                 limit: pager.limit,
60                 order_by: orderBy
61             };
62             return this.pcrud.retrieveAll(this.idl_class, searchOps, {fleshSelectors: true});
63         };
64     }
65
66     rowFlair() {
67         this.rowFlairCallback = (row: any): GridRowFlairEntry => {
68             const flair = {icon: null, title: null};
69             if (row.id() < 100) {
70                 flair.icon = 'not_interested';
71                 flair.title = this.flairTooltip.text;
72             }
73             return flair;
74         };
75     }
76
77     gridCellClassCallback = (row: any, col: GridColumn): string => {
78         if (col.name === 'id' && row.a[0] < 100) {
79             return 'text-danger';
80         }
81         return '';
82     }
83
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(
89                 result => {
90                     this.successString.current()
91                         .then(str => this.toast.success(str));
92                     this.grid.reload();
93                     resolve(result);
94                 },
95                 error => {
96                     this.updateFailedString.current()
97                         .then(str => this.toast.danger(str));
98                     reject(error);
99                 }
100             );
101         });
102     }
103
104     createNew() {
105         this.editDialog.mode = 'create';
106         this.editDialog.recordId = null;
107         this.editDialog.record = null;
108         this.editDialog.open({size: this.dialog_size}).subscribe(
109             ok => {
110                 this.createString.current()
111                     .then(str => this.toast.success(str));
112                 this.grid.reload();
113             },
114             rejection => {
115                 if (!rejection.dismissed) {
116                     this.createErrString.current()
117                         .then(str => this.toast.danger(str));
118                 }
119             }
120         );
121     }
122
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()));
129         };
130         editOneThing(fields.shift());
131     }
132
133     deleteSelected(idl_object: IdlObject[]) {
134             idl_object.forEach(idl_object => idl_object.isdeleted(true));
135             this.pcrud.autoApply(idl_object).subscribe(
136                 val => {
137                     console.debug('deleted: ' + val);
138                     this.deleteSuccessString.current()
139                         .then(str => this.toast.success(str));
140                 },
141                 err => {
142                     this.deleteFailedString.current()
143                         .then(str => this.toast.danger(str));
144                 },
145                 ()  => this.grid.reload()
146             );
147         };
148 }
149