]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/reporter/simple/sr-editor.component.ts
LP1959048: manual ng lint fixes
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / reporter / simple / sr-editor.component.ts
1 import {Component, OnInit, ViewChild} from '@angular/core';
2 import {Router, ActivatedRoute} from '@angular/router';
3 import {Location} from '@angular/common';
4 import {of} from 'rxjs';
5 import {NgbNav} from '@ng-bootstrap/ng-bootstrap';
6 import {EventService} from '@eg/core/event.service';
7 import {ToastService} from '@eg/share/toast/toast.service';
8 import {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
9 import {IdlService, IdlObject} from '@eg/core/idl.service';
10 import {PcrudService} from '@eg/core/pcrud.service';
11 import {StringComponent} from '@eg/share/string/string.component';
12 import {SimpleReporterService, SRTemplate} from './simple-reporter.service';
13
14 @Component({
15     templateUrl: './sr-editor.component.html',
16     styleUrls: ['./sr-editor.component.css'],
17 })
18
19 export class SREditorComponent implements OnInit {
20
21     rptType = '';
22     oldRptType = '';
23     name = '';
24     templ: SRTemplate = null;
25     isNew = true;
26     sourceClass: IdlObject = null;
27     fieldGroups: IdlObject[] = [];
28     allFields: IdlObject[] = [];
29     pageTitle = '';
30     forcedFields = 0;
31     _isDirty = false;
32
33     @ViewChild('templateSaved', { static: true }) templateSavedString: StringComponent;
34     @ViewChild('templateSaveError', { static: true }) templateSaveErrorString: StringComponent;
35     @ViewChild('newTitle', { static: true }) newTitleString: StringComponent;
36     @ViewChild('editTitle', { static: true }) editTitleString: StringComponent;
37     @ViewChild('srEditorTabs', { static: true }) tabs: NgbNav;
38     @ViewChild('changeTypeDialog', { static: false }) changeTypeDialog: ConfirmDialogComponent;
39     @ViewChild('closeFormDialog', { static: false }) closeFormDialog: ConfirmDialogComponent;
40
41
42     constructor(
43         private route: ActivatedRoute,
44         private router: Router,
45         private location: Location,
46         private toast: ToastService,
47         private evt: EventService,
48         private idl: IdlService,
49         private pcrud: PcrudService,
50         private srSvc: SimpleReporterService
51     ) {
52         const id = this.route.snapshot.paramMap.get('id');
53         if ( id === null ) {
54             this.templ = new SRTemplate();
55         } else {
56             this.isNew = false;
57             this.loadTemplate(Number(id))
58             .then( x => this.reloadFields(this.templ.fmClass));
59         }
60
61     }
62
63     ngOnInit() {
64         this._setPageTitle();
65     }
66
67     _setPageTitle() {
68          if ( this.isNew ) {
69             this.newTitleString.current()
70             .then(str => this.pageTitle = str );
71         } else {
72             this.editTitleString.current()
73             .then(str => this.pageTitle = str );
74         }
75     }
76
77     reloadFields(fmClass: string) {
78         this.allFields = [];
79         this.forcedFields = 0;
80         this.sourceClass = this.idl.classes[fmClass];
81         ('field_groups' in this.sourceClass) ?
82             // grab a clone
83             this.fieldGroups = this.sourceClass.field_groups.map(x => ({...x})) :
84             this.fieldGroups = [];
85
86         this.sourceClass.fields.forEach(f => {
87
88             f.transform = this.srSvc.defaultTransform();
89             f.operator = this.srSvc.defaultOperator(f.datatype);
90
91             if ( f.suggest_transform ) {
92                 f.transform = this.srSvc.getTransformByName(f.suggest_transform);
93             }
94
95             if ( f.suggest_operator ) {
96                 f.operator = this.srSvc.getOperatorByName(f.suggest_operator);
97             }
98
99             if ( f.force_transform ) {
100                 if ( typeof f.force_transform === 'string' ) {
101                     f.transform = this.srSvc.getTransformByName(f.force_transform);
102                 } else {
103                     f.transform = this.srSvc.getGenericTransformWithParams(f.force_transform);
104                 }
105             }
106
107             if ( f.force_operator ) {
108                 f.operator = this.srSvc.getOperatorByName(f.force_operator);
109             }
110
111             if ( f.force_filter ) {
112                 if ( this.templ.filterFields.findIndex(el => el.name === f.name) === -1 ) {
113                     this.templ.filterFields.push(f);
114                     this.forcedFields++;
115                 }
116             }
117
118             this.allFields.push(f);
119             if ( 'field_groups' in f ) {
120                 f.field_groups.forEach(g => {
121                     const idx = this.fieldGroups.findIndex(el => el.name === g);
122                     if ( idx > -1 ) {
123                         if ( !('members' in this.fieldGroups[idx]) ) {
124                             this.fieldGroups[idx].members = [];
125                         }
126                         this.fieldGroups[idx].members.push(f);
127                     }
128                 });
129             }
130         });
131
132         this.allFields.sort( (a, b) => a.label.localeCompare(b.label) );
133
134     }
135
136     changeReportType() {
137         if ( this.oldRptType === '' || (this.templ.displayFields.length === 0 && this.templ.filterFields.length === this.forcedFields) ) {
138             this.oldRptType = this.rptType;
139             this.templ = new SRTemplate();
140             this.templ.fmClass = this.rptType;
141             this.reloadFields(this.rptType);
142             this._isDirty = true;
143         } else {
144             return this.changeTypeDialog.open()
145             .subscribe(confirmed => {
146                 if ( confirmed ) {
147                     this.oldRptType = this.rptType;
148                     this.templ = new SRTemplate();
149                     this.templ.fmClass = this.rptType;
150                     this.reloadFields(this.rptType);
151                     this._isDirty = true;
152                 } else {
153                     this.rptType = this.oldRptType;
154                 }
155             });
156         }
157     }
158
159     dirty() {
160         this._isDirty = true;
161     }
162
163     isDirty() {
164         return this._isDirty;
165     }
166
167     readyToSave() {
168         return ( this.sourceClass !== null && this.name !== '' );
169     }
170
171     readyToSchedule = () => {
172         return ( this.readyToSave() && this.templ.displayFields.length > 0 );
173     }
174
175     canLeaveEditor() {
176         if ( this.isDirty() ) {
177             return this.closeFormDialog.open();
178         } else {
179             return of(true);
180         }
181     }
182
183     loadTemplate(id: number) {
184         return this.srSvc.loadTemplate(id)
185         .then(idl => {
186             this.templ = new SRTemplate(idl);
187             this.name = this.templ.name;
188             this.rptType = this.templ.fmClass;
189             this.oldRptType = this.templ.fmClass;
190         });
191     }
192
193     saveTemplate = (scheduleNow) => {
194         this.templ.name = this.name;
195
196         this.srSvc.saveTemplate(this.templ, scheduleNow)
197         .then(rt => {
198             this._isDirty = false;
199             // It appears that calling pcrud.create will return the newly created object,
200             // while pcrud.update just gives you back the id of the updated object.
201             if ( typeof rt === 'object' ) {
202                 this.templ = new SRTemplate(rt); // pick up the id and create_time fields
203             }
204             this.templateSavedString.current()
205             .then(str => {
206                 this.toast.success(str);
207             });
208             if (scheduleNow) {
209                 // we're done, so jump to the main page
210                 this.router.navigate(['/staff/reporter/simple']);
211             } else if (this.isNew) {
212                 // we've successfully saved, so we're no longer new
213                 // adjust page title...
214                 this.isNew = false;
215                 this._setPageTitle();
216                 // ... and make the URL say that we're editing
217                 const url = this.router.createUrlTree(['/staff/reporter/simple/edit/' + this.templ.id]).toString();
218                 this.location.go(url); // go without reloading
219             }
220         },
221         err => {
222             this.templateSaveErrorString.current()
223             .then(str => {
224                 this.toast.danger(str + err);
225                 console.error('Error saving template: %o', err);
226             });
227         });
228     }
229
230     closeForm() {
231         this.router.navigate(['/staff/reporter/simple']);
232     }
233
234 }
235