]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/admin/local/standing-penalty.component.ts
LP#1840327: (follow-up) various fixes and improvements
[working/Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / admin / local / standing-penalty.component.ts
1 import {Pager} from '@eg/share/util/pager';
2 import {Component, OnInit, Input, ViewChild} from '@angular/core';
3 import {GridComponent} from '@eg/share/grid/grid.component';
4 import {GridDataSource, GridColumn, GridRowFlairEntry} from '@eg/share/grid/grid';
5 import {IdlObject} from '@eg/core/idl.service';
6 import {PcrudService} from '@eg/core/pcrud.service';
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: './standing-penalty.component.html'
13 })
14
15 export class StandingPenaltyComponent implements OnInit {
16     recId: number;
17     gridDataSource: GridDataSource;
18     initDone = false;
19     cspSource: GridDataSource = new GridDataSource();
20
21     @ViewChild('editDialog') editDialog: FmRecordEditorComponent;
22     @ViewChild('grid') grid: GridComponent;
23     @ViewChild('successString') successString: StringComponent;
24     @ViewChild('createString') createString: StringComponent;
25     @ViewChild('createErrString') createErrString: StringComponent;
26     @ViewChild('updateFailedString') updateFailedString: StringComponent;
27     @ViewChild('deleteFailedString') deleteFailedString: StringComponent;
28     @ViewChild('deleteSuccessString') deleteSuccessString: StringComponent;
29     @ViewChild('cspFlairTooltip') private cspFlairTooltip: StringComponent;
30
31     cspRowFlairCallback: (row: any) => GridRowFlairEntry;
32
33     canCreate: boolean;
34     canDelete: boolean;
35     deleteSelected: (rows: IdlObject[]) => void;
36
37     permissions: {[name: string]: boolean};
38
39     // Default sort field, used when no grid sorting is applied.
40     @Input() sortField: string;
41
42     @Input() idlClass = 'csp';
43     // Size of create/edito dialog.  Uses large by default.
44     @Input() dialogSize: 'sm' | 'lg' = 'lg';
45     // Optional comma-separated list of read-only fields
46     // @Input() readonlyFields: string;
47
48     constructor(
49         private pcrud: PcrudService,
50         private toast: ToastService
51     ) {
52         this.gridDataSource = new GridDataSource();
53     }
54
55     ngOnInit() {
56         this.initDone = true;
57         this.cspSource.getRows = (pager: Pager, sort: any[]) => {
58             const orderBy: any = {};
59             if (sort.length) {
60                 // Sort specified from grid
61                 orderBy[this.idlClass] = sort[0].name + ' ' + sort[0].dir;
62             } else if (this.sortField) {
63                 // Default sort field
64                 orderBy[this.idlClass] = this.sortField;
65             }
66
67             const searchOps = {
68                 offset: pager.offset,
69                 limit: pager.limit,
70                 order_by: orderBy
71             };
72             return this.pcrud.retrieveAll('csp', searchOps, {fleshSelectors: true});
73         };
74
75         this.cspRowFlairCallback = (row: any): GridRowFlairEntry => {
76             const flair = {icon: null, title: null};
77             if (row.id() < 100) {
78                 flair.icon = 'not_interested';
79                 flair.title = this.cspFlairTooltip.text;
80             }
81             return flair;
82         };
83
84         this.deleteSelected = (idlThings: IdlObject[]) => {
85             idlThings.forEach(idlThing => idlThing.isdeleted(true));
86             this.pcrud.autoApply(idlThings).subscribe(
87                 val => {
88                     console.debug('deleted: ' + val);
89                     this.deleteSuccessString.current()
90                         .then(str => this.toast.success(str));
91                 },
92                 err => {
93                     this.deleteFailedString.current()
94                         .then(str => this.toast.danger(str));
95                 },
96                 ()  => this.grid.reload()
97             );
98         };
99
100     }
101
102     cspReadonlyOverride = (field: string, csp: IdlObject): boolean => {
103         if (csp.id() >= 100 || csp.id() === undefined) {
104             return true;
105         }
106         return false;
107     }
108
109     cspGridCellClassCallback = (row: any, col: GridColumn): string => {
110         if (col.name === 'id' && row.a[0] < 100) {
111             return 'text-danger';
112         }
113         return '';
114     }
115
116     showEditDialog(standingPenalty: IdlObject): Promise<any> {
117         this.editDialog.mode = 'update';
118         this.editDialog.recordId = standingPenalty['id']();
119         return new Promise((resolve, reject) => {
120             this.editDialog.open({size: this.dialogSize}).subscribe(
121                 result => {
122                     this.successString.current()
123                         .then(str => this.toast.success(str));
124                     this.grid.reload();
125                     resolve(result);
126                 },
127                 error => {
128                     this.updateFailedString.current()
129                         .then(str => this.toast.danger(str));
130                     reject(error);
131                 }
132             );
133         });
134     }
135
136     editSelected(standingPenaltyFields: IdlObject[]) {
137         // Edit each IDL thing one at a time
138         const editOneThing = (standingPenalty: IdlObject) => {
139             if (!standingPenalty) { return; }
140
141             this.showEditDialog(standingPenalty).then(
142                 () => editOneThing(standingPenaltyFields.shift()));
143         };
144
145         editOneThing(standingPenaltyFields.shift());
146     }
147
148     createNew() {
149         this.editDialog.mode = 'create';
150         // We reuse the same editor for all actions.  Be sure
151         // create action does not try to modify an existing record.
152         this.editDialog.recordId = null;
153         this.editDialog.record = null;
154         this.editDialog.open({size: this.dialogSize}).subscribe(
155             ok => {
156                 this.createString.current()
157                     .then(str => this.toast.success(str));
158                 this.grid.reload();
159             },
160             rejection => {
161                 if (!rejection.dismissed) {
162                     this.createErrString.current()
163                         .then(str => this.toast.danger(str));
164                 }
165             }
166         );
167     }
168
169 }
170