1 import {Component, OnInit} from '@angular/core';
2 import {InputActionAttribute, OutputActionAttribute} from './models/InputActionAttribute';
3 import {DesignerStore} from '../designer.store';
4 import {DesignerDashboardState} from '../model/designer.dashboard.state';
5 import {Action} from './models/Action';
6 import {FunctionsStore} from '../functions.store';
7 import {FunctionsState} from '../model/functions.state';
10 selector: 'app-action-attributes',
11 templateUrl: './action-attributes.component.html',
12 styleUrls: ['./action-attributes.component.css']
14 export class ActionAttributesComponent implements OnInit {
20 actionAttributesSideBar: boolean;
21 inputActionAttribute = new InputActionAttribute();
22 outputActionAttribute = new OutputActionAttribute();
23 isInputOtherType: boolean;
24 isOutputOtherType: boolean;
28 designerState: DesignerDashboardState;
29 isFunctionAttributeActive = false;
30 functions: FunctionsState;
32 suggestedInputs: string[] = [];
33 suggestedOutputs: string[] = [];
35 tempInputs: string[] = [];
36 tempOutputs: string[] = [];
37 currentInterfaceName: string;
38 functionAndAttributesInput: Map<string, string[]> = new Map<string, string[]>();
39 private currentTargetFunctionName: any;
40 private functionAndAttributesOutput: Map<string, string[]> = new Map<string, string[]>();
41 suggestedAttributes: string[] = [];
42 selectedFunctionName = '';
43 selectedAttributeName = '';
45 constructor(private designerStore: DesignerStore, private functionsStore: FunctionsStore) {
50 this.designerStore.state$.subscribe(designerState => {
51 this.designerState = designerState;
52 if (this.designerState && this.designerState.actionName) {
53 this.actionName = this.designerState.actionName;
54 console.log(this.actionName);
55 const action = this.designerState.template.workflows[this.actionName] as Action;
57 const steps = Object.keys(action.steps);
58 this.isFunctionAttributeActive = steps && steps.length > 0;
60 this.suggestedOutputs = [];
61 this.suggestedInputs = [];
66 const namesOfInput = Object.keys(action.inputs);
67 this.inputs = this.extractFields(namesOfInput, action.inputs);
71 const namesOfOutput = Object.keys(action.outputs);
72 this.outputs = this.extractFields(namesOfOutput, action.outputs);
77 this.functionsStore.state$.subscribe(functions => {
78 this.functions = functions;
83 private extractFields(namesOfOutput: string[], container: {}) {
85 for (const nameOutput of namesOfOutput) {
86 const fieldAttribute = new OutputActionAttribute();
87 fieldAttribute.name = nameOutput;
88 fieldAttribute.description = container[nameOutput].description;
89 fieldAttribute.required = container[nameOutput].required;
90 fieldAttribute.type = container[nameOutput].type;
91 const insertedOutputActionAttribute = Object.assign({}, fieldAttribute);
92 fields.push(insertedOutputActionAttribute);
97 addInput(input: InputActionAttribute) {
98 if (input && input.type && input.name) {
99 const insertedInputActionAttribute = Object.assign({}, input);
100 this.newInputs.push(insertedInputActionAttribute);
104 addOutput(output: OutputActionAttribute) {
106 if (output && output.type && output.name) {
107 const insertedOutputActionAttribute = Object.assign({}, output);
108 this.newOutputs.push(insertedOutputActionAttribute);
112 setInputType(type: string) {
113 this.inputActionAttribute.type = type;
114 this.isInputOtherType = this.checkIfTypeIsOther(type);
117 setInputRequired(isRequired) {
118 this.inputActionAttribute.required = isRequired;
121 setOutputRequired(isRequired) {
122 this.outputActionAttribute.required = isRequired;
125 setOutputType(type: string) {
126 this.outputActionAttribute.type = type;
127 this.isOutputOtherType = this.checkIfTypeIsOther(type);
130 checkIfTypeIsOther(type) {
131 return type.includes('Other');
135 this.addInput(this.inputActionAttribute);
136 if (this.selectedFunctionName && this.selectedAttributeName) {
137 this.outputActionAttribute.value =
138 '["' + this.selectedFunctionName + '","' + this.selectedAttributeName + '"]';
140 this.addOutput(this.outputActionAttribute);
141 this.clearFormInputs();
142 this.storeOutputs(this.newOutputs);
143 this.storeInputs((this.newInputs));
144 this.newInputs.forEach(input => {
145 this.inputs.push(input);
148 this.newOutputs.forEach(output => {
149 this.outputs.push(output);
153 private clearFormInputs() {
154 this.inputActionAttribute = new InputActionAttribute();
155 this.outputActionAttribute = new OutputActionAttribute();
156 this.outputOtherType = '';
157 this.inputOtherType = '';
160 private storeInputs(InputActionAttributes: InputActionAttribute[]) {
163 InputActionAttributes.forEach(input => {
164 inputs += this.appendAttributes(input);
167 this.writeAttribute(inputs, 'inputs');
170 private storeOutputs(OutputActionAttributes: OutputActionAttribute[]) {
172 OutputActionAttributes.forEach(output => {
173 outputs += this.appendOutputAttributes(output);
175 this.writeAttribute(outputs, 'outputs');
178 private appendAttributes(inputActionAttribute: InputActionAttribute) {
179 return '"' + inputActionAttribute.name + '" : {\n' +
180 ' "required" : ' + inputActionAttribute.required + ',\n' +
181 ' "type" : "' + inputActionAttribute.type + '",\n' +
182 ' "description" : "' + inputActionAttribute.description + '"\n' +
186 setInputAndOutputs(targetName) {
187 console.log(targetName);
188 const nodeTemplate = this.designerState.template.node_templates[targetName];
189 console.log(this.designerState.template.node_templates);
190 console.log(nodeTemplate);
191 /* tslint:disable:no-string-literal */
192 console.log(nodeTemplate['type']);
193 this.functions.serverFunctions
194 /* tslint:disable:no-string-literal */
195 .filter(currentFunction => currentFunction.modelName.includes(nodeTemplate['type']))
196 .forEach(currentFunction => {
197 console.log(currentFunction);
198 /* tslint:disable:no-string-literal */
199 if (currentFunction['definition'] && currentFunction['definition']['interfaces']) {
200 const interfaces = Object.keys(currentFunction['definition']['interfaces']);
201 if (interfaces && interfaces.length > 0) {
202 const interfaceName = interfaces[0];
203 console.log(interfaceName);
204 this.currentInterfaceName = interfaceName;
206 if (!this.functionAndAttributesInput.has(targetName)) {
207 this.currentTargetFunctionName = targetName;
208 this.functionAndAttributesInput.set(targetName, []);
211 if (!this.functionAndAttributesOutput.has(targetName)) {
212 this.currentTargetFunctionName = targetName;
213 this.functionAndAttributesOutput.set(targetName, []);
216 if (nodeTemplate['interfaces'] &&
217 nodeTemplate['interfaces'][interfaceName]['operations'] &&
218 nodeTemplate['interfaces'][interfaceName]['operations']['process']
221 if (nodeTemplate['interfaces'][interfaceName]['operations']['process']['inputs']) {
222 /* tslint:disable:no-string-literal */
223 this.suggestedInputs = Object.keys(nodeTemplate['interfaces']
224 [interfaceName]['operations']['process']['inputs']);
226 if (nodeTemplate['interfaces'][interfaceName]['operations']['process']['outputs']) {
227 /* tslint:disable:no-string-literal */
228 this.suggestedOutputs = Object.keys(nodeTemplate['interfaces']
229 [interfaceName]['operations']['process']['outputs']);
230 console.log(this.suggestedInputs);
237 console.log(nodeTemplate);
241 console.log('something');
244 addTempInput(suggestedInput: string) {
245 this.addAttribute(this.tempInputs, suggestedInput);
246 this.deleteAttribute(this.suggestedInputs, suggestedInput);
247 this.addAttribute(this.functionAndAttributesInput.get(this.currentTargetFunctionName), suggestedInput);
250 addTempOutput(suggestedOutput: string) {
251 this.addAttribute(this.tempOutputs, suggestedOutput);
252 this.deleteAttribute(this.suggestedOutputs, suggestedOutput);
253 this.addAttribute(this.functionAndAttributesOutput.get(this.currentTargetFunctionName), suggestedOutput);
256 deleteAttribute(container: string[], suggestedAttribute: string) {
257 if (container && suggestedAttribute && container.includes(suggestedAttribute)) {
258 const index: number = container.indexOf(suggestedAttribute);
260 container.splice(index, 1);
265 addAttribute(container: string[], suggestedAttribute: string) {
266 if (container && suggestedAttribute && !container.includes(suggestedAttribute)) {
267 container.push(suggestedAttribute);
272 submitTempAttributes() {
273 this.writeSelectedAttribute(this.functionAndAttributesInput, 'inputs');
274 this.writeSelectedAttribute(this.functionAndAttributesOutput, 'outputs');
277 private writeSelectedAttribute(map: Map<string, string[]>, attributeType: string) {
278 map.forEach((value, key) => {
279 const nodeTemplate = this.getNodeTemplate(key);
280 this.functions.serverFunctions
281 /* tslint:disable:no-string-literal */
282 .filter(currentFunction => currentFunction.modelName.includes(nodeTemplate['type']))
283 .forEach(currentFunction => {
285 if (currentFunction['definition'] && currentFunction['definition']['interfaces']
286 [Object.keys(currentFunction['definition'] && currentFunction['definition']['interfaces'])]
287 ['operations']['process'][attributeType]) {
288 let newAttributes = '';
289 const attributes = currentFunction['definition'] && currentFunction['definition']['interfaces']
290 [Object.keys(currentFunction['definition'] && currentFunction['definition']['interfaces'])]
291 ['operations']['process'][attributeType];
292 value.forEach(attribute => {
293 newAttributes += '"' + attribute + '": ' + this.convertToString(attributes[attribute]) + ',';
295 if (value.length > 0) {
296 this.writeAttribute(newAttributes, attributeType);
303 private writeAttribute(newAttributes: string, attributeType: string) {
304 newAttributes = this.removeTheLastComma(newAttributes);
305 const originalAttributes = this.convertToString(this.designerState.template.workflows[this.actionName]
307 console.log(originalAttributes.substr(0, originalAttributes.length - 1) + ',' + newAttributes + '}');
308 this.designerState.template.workflows[this.actionName][attributeType] =
309 this.convertToObject(originalAttributes.substr(0, originalAttributes.length - 1)
310 + ',' + newAttributes + '}');
313 private removeTheLastComma = (newInputs: string) => {
314 if (newInputs.endsWith(',')) {
315 newInputs = newInputs.substr(0, newInputs.length - 1);
320 private convertToString = object => JSON.stringify(object);
322 private convertToObject = stringValue => JSON.parse(stringValue);
324 private getNodeTemplate = (value: string) => this.designerState.template.node_templates[value];
326 getAttributesAndOutputs(functionName: string) {
327 this.suggestedAttributes = [];
328 console.log(functionName);
329 const nodeTemplate = this.designerState.template.node_templates[functionName];
330 console.log(this.designerState.template.node_templates);
331 console.log(nodeTemplate);
332 /* tslint:disable:no-string-literal */
333 console.log(nodeTemplate['type']);
334 this.functions.serverFunctions
335 /* tslint:disable:no-string-literal */
336 .filter(currentFunction => currentFunction.modelName.includes(nodeTemplate['type']))
337 .forEach(currentFunction => {
338 if (currentFunction.definition['attributes']) {
339 Object.keys(currentFunction.definition['attributes']).forEach(attribute => {
340 this.suggestedAttributes.push(attribute);
343 console.log(this.suggestedAttributes);
344 this.selectedFunctionName = functionName;
348 addTempOutputAttr(suggestedOutputAndAttribute: string) {
349 this.selectedAttributeName = suggestedOutputAndAttribute;
353 private appendOutputAttributes(output: OutputActionAttribute) {
354 return '"' + output.name + '" : {\n' +
355 ' "required" : ' + output.required + ',\n' +
356 ' "type" : "' + output.type + '",\n' +
357 ' "description" : "' + output.description + '",\n' +
358 ' "value\" :' + '{\n' +
359 ' "get_attribute" : ' + output.value + '\n' +