1 import {Pager} from '@eg/share/util/pager';
2 import {Component, OnInit, ViewChild} from '@angular/core';
3 import {GridComponent} from '@eg/share/grid/grid.component';
4 import {GridDataSource} from '@eg/share/grid/grid';
5 import {ActivatedRoute} from '@angular/router';
6 import {IdlService, IdlObject} from '@eg/core/idl.service';
7 import {PcrudService} from '@eg/core/pcrud.service';
8 import {NetService} from '@eg/core/net.service';
9 import {AuthService} from '@eg/core/auth.service';
10 import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
11 import {StringComponent} from '@eg/share/string/string.component';
12 import {ToastService} from '@eg/share/toast/toast.service';
13 import {NgbNav, NgbNavChangeEvent} from '@ng-bootstrap/ng-bootstrap';
14 import {Router} from '@angular/router';
17 templateUrl: './trigger-edit.component.html'
20 export class EditEventDefinitionComponent implements OnInit {
25 testErr1: String = '';
26 testErr2: String = '';
27 testResult: String = '';
28 testDone: Boolean = false;
30 envDataSource: GridDataSource = new GridDataSource();
31 paramDataSource: GridDataSource = new GridDataSource();
33 editTab: 'def' | 'env' | 'param' | 'test' = 'def';
35 @ViewChild('paramDialog') paramDialog: FmRecordEditorComponent;
36 @ViewChild('envDialog') envDialog: FmRecordEditorComponent;
38 @ViewChild('envGrid') envGrid: GridComponent;
39 @ViewChild('paramGrid') paramGrid: GridComponent;
41 @ViewChild('updateSuccessString') updateSuccessString: StringComponent;
42 @ViewChild('updateFailedString') updateFailedString: StringComponent;
43 @ViewChild('cloneSuccessString') cloneSuccessString: StringComponent;
44 @ViewChild('cloneFailedString') cloneFailedString: StringComponent;
45 @ViewChild('deleteFailedString') deleteFailedString: StringComponent;
46 @ViewChild('deleteSuccessString') deleteSuccessString: StringComponent;
47 @ViewChild('createSuccessString') createSuccessString: StringComponent;
48 @ViewChild('createErrString') createErrString: StringComponent;
49 @ViewChild('eventDuringTestString') eventDuringTestString: StringComponent;
50 @ViewChild('errorDuringTestString') errorDuringTestString: StringComponent;
53 private router: Router,
54 private idl: IdlService,
55 private pcrud: PcrudService,
56 private toast: ToastService,
57 private route: ActivatedRoute,
58 private net: NetService,
59 private auth: AuthService,
64 this.evtDefId = parseInt(this.route.snapshot.paramMap.get('id'), 10);
66 // get current event def name to display on the banner
67 this.pcrud.search('atevdef',
68 {id: this.evtDefId}, {}).toPromise().then(rec => {
69 this.evtDefName = rec.name();
72 this.envDataSource.getRows = (pager: Pager, sort: any[]) => {
73 return this.pcrud.search('atenv',
74 {event_def: this.evtDefId}, {});
77 this.paramDataSource.getRows = (pager: Pager, sort: any[]) => {
78 return this.pcrud.search('atevparam',
79 {event_def: this.evtDefId}, {});
83 onTabChange(event: NgbNavChangeEvent) {
84 this.editTab = event.nextId;
87 createNewEnv = () => {
88 this.createNewThing(this.envDialog, this.envGrid, 'atenv');
91 createNewParam = () => {
92 this.createNewThing(this.paramDialog, this.paramGrid, 'atevparam');
95 createNewThing = (currentDialog: any, currentGrid: any, idl: any) => {
96 currentDialog.mode = 'create';
97 currentDialog.recordId = null;
98 const newRecord = this.idl.create(idl);
99 newRecord.event_def(this.evtDefId);
100 currentDialog.mode = 'create';
101 currentDialog.record = newRecord;
102 currentDialog.open({size: 'lg'}).subscribe(
104 this.createSuccessString.current()
105 .then(str => this.toast.success(str));
106 currentGrid.reload();
109 if (!rejection.dismissed) {
110 this.createErrString.current()
111 .then(str => this.toast.danger(str));
117 deleteSelected = (idlThings: IdlObject[]) => {
119 if (idlThings[0].classname === 'atenv') {
120 currentGrid = this.envGrid;
122 currentGrid = this.paramGrid;
124 idlThings.forEach(idlThing => idlThing.isdeleted(true));
126 this.pcrud.autoApply(idlThings).subscribe(
128 console.debug('deleted: ' + val);
129 this.deleteSuccessString.current()
130 .then(str => this.toast.success(str));
134 this.deleteFailedString.current()
135 .then(str => this.toast.danger(str));
139 currentGrid.reload();
145 editSelected = (selectedRecords: IdlObject[]) => {
146 const editOneThing = (record: IdlObject) => {
147 if (!record) { return; }
148 this.showEditDialog(record).then(
149 () => editOneThing(selectedRecords.shift()));
151 editOneThing(selectedRecords.shift());
154 showEditDialog = (selectedRecord: IdlObject): Promise<any> => {
157 if (selectedRecord.classname === 'atenv') {
158 currentDialog = this.envDialog;
159 currentGrid = this.envGrid;
161 currentDialog = this.paramDialog;
162 currentGrid = this.paramGrid;
164 currentDialog.mode = 'update';
165 const clone = this.idl.clone(selectedRecord);
166 currentDialog.record = clone;
167 return new Promise((resolve, reject) => {
168 currentDialog.open({size: 'lg'}).subscribe(
170 this.updateSuccessString.current()
171 .then(str => this.toast.success(str));
172 currentGrid.reload();
176 this.updateFailedString.current()
177 .then(str => this.toast.danger(str));
184 runTest = (barcode) => {
188 this.clearTestResults();
190 'open-ils.circ', 'open-ils.circ.trigger_event_by_def_and_barcode.fire',
191 this.auth.token(), this.evtDefId, barcode
193 this.testDone = true;
195 this.eventDuringTestString.current({ ilsevent: res.ilsevent, textcode : res.textcode})
196 .then(str => this.testErr1 = str);
197 this.testErr2 = res.desc;
199 this.testResult = res.template_output().data();
202 this.testDone = true;
203 this.errorDuringTestString.current().then(str => this.testErr1 = str);
208 clearTestResults = () => {
209 this.testDone = false;
212 this.testResult = '';
215 this.router.navigate(['/staff/admin/local/action_trigger/event_definition/']);