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 {ConfirmDialogComponent} from '@eg/share/dialog/confirm.component';
10 import {StringComponent} from '@eg/share/string/string.component';
11 import {ToastService} from '@eg/share/toast/toast.service';
12 import {NgbNav, NgbNavChangeEvent} from '@ng-bootstrap/ng-bootstrap';
15 templateUrl: './triggers.component.html'
18 export class TriggersComponent implements OnInit {
20 eventsDataSource: GridDataSource = new GridDataSource();
21 hooksDataSource: GridDataSource = new GridDataSource();
22 reactorsDataSource: GridDataSource = new GridDataSource();
23 validatorsDataSource: GridDataSource = new GridDataSource();
24 triggerTab: 'eventDefinitions' | 'hooks' | 'reactors' | 'validators' = 'eventDefinitions';
27 @ViewChild('eventDialog', {static: false}) eventDialog: FmRecordEditorComponent;
28 @ViewChild('hookDialog', {static: false}) hookDialog: FmRecordEditorComponent;
29 @ViewChild('reactorDialog', {static: false}) reactorDialog: FmRecordEditorComponent;
30 @ViewChild('validatorDialog', {static: false}) validatorDialog: FmRecordEditorComponent;
32 @ViewChild('confirmDialog', {static: false}) private confirmDialog: ConfirmDialogComponent;
34 @ViewChild('eventsGrid', {static: false}) eventsGrid: GridComponent;
35 @ViewChild('hooksGrid', {static: false}) hooksGrid: GridComponent;
36 @ViewChild('reactorsGrid', {static: false}) reactorsGrid: GridComponent;
37 @ViewChild('validatorsGrid', {static: false}) validatorsGrid: GridComponent;
39 @ViewChild('updateSuccessString', {static: false}) updateSuccessString: StringComponent;
40 @ViewChild('updateFailedString', {static: false}) updateFailedString: StringComponent;
41 @ViewChild('cloneSuccessString', {static: false}) cloneSuccessString: StringComponent;
42 @ViewChild('cloneFailedString', {static: false}) cloneFailedString: StringComponent;
43 @ViewChild('deleteFailedString', {static: false}) deleteFailedString: StringComponent;
44 @ViewChild('deleteSuccessString', {static: false}) deleteSuccessString: StringComponent;
45 @ViewChild('createSuccessString', {static: false}) createSuccessString: StringComponent;
46 @ViewChild('createErrString', {static: false}) createErrString: StringComponent;
49 private idl: IdlService,
50 private pcrud: PcrudService,
51 private toast: ToastService,
52 private router: Router,
57 this.eventsDataSource.getRows = (pager: Pager, sort: any[]) => {
58 const orderEventsBy: any = {atevdef: 'name'};
60 orderEventsBy.atevdef = sort[0].name + ' ' + sort[0].dir;
62 return this.getData('atevdef', orderEventsBy, this.eventsDataSource, pager);
65 this.hooksDataSource.getRows = (pager: Pager, sort: any[]) => {
66 const orderHooksBy: any = {ath: 'key'};
68 orderHooksBy.ath = sort[0].name + ' ' + sort[0].dir;
70 return this.getData('ath', orderHooksBy, this.hooksDataSource, pager);
73 this.reactorsDataSource.getRows = (pager: Pager, sort: any[]) => {
74 const orderReactorsBy: any = {atreact: 'module'};
76 orderReactorsBy.atreact = sort[0].name + ' ' + sort[0].dir;
78 return this.getData('atreact', orderReactorsBy, this.reactorsDataSource, pager);
81 this.validatorsDataSource.getRows = (pager: Pager, sort: any[]) => {
82 const orderValidatorsBy: any = {atval: 'module'};
84 orderValidatorsBy.atval = sort[0].name + ' ' + sort[0].dir;
86 return this.getData('atval', orderValidatorsBy, this.validatorsDataSource, pager);
90 getData(idlString: any, currentOrderBy: any, currentDataSource: any, pager: Pager) {
91 const base: Object = {};
92 base[this.idl.classes[idlString].pkey] = {'!=' : null};
93 const query: any = new Array();
95 Object.keys(currentDataSource.filters).forEach(key => {
96 Object.keys(currentDataSource.filters[key]).forEach(key2 => {
97 query.push(currentDataSource.filters[key][key2]);
100 return this.pcrud.search(idlString,
102 offset: pager.offset,
104 order_by: currentOrderBy
108 onTabChange(event: NgbNavChangeEvent) {
109 this.triggerTab = event.nextId;
112 createNewEvent = () => {
113 this.createNewThing(this.eventDialog, this.eventsGrid);
116 createNewHook = () => {
117 this.createNewThing(this.hookDialog, this.hooksGrid);
120 createNewReactor = () => {
121 this.createNewThing(this.reactorDialog, this.reactorsGrid);
124 createNewValidator = () => {
125 this.createNewThing(this.validatorDialog, this.validatorsGrid);
128 createNewThing = (currentDialog: any, currentGrid: any) => {
129 currentDialog.mode = 'create';
130 currentDialog.recordId = null;
131 currentDialog.record = null;
132 currentDialog.open({size: 'lg'}).subscribe(
134 this.createSuccessString.current()
135 .then(str => this.toast.success(str));
136 currentGrid.reload();
139 if (!rejection.dismissed) {
140 this.createErrString.current()
141 .then(str => this.toast.danger(str));
147 editSelected = (selectedRecords: IdlObject[]) => {
148 if (this.triggerTab === 'eventDefinitions') {
149 this.editEventDefinition(selectedRecords);
152 const editOneThing = (record: IdlObject) => {
153 if (!record) { return; }
154 this.showEditDialog(record).then(
155 () => editOneThing(selectedRecords.shift()));
157 editOneThing(selectedRecords.shift());
160 editEventDefinition = (selectedRecords: IdlObject[]) => {
161 const id = selectedRecords[0].id();
162 this.router.navigate(['/staff/admin/local/action_trigger/event_definition/' + id]);
165 lookUpIdl (idl: string) {
170 currentDialog = this.eventDialog;
171 currentGrid = this.eventsGrid;
174 currentDialog = this.hookDialog;
175 currentGrid = this.hooksGrid;
178 currentDialog = this.reactorDialog;
179 currentGrid = this.reactorsGrid;
182 currentDialog = this.validatorDialog;
183 currentGrid = this.validatorsGrid;
186 console.debug('Unknown class name');
188 return {currentDialog: currentDialog, currentGrid: currentGrid};
191 showEditDialog = (selectedRecord: IdlObject): Promise<any> => {
192 const idl = selectedRecord.classname;
193 const lookupResults = this.lookUpIdl(idl);
194 const currentDialog = lookupResults.currentDialog;
195 const currentGrid = lookupResults.currentGrid;
196 currentDialog.mode = 'update';
197 const clone = this.idl.clone(selectedRecord);
198 currentDialog.record = clone;
199 return new Promise((resolve, reject) => {
200 currentDialog.open({size: 'lg'}).subscribe(
202 this.updateSuccessString.current()
203 .then(str => this.toast.success(str));
204 currentGrid.reload();
208 this.updateFailedString.current()
209 .then(str => this.toast.danger(str));
216 deleteSelected = (idlThings: IdlObject[]) => {
217 const idl = idlThings[0].classname;
218 const currentGrid = this.lookUpIdl(idl).currentGrid;
219 idlThings.forEach(idlThing => idlThing.isdeleted(true));
220 this.pcrud.autoApply(idlThings).subscribe(
222 console.debug('deleted: ' + val);
223 this.deleteSuccessString.current()
224 .then(str => this.toast.success(str));
225 currentGrid.reload();
228 this.deleteFailedString.current()
229 .then(str => this.toast.danger(str));
234 cloneSelected = (selectedRecords: IdlObject[]) => {
235 const clone = this.idl.clone(selectedRecords[0]);
236 // look for existing environments
237 this.pcrud.search('atenv', {event_def: selectedRecords[0].id()}, {}, {atomic: true})
238 .toPromise().then(envs => {
240 // if environments found, ask user if they want to clone them
241 this.confirmDialog.open().toPromise().then(ok => {
243 this.doClone(clone, envs);
245 this.doClone(clone, []);
249 this.doClone(clone, []);
254 doClone(eventDef, env_list) {
256 eventDef.owner(null);
257 this.eventDialog.mode = 'create';
258 this.eventDialog.recordId = null;
259 this.eventDialog.record = eventDef;
260 this.eventDialog.open({size: 'lg'}).subscribe(
262 this.cloneSuccessString.current()
263 .then(str => this.toast.success(str));
264 this.eventsGrid.reload();
265 // clone environments also if user previously confirmed
266 if (env_list.length) {
267 this.cloneEnvs(response.id(), env_list);
271 if (!rejection.dismissed) {
272 this.cloneFailedString.current()
273 .then(str => this.toast.danger(str));
279 cloneEnvs(cloneId, env_list) {
280 env_list.forEach(env => {
281 env.event_def(cloneId);
284 this.pcrud.create(env_list).toPromise().then(
287 this.cloneFailedString.current()
288 .then(str => this.toast.danger(str));