/
task-launch.component.ts
159 lines (146 loc) · 4.52 KB
/
task-launch.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { Component, OnInit, OnDestroy } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { TasksService } from '../tasks.service';
import { mergeMap, takeUntil } from 'rxjs/operators';
import { Observable, Subject } from 'rxjs';
import { TaskDefinition } from '../model/task-definition';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { BusyService } from '../../shared/services/busy.service';
import { NotificationService } from '../../shared/services/notification.service';
import { AppError } from '../../shared/model/error.model';
import { RoutingStateService } from '../../shared/services/routing-state.service';
import { TaskLaunchParams } from '../components/tasks.interface';
import { TaskLaunchValidator } from './task-launch.validator';
import { KvRichTextValidator } from '../../shared/components/kv-rich-text/kv-rich-text.validator';
/**
* Component that provides a launcher of task.
*
* @author Janne Valkealahti
* @author Gunnar Hillert
* @author Damien Vitrac
*/
@Component({
selector: 'app-task-launch',
templateUrl: './task-launch.component.html',
styleUrls: ['./styles.scss']
})
export class TaskLaunchComponent implements OnInit, OnDestroy {
/**
* Busy Subscriptions
*/
private ngUnsubscribe$: Subject<any> = new Subject();
/**
* Observable of Task Definition
*/
taskDefinition$: Observable<TaskDefinition>;
/**
* Form
*/
form: FormGroup;
/**
* Validators args / props component
*/
kvValidators = {
args: {
key: [Validators.required],
value: []
},
props: {
key: [Validators.required, TaskLaunchValidator.key],
value: []
}
};
/**
* Form Subbmitted
*/
submitted = false;
/**
* Constructor
*
* @param {TasksService} tasksService
* @param {NotificationService} notificationService
* @param {BusyService} busyService
* @param {RoutingStateService} routingStateService
* @param {ActivatedRoute} route
* @param {Router} router
*/
constructor(private tasksService: TasksService,
private notificationService: NotificationService,
private busyService: BusyService,
private routingStateService: RoutingStateService,
private route: ActivatedRoute,
private router: Router) {
}
/**
* Initialize
*/
ngOnInit() {
this.form = new FormGroup({
args: new FormControl('', KvRichTextValidator.validateKvRichText(this.kvValidators.args)),
props: new FormControl('', KvRichTextValidator.validateKvRichText(this.kvValidators.props))
});
this.taskDefinition$ = this.route.params
.pipe(mergeMap(
val => this.tasksService.getDefinition(val.id)
));
}
/**
* Destroy
*
* Will cleanup any {@link Subscription}s to prevent
* memory leaks.
*/
ngOnDestroy() {
this.ngUnsubscribe$.next();
this.ngUnsubscribe$.complete();
}
/**
* Prepare params
* @param {string} name
* @param {Array<string>} taskArguments
* @param {Array<string>} taskProperties
* @returns {TaskLaunchParams}
*/
prepareParams(name: string, taskArguments: Array<string>, taskProperties: Array<string>): TaskLaunchParams {
return {
name: name,
args: taskArguments.filter((a) => a !== '').join(' '),
props: taskProperties.filter((a) => a !== '').join(', ')
};
}
/**
* Launch the task
*
* @param {string} name
*/
submit(name: string) {
this.submitted = true;
if (!this.form.valid) {
this.notificationService.error('Some field(s) are invalid.');
} else {
const taskArguments = this.form.get('args').value.toString().split('\n');
const taskProperties = this.form.get('props').value.toString().split('\n');
const busy = this.tasksService.launchDefinition(this.prepareParams(name, taskArguments, taskProperties))
.pipe(takeUntil(this.ngUnsubscribe$))
.subscribe(
data => {
this.notificationService.success('Successfully launched task "' + name + '"');
this.router.navigate(['/tasks/definitions']);
},
error => {
this.notificationService.error(AppError.is(error) ? error.getMessage() : error);
}
);
this.busyService.addSubscription(busy);
}
}
/**
* Back action
* Navigate to the previous URL or /tasks/definitions
*
* @param {TaskDefinition} taskDefinition
*/
cancel(taskDefinition: TaskDefinition) {
this.routingStateService.back(`/tasks/definitions/${taskDefinition.name}`);
}
}