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 {Router} from '@angular/router';
6 import {IdlService, IdlObject} from '@eg/core/idl.service';
7 import {PcrudService} from '@eg/core/pcrud.service';
8 import {FmRecordEditorComponent} from '@eg/share/fm-editor/fm-editor.component';
9 import {StringComponent} from '@eg/share/string/string.component';
10 import {ToastService} from '@eg/share/toast/toast.service';
11 import {NgbNav, NgbNavChangeEvent} from '@ng-bootstrap/ng-bootstrap';
14 templateUrl: './triggers.component.html'
17 export class TriggersComponent implements OnInit {
19 eventsDataSource: GridDataSource = new GridDataSource();
20 hooksDataSource: GridDataSource = new GridDataSource();
21 reactorsDataSource: GridDataSource = new GridDataSource();
22 validatorsDataSource: GridDataSource = new GridDataSource();
23 triggerTab: 'eventDefinitions' | 'hooks' | 'reactors' | 'validators' = 'eventDefinitions';
26 @ViewChild('eventDialog', {static: false}) eventDialog: FmRecordEditorComponent;
27 @ViewChild('hookDialog', {static: false}) hookDialog: FmRecordEditorComponent;
28 @ViewChild('reactorDialog', {static: false}) reactorDialog: FmRecordEditorComponent;
29 @ViewChild('validatorDialog', {static: false}) validatorDialog: FmRecordEditorComponent;
31 @ViewChild('eventsGrid', {static: false}) eventsGrid: GridComponent;
32 @ViewChild('hooksGrid', {static: false}) hooksGrid: GridComponent;
33 @ViewChild('reactorsGrid', {static: false}) reactorsGrid: GridComponent;
34 @ViewChild('validatorsGrid', {static: false}) validatorsGrid: GridComponent;
36 @ViewChild('updateSuccessString', {static: false}) updateSuccessString: StringComponent;
37 @ViewChild('updateFailedString', {static: false}) updateFailedString: StringComponent;
38 @ViewChild('cloneSuccessString', {static: false}) cloneSuccessString: StringComponent;
39 @ViewChild('cloneFailedString', {static: false}) cloneFailedString: StringComponent;
40 @ViewChild('deleteFailedString', {static: false}) deleteFailedString: StringComponent;
41 @ViewChild('deleteSuccessString', {static: false}) deleteSuccessString: StringComponent;
42 @ViewChild('createSuccessString', {static: false}) createSuccessString: StringComponent;
43 @ViewChild('createErrString', {static: false}) createErrString: StringComponent;
46 private idl: IdlService,
47 private pcrud: PcrudService,
48 private toast: ToastService,
49 private router: Router,
54 this.eventsDataSource.getRows = (pager: Pager, sort: any[]) => {
55 const orderEventsBy: any = {atevdef: 'name'};
57 orderEventsBy.atevdef = sort[0].name + ' ' + sort[0].dir;
59 return this.getData('atevdef', orderEventsBy, this.eventsDataSource, pager);
62 this.hooksDataSource.getRows = (pager: Pager, sort: any[]) => {
63 const orderHooksBy: any = {ath: 'key'};
65 orderHooksBy.ath = sort[0].name + ' ' + sort[0].dir;
67 return this.getData('ath', orderHooksBy, this.hooksDataSource, pager);
70 this.reactorsDataSource.getRows = (pager: Pager, sort: any[]) => {
71 const orderReactorsBy: any = {atreact: 'module'};
73 orderReactorsBy.atreact = sort[0].name + ' ' + sort[0].dir;
75 return this.getData('atreact', orderReactorsBy, this.reactorsDataSource, pager);
78 this.validatorsDataSource.getRows = (pager: Pager, sort: any[]) => {
79 const orderValidatorsBy: any = {atval: 'module'};
81 orderValidatorsBy.atval = sort[0].name + ' ' + sort[0].dir;
83 return this.getData('atval', orderValidatorsBy, this.validatorsDataSource, pager);
87 getData(idlString: any, currentOrderBy: any, currentDataSource: any, pager: Pager) {
88 const base: Object = {};
89 base[this.idl.classes[idlString].pkey] = {'!=' : null};
90 const query: any = new Array();
92 Object.keys(currentDataSource.filters).forEach(key => {
93 Object.keys(currentDataSource.filters[key]).forEach(key2 => {
94 query.push(currentDataSource.filters[key][key2]);
97 return this.pcrud.search(idlString,
101 order_by: currentOrderBy
105 onTabChange(event: NgbNavChangeEvent) {
106 this.triggerTab = event.nextId;
109 createNewEvent = () => {
110 this.createNewThing(this.eventDialog, this.eventsGrid);
113 createNewHook = () => {
114 this.createNewThing(this.hookDialog, this.hooksGrid);
117 createNewReactor = () => {
118 this.createNewThing(this.reactorDialog, this.reactorsGrid);
121 createNewValidator = () => {
122 this.createNewThing(this.validatorDialog, this.validatorsGrid);
125 createNewThing = (currentDialog: any, currentGrid: any) => {
126 currentDialog.mode = 'create';
127 currentDialog.recordId = null;
128 currentDialog.record = null;
129 currentDialog.open({size: 'lg'}).subscribe(
131 this.createSuccessString.current()
132 .then(str => this.toast.success(str));
133 currentGrid.reload();
136 if (!rejection.dismissed) {
137 this.createErrString.current()
138 .then(str => this.toast.danger(str));
144 editSelected = (selectedRecords: IdlObject[]) => {
145 if (this.triggerTab === 'eventDefinitions') {
146 this.editEventDefinition(selectedRecords);
149 const editOneThing = (record: IdlObject) => {
150 if (!record) { return; }
151 this.showEditDialog(record).then(
152 () => editOneThing(selectedRecords.shift()));
154 editOneThing(selectedRecords.shift());
157 editEventDefinition = (selectedRecords: IdlObject[]) => {
158 const id = selectedRecords[0].id();
159 this.router.navigate(['/staff/admin/local/action_trigger/event_definition/' + id]);
162 lookUpIdl (idl: string) {
167 currentDialog = this.eventDialog;
168 currentGrid = this.eventsGrid;
171 currentDialog = this.hookDialog;
172 currentGrid = this.hooksGrid;
175 currentDialog = this.reactorDialog;
176 currentGrid = this.reactorsGrid;
179 currentDialog = this.validatorDialog;
180 currentGrid = this.validatorsGrid;
183 console.debug('Unknown class name');
185 return {currentDialog: currentDialog, currentGrid: currentGrid};
188 showEditDialog = (selectedRecord: IdlObject): Promise<any> => {
189 const idl = selectedRecord.classname;
190 const lookupResults = this.lookUpIdl(idl);
191 const currentDialog = lookupResults.currentDialog;
192 const currentGrid = lookupResults.currentGrid;
193 currentDialog.mode = 'update';
194 const clone = this.idl.clone(selectedRecord);
195 currentDialog.record = clone;
196 return new Promise((resolve, reject) => {
197 currentDialog.open({size: 'lg'}).subscribe(
199 this.updateSuccessString.current()
200 .then(str => this.toast.success(str));
201 currentGrid.reload();
205 this.updateFailedString.current()
206 .then(str => this.toast.danger(str));
213 deleteSelected = (idlThings: IdlObject[]) => {
214 const idl = idlThings[0].classname;
215 const currentGrid = this.lookUpIdl(idl).currentGrid;
216 idlThings.forEach(idlThing => idlThing.isdeleted(true));
217 this.pcrud.autoApply(idlThings).subscribe(
219 console.debug('deleted: ' + val);
220 this.deleteSuccessString.current()
221 .then(str => this.toast.success(str));
222 currentGrid.reload();
225 this.deleteFailedString.current()
226 .then(str => this.toast.danger(str));
231 cloneSelected = (idlThings: IdlObject[]) => {
232 const clone = this.idl.clone(idlThings[0]);
234 this.eventDialog.mode = 'create';
235 this.eventDialog.recordId = null;
236 this.eventDialog.record = clone;
237 this.eventDialog.open({size: 'lg'}).subscribe(
239 this.cloneSuccessString.current()
240 .then(str => this.toast.success(str));
241 this.eventsGrid.reload();
244 if (!rejection.dismissed) {
245 this.cloneFailedString.current()
246 .then(str => this.toast.danger(str));