-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathmodalFieldComponent.ts
More file actions
204 lines (175 loc) · 7.43 KB
/
Copy pathmodalFieldComponent.ts
File metadata and controls
204 lines (175 loc) · 7.43 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import { AbstractModal, ModalController } from '../abstractModal';
/**
* @memberof miqStaticAssets
* @ngdoc component
* @name dialogEditorModalField
* @description
* Component contains templates for the modal for editing dialog editors
* field (group) details
* @example
* <dialog-editor-modal-field></dialog-editor-modal-field>
*/
export default class ModalField extends AbstractModal {
public template = require('./field.html');
public controller = ModalFieldController;
}
class ModalFieldController extends ModalController {
public treeOptions: any;
public modalData: any;
public validation: any;
public searchQuery: string = '';
public $onInit() {
const emsWorkflowsEnabled = this.treeOptions.emsWorkflowsEnabled === 'true' ? true : false;
/** Function to load the selected workflow if configuration_script_id is available. */
if (emsWorkflowsEnabled && this.modalData.resource_action && this.modalData.resource_action.configuration_script_id) {
this.loadWorkflow(this.modalData.resource_action.configuration_script_id);
};
this.treeOptions = {
...this.treeOptions,
show: false,
includeDomain: false,
data: null,
automationType: null,
automationTypes: {
automate: 'embedded_automate',
workflow: 'embedded_workflow',
},
emsWorkflowsEnabled: emsWorkflowsEnabled,
searchQuery: '',
filteredWorkflows: [],
/** Function to reset the modalData while changin the Automation Type. */
onAutomationTypeChange: () => {
this.treeOptions.automationType = this.modalData.automation_type;
},
/** Function to display the automation_type select box
* 'Embedded Automate' will be displayed, by default.
* When the workflows are enabled, and automation_type === embedded_automate, 'Embedded Automate' select box will be displayed.
* Else, 'Embedded Workflow' will be selected.
*/
displayAutomationType: () => {
let displayAutomate = true;
if(emsWorkflowsEnabled) {
displayAutomate = this.modalData.automation_type === this.treeOptions.automationTypes.automate;
}
return {automate: displayAutomate, workflow: !displayAutomate};
},
/** Function to open the modal box and load the automate tree. */
toggle: () => {
this.treeOptions.show = ! this.treeOptions.show;
this.treeOptions.automationType = this.treeOptions.automationTypes.automate;
if (this.treeOptions.show) {
const fqname = this.showFullyQualifiedName(this.modalData.resource_action) || null;
this.treeOptions.load(fqname).then((data) => {
this.treeOptions.data = data;
this.treeOptions.selected = {fqname: '/' + fqname};
});
}
},
/** Function to open the modal box and load the workflows list. */
toggleWorkflows: () => {
this.treeOptions.show = ! this.treeOptions.show;
this.treeOptions.automationType = this.treeOptions.automationTypes.workflow;
if (this.treeOptions.show) {
this.treeOptions.loadAvailableWorkflows().then((data) => {
this.treeOptions.data = data.resources.filter((item: any) => item.payload);
const workflow = this.treeOptions.data.find((item) => item.id === this.modalData.resource_action.configuration_script_id);
this.treeOptions.selected = workflow ? workflow.name : null;
this.treeOptions.filteredWorkflows = this.treeOptions.data;
});
}
},
/** Function to handle the onclick event of an item in tree. */
onSelect: (node) => {
this.treeSelectorSelect(node, this.modalData);
},
/** Function to filter the API results with Repository or Workflow Name. */
filterResults: () => {
if ( this.treeOptions.searchQuery.length === 0) {
this.treeOptions.filteredWorkflows = this.treeOptions.data;
} else {
const query = this.treeOptions.searchQuery.toLowerCase().trim();
this.treeOptions.filteredWorkflows = this.treeOptions.data.filter((workflow) =>
(workflow.configuration_script_source && workflow.configuration_script_source.name
? workflow.configuration_script_source.name.toLowerCase().includes(query)
: false) ||
(workflow.name
? workflow.name.toLowerCase().includes(query)
: false)
);
}
},
/** Function to clear the search text and reset the list. */
clearSearchQuery: () => {
this.treeOptions.searchQuery = '';
this.treeOptions.filteredWorkflows = this.treeOptions.data;
}
};
}
public showFullyQualifiedName(resourceAction) {
if (!resourceAction) {
return '';
}
const actionKeys = ['ae_namespace', 'ae_class', 'ae_instance'];
const keysPresent = actionKeys.every((item) => resourceAction.hasOwnProperty(item));
if (keysPresent && resourceAction.ae_namespace && resourceAction.ae_class && resourceAction.ae_instance) {
return `${resourceAction.ae_namespace}/${resourceAction.ae_class}/${resourceAction.ae_instance}`;
} else {
return '';
}
}
/** Function to extract the values needed for embedded_automate during onclick event of an item from the tree */
public onEmbeddedAutomateSelect(node, elementData) {
const fqname = node.fqname.split('/');
if (this.treeOptions.includeDomain === false) {
fqname.splice(1, 1);
}
if (elementData.resource_action) {
elementData.resource_action = {
...elementData.resource_action,
ae_instance: fqname.pop(),
ae_class: fqname.pop(),
ae_namespace: fqname.filter(String).join('/'),
};
}
}
/** Function to extract the values needed for embedded_workflow during onclick event of an item from the list */
public onEmbeddedWorkflowsSelect(workflow, elementData) {
if (elementData.resource_action) {
elementData.resource_action = {
...elementData.resource_action,
configuration_script_id: workflow.id,
workflow_name: workflow.name,
};
}
}
/** Function to extract the values needed for entry points during onclick event of an item from the tree or list */
public treeSelectorSelect(node, elementData) {
if (this.treeOptions.automationType === this.treeOptions.automationTypes.automate) {
this.onEmbeddedAutomateSelect(node, elementData);
} else if (this.treeOptions.automationType === this.treeOptions.automationTypes.workflow) {
this.onEmbeddedWorkflowsSelect(node, elementData);
}
this.treeOptions.show = false;
}
/** Function to get notification messages for options modal box if any. */
public modalNotification() {
const notice = { error: false, message: undefined };
if (!this.modalFieldIsValid() && this.validation.invalid && this.validation.invalid.message) {
notice.message = this.validation.invalid.message;
notice.error = true;
} else if (this.modalData.validationMessage) {
notice.message = this.modalData.validationMessage;
notice.error = true;
}
return notice;
}
public modalFieldIsValid() {
return this.validation.validateField(this.modalData);
}
/** Function to load a selected workflow. */
public loadWorkflow(id: number) {
this.treeOptions.loadWorkflow(id).then(({data, status}) => {
this.modalData.resource_action.workflow_name = status ? data.name : '';
});
}
}