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