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';
16 templateUrl: './trigger-edit.component.html'
19 export class EditEventDefinitionComponent implements OnInit {
24 testErr1: String = '';
25 testErr2: String = '';
26 testResult: String = '';
27 testDone: Boolean = false;
29 envDataSource: GridDataSource = new GridDataSource();
30 paramDataSource: GridDataSource = new GridDataSource();
32 editTab: 'def' | 'env' | 'param' | 'test' = 'def';
34 @ViewChild('paramDialog') paramDialog: FmRecordEditorComponent;
35 @ViewChild('envDialog') envDialog: FmRecordEditorComponent;
37 @ViewChild('envGrid') envGrid: GridComponent;
38 @ViewChild('paramGrid') paramGrid: GridComponent;
40 @ViewChild('updateSuccessString') updateSuccessString: StringComponent;
41 @ViewChild('updateFailedString') updateFailedString: StringComponent;
42 @ViewChild('cloneSuccessString') cloneSuccessString: StringComponent;
43 @ViewChild('cloneFailedString') cloneFailedString: StringComponent;
44 @ViewChild('deleteFailedString') deleteFailedString: StringComponent;
45 @ViewChild('deleteSuccessString') deleteSuccessString: StringComponent;
46 @ViewChild('createSuccessString') createSuccessString: StringComponent;
47 @ViewChild('createErrString') createErrString: StringComponent;
50 private idl: IdlService,
51 private pcrud: PcrudService,
52 private toast: ToastService,
53 private route: ActivatedRoute,
54 private net: NetService,
55 private auth: AuthService,
60 this.evtDefId = parseInt(this.route.snapshot.paramMap.get('id'), 10);
62 // get current event def name to display on the banner
63 this.pcrud.search('atevdef',
64 {id: this.evtDefId}, {}).toPromise().then(rec => {
65 this.evtDefName = rec.name();
68 this.envDataSource.getRows = (pager: Pager, sort: any[]) => {
69 return this.pcrud.search('atenv',
70 {event_def: this.evtDefId}, {});
73 this.paramDataSource.getRows = (pager: Pager, sort: any[]) => {
74 return this.pcrud.search('atevparam',
75 {event_def: this.evtDefId}, {});
79 onTabChange(event: NgbNavChangeEvent) {
80 this.editTab = event.nextId;
83 createNewEnv = () => {
84 this.createNewThing(this.envDialog, this.envGrid, 'atenv');
87 createNewParam = () => {
88 this.createNewThing(this.paramDialog, this.paramGrid, 'atevparam');
91 createNewThing = (currentDialog: any, currentGrid: any, idl: any) => {
92 currentDialog.mode = 'create';
93 currentDialog.recordId = null;
94 const newRecord = this.idl.create(idl);
95 newRecord.event_def(this.evtDefId);
96 currentDialog.mode = 'create';
97 currentDialog.record = newRecord;
98 currentDialog.open({size: 'lg'}).subscribe(
100 this.createSuccessString.current()
101 .then(str => this.toast.success(str));
102 currentGrid.reload();
105 if (!rejection.dismissed) {
106 this.createErrString.current()
107 .then(str => this.toast.danger(str));
113 deleteSelected = (idlThings: IdlObject[]) => {
115 if (idlThings[0].classname === 'atenv') {
116 currentGrid = this.envGrid;
118 currentGrid = this.paramGrid;
120 idlThings.forEach(idlThing => idlThing.isdeleted(true));
121 this.pcrud.autoApply(idlThings).subscribe(
123 console.debug('deleted: ' + val);
124 this.deleteSuccessString.current()
125 .then(str => this.toast.success(str));
126 currentGrid.reload();
129 this.deleteFailedString.current()
130 .then(str => this.toast.danger(str));
135 editSelected = (selectedRecords: IdlObject[]) => {
136 const editOneThing = (record: IdlObject) => {
137 if (!record) { return; }
138 this.showEditDialog(record).then(
139 () => editOneThing(selectedRecords.shift()));
141 editOneThing(selectedRecords.shift());
144 showEditDialog = (selectedRecord: IdlObject): Promise<any> => {
147 if (selectedRecord.classname === 'atenv') {
148 currentDialog = this.envDialog;
149 currentGrid = this.envGrid;
151 currentDialog = this.paramDialog;
152 currentGrid = this.paramGrid;
154 currentDialog.mode = 'update';
155 const clone = this.idl.clone(selectedRecord);
156 currentDialog.record = clone;
157 return new Promise((resolve, reject) => {
158 currentDialog.open({size: 'lg'}).subscribe(
160 this.updateSuccessString.current()
161 .then(str => this.toast.success(str));
162 currentGrid.reload();
166 this.updateFailedString.current()
167 .then(str => this.toast.danger(str));
174 runTest = (barcode) => {
178 this.clearTestResults();
180 'open-ils.circ', 'open-ils.circ.trigger_event_by_def_and_barcode.fire',
181 this.auth.token(), this.evtDefId, barcode
183 this.testDone = true;
185 this.testErr1 = 'Event: ' + res.ilsevent + ': ' + res.textcode + ' ->';
186 this.testErr2 = res.desc;
188 this.testResult = res.template_output().data();
193 clearTestResults = () => {
194 this.testDone = false;
197 this.testResult = '';