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';
15 templateUrl: './sr-editor.component.html',
16 styleUrls: ['./sr-editor.component.css'],
19 export class SREditorComponent implements OnInit {
24 templ: SRTemplate = null;
26 sourceClass: IdlObject = null;
27 fieldGroups: IdlObject[] = [];
28 allFields: IdlObject[] = [];
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;
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
52 const id = this.route.snapshot.paramMap.get('id');
54 this.templ = new SRTemplate();
57 this.loadTemplate(Number(id))
58 .then( x => this.reloadFields(this.templ.fmClass));
69 this.newTitleString.current()
70 .then(str => this.pageTitle = str );
72 this.editTitleString.current()
73 .then(str => this.pageTitle = str );
77 reloadFields(fmClass: string) {
79 this.forcedFields = 0;
80 this.sourceClass = this.idl.classes[fmClass];
81 ('field_groups' in this.sourceClass) ?
83 this.fieldGroups = this.sourceClass.field_groups.map(x => ({...x})) :
84 this.fieldGroups = [];
86 this.sourceClass.fields.forEach(f => {
88 f.transform = this.srSvc.defaultTransform();
89 f.operator = this.srSvc.defaultOperator(f.datatype);
91 if ( f.suggest_transform ) {
92 f.transform = this.srSvc.getTransformByName(f.suggest_transform);
95 if ( f.suggest_operator ) {
96 f.operator = this.srSvc.getOperatorByName(f.suggest_operator);
99 if ( f.force_transform ) {
100 if ( typeof f.force_transform === 'string' ) {
101 f.transform = this.srSvc.getTransformByName(f.force_transform);
103 f.transform = this.srSvc.getGenericTransformWithParams(f.force_transform);
107 if ( f.force_operator ) {
108 f.operator = this.srSvc.getOperatorByName(f.force_operator);
111 if ( f.force_filter ) {
112 if ( this.templ.filterFields.findIndex(el => el.name === f.name) === -1 ) {
113 this.templ.filterFields.push(f);
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);
123 if ( !('members' in this.fieldGroups[idx]) ) {
124 this.fieldGroups[idx].members = [];
126 this.fieldGroups[idx].members.push(f);
132 this.allFields.sort( (a, b) => a.label.localeCompare(b.label) );
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;
144 return this.changeTypeDialog.open()
145 .subscribe(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;
153 this.rptType = this.oldRptType;
160 this._isDirty = true;
164 return this._isDirty;
168 return ( this.sourceClass !== null && this.name !== '' );
171 readyToSchedule = () => {
172 return ( this.readyToSave() && this.templ.displayFields.length > 0 );
176 if ( this.isDirty() ) {
177 return this.closeFormDialog.open();
183 loadTemplate(id: number) {
184 return this.srSvc.loadTemplate(id)
186 this.templ = new SRTemplate(idl);
187 this.name = this.templ.name;
188 this.rptType = this.templ.fmClass;
189 this.oldRptType = this.templ.fmClass;
193 saveTemplate = (scheduleNow) => {
194 this.templ.name = this.name;
196 this.srSvc.saveTemplate(this.templ, scheduleNow)
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
204 this.templateSavedString.current()
206 this.toast.success(str);
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...
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
222 this.templateSaveErrorString.current()
224 this.toast.danger(str + err);
225 console.error('Error saving template: %o', err);
231 this.router.navigate(['/staff/reporter/simple']);