]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/admin/local/standing-penalty.component.ts
lp1843640 Standing Penalty Followup
[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', { static: true }) editDialog: FmRecordEditorComponent;
22     @ViewChild('grid', { static: true }) grid: GridComponent;
23     @ViewChild('successString', { static: true }) successString: StringComponent;
24     @ViewChild('createString', { static: false }) createString: StringComponent;
25     @ViewChild('createErrString', { static: false }) createErrString: StringComponent;
26     @ViewChild('updateFailedString', { static: false }) updateFailedString: StringComponent;
27     @ViewChild('deleteFailedString', { static: true }) deleteFailedString: StringComponent;
28     @ViewChild('deleteSuccessString', { static: true }) deleteSuccessString: StringComponent;
29     @ViewChild('cspFlairTooltip', { static: true }) 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         this.grid.onRowActivate.subscribe(
101             (idlThing: IdlObject) => this.showEditDialog(idlThing)
102         );
103
104     }
105
106     cspReadonlyOverride = (field: string, csp: IdlObject): boolean => {
107         if (csp.id() >= 100 || csp.id() === undefined) {
108             return true;
109         }
110         return false;
111     }
112
113     cspGridCellClassCallback = (row: any, col: GridColumn): string => {
114         if (col.name === 'id' && row.a[0] < 100) {
115             return 'text-danger';
116         }
117         return '';
118     }
119
120     showEditDialog(standingPenalty: IdlObject): Promise<any> {
121         this.editDialog.mode = 'update';
122         this.editDialog.recordId = standingPenalty['id']();
123         return new Promise((resolve, reject) => {
124             this.editDialog.open({size: this.dialogSize}).subscribe(
125                 result => {
126                     this.successString.current()
127                         .then(str => this.toast.success(str));
128                     this.grid.reload();
129                     resolve(result);
130                 },
131                 error => {
132                     this.updateFailedString.current()
133                         .then(str => this.toast.danger(str));
134                     reject(error);
135                 }
136             );
137         });
138     }
139
140     editSelected(standingPenaltyFields: IdlObject[]) {
141         // Edit each IDL thing one at a time
142         const editOneThing = (standingPenalty: IdlObject) => {
143             if (!standingPenalty) { return; }
144
145             this.showEditDialog(standingPenalty).then(
146                 () => editOneThing(standingPenaltyFields.shift()));
147         };
148
149         editOneThing(standingPenaltyFields.shift());
150     }
151
152     createNew() {
153         this.editDialog.mode = 'create';
154         // We reuse the same editor for all actions.  Be sure
155         // create action does not try to modify an existing record.
156         this.editDialog.recordId = null;
157         this.editDialog.record = null;
158         this.editDialog.open({size: this.dialogSize}).subscribe(
159             ok => {
160                 this.createString.current()
161                     .then(str => this.toast.success(str));
162                 this.grid.reload();
163             },
164             rejection => {
165                 if (!rejection.dismissed) {
166                     this.createErrString.current()
167                         .then(str => this.toast.danger(str));
168                 }
169             }
170         );
171     }
172
173 }
174