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 {
18 actionAttributesSideBar: boolean;
19 inputActionAttribute = new InputActionAttribute();
20 outputActionAttribute = new OutputActionAttribute();
21 isInputOtherType: boolean;
22 isOutputOtherType: boolean;
26 designerState: DesignerDashboardState;
27 isFunctionAttributeActive = false;
28 functions: FunctionsState;
30 suggestedInputs: string[] = [];
31 suggestedOutputs: string[] = [];
33 tempInputs: string[] = [];
34 tempOutputs: string[] = [];
35 currentInterfaceName: string;
36 functionAndAttributesInput: Map<string, string[]> = new Map<string, string[]>();
37 private currentTargetFunctionName: any;
38 private functionAndAttributesOutput: Map<string, string[]> = new Map<string, string[]>();
40 constructor(private designerStore: DesignerStore, private functionsStore: FunctionsStore) {
45 this.designerStore.state$.subscribe(designerState => {
46 this.designerState = designerState;
47 if (this.designerState && this.designerState.actionName) {
48 this.actionName = this.designerState.actionName;
49 const action = this.designerState.template.workflows[this.actionName] as Action;
51 const steps = Object.keys(action.steps);
52 if (steps && steps.length > 0) {
53 this.isFunctionAttributeActive = true;
55 this.isFunctionAttributeActive = false;
58 this.suggestedOutputs = [];
59 this.suggestedInputs = [];
60 /*steps.forEach(step => {
61 const target = action.steps[step].target;
62 this.getInputs(target);
69 const namesOfInput = Object.keys(action.inputs);
70 this.inputs = this.extractFields(namesOfInput, action.inputs);
74 const namesOfOutput = Object.keys(action.outputs);
75 this.outputs = this.extractFields(namesOfOutput, action.outputs);
80 this.functionsStore.state$.subscribe(functions => {
81 this.functions = functions;
86 private extractFields(namesOfOutput: string[], container: {}) {
88 for (const nameOutput of namesOfOutput) {
89 const fieldAttribute = new OutputActionAttribute();
90 fieldAttribute.name = nameOutput;
91 fieldAttribute.description = container[nameOutput].description;
92 fieldAttribute.required = container[nameOutput].required;
93 fieldAttribute.type = container[nameOutput].type;
94 const insertedOutputActionAttribute = Object.assign({}, fieldAttribute);
95 fields.push(insertedOutputActionAttribute);
100 addInput(input: InputActionAttribute) {
101 if (input && input.type && input.name) {
102 const insertedInputActionAttribute = Object.assign({}, input);
103 this.inputs.push(insertedInputActionAttribute);
107 addOutput(output: OutputActionAttribute) {
109 if (output && output.type && output.name) {
110 const insertedOutputActionAttribute = Object.assign({}, output);
111 this.outputs.push(insertedOutputActionAttribute);
115 setInputType(type: string) {
116 this.inputActionAttribute.type = type;
117 this.isInputOtherType = this.checkIfTypeIsOther(type);
120 setInputRequired(isRequired) {
121 this.inputActionAttribute.required = isRequired;
124 setOutputRequired(isRequired) {
125 this.outputActionAttribute.required = isRequired;
128 setOutputType(type: string) {
129 this.outputActionAttribute.type = type;
130 this.isOutputOtherType = this.checkIfTypeIsOther(type);
133 checkIfTypeIsOther(type) {
134 return type.includes('Other');
138 this.addInput(this.inputActionAttribute);
139 this.addOutput(this.outputActionAttribute);
140 this.clearFormInputs();
141 this.designerStore.setInputsAndOutputsToSpecificWorkflow(this.storeInputs(this.inputs)
142 , this.storeOutputs(this.outputs), this.actionName);
145 private clearFormInputs() {
146 this.inputActionAttribute = new InputActionAttribute();
147 this.outputActionAttribute = new OutputActionAttribute();
148 this.outputOtherType = '';
149 this.inputOtherType = '';
152 private storeInputs(InputActionAttributes: InputActionAttribute[]) {
155 InputActionAttributes.forEach(input => {
156 inputs += this.appendAttributes(input);
159 if (inputs.endsWith(',')) {
160 inputs = inputs.substr(0, inputs.length - 1);
162 return this.convertToObject('{' + inputs + '}');
165 private storeOutputs(OutputActionAttributes: OutputActionAttribute[]) {
167 OutputActionAttributes.forEach(output => {
168 outputs += this.appendAttributes(output);
170 if (outputs.endsWith(',')) {
171 outputs = outputs.substr(0, outputs.length - 1);
173 return this.convertToObject('{' + outputs + '}');
176 private appendAttributes(output: OutputActionAttribute) {
177 return '"' + output.name + '" : {\n' +
178 ' "required" : ' + output.required + ',\n' +
179 ' "type" : "' + output.type + '",\n' +
180 ' "description" : "' + output.description + '"\n' +
184 setInputAndOutputs(targetName) {
185 console.log(targetName);
186 const nodeTemplate = this.designerState.template.node_templates[targetName];
187 console.log(this.designerState.template.node_templates);
188 console.log(nodeTemplate);
189 /* tslint:disable:no-string-literal */
190 console.log(nodeTemplate['type']);
191 this.functions.serverFunctions
192 /* tslint:disable:no-string-literal */
193 .filter(currentFunction => currentFunction.modelName.includes(nodeTemplate['type']))
194 .forEach(currentFunction => {
195 console.log(currentFunction);
196 /* tslint:disable:no-string-literal */
197 if (currentFunction['definition'] && currentFunction['definition']['interfaces']) {
198 const interfaces = Object.keys(currentFunction['definition']['interfaces']);
199 if (interfaces && interfaces.length > 0) {
200 const interfaceName = interfaces[0];
201 console.log(interfaceName);
202 this.currentInterfaceName = interfaceName;
204 if (!this.functionAndAttributesInput.has(targetName)) {
205 this.currentTargetFunctionName = targetName;
206 this.functionAndAttributesInput.set(targetName, []);
209 if (!this.functionAndAttributesOutput.has(targetName)) {
210 this.currentTargetFunctionName = targetName;
211 this.functionAndAttributesOutput.set(targetName, []);
214 if (nodeTemplate['interfaces'] &&
215 nodeTemplate['interfaces'][interfaceName]['operations'] &&
216 nodeTemplate['interfaces'][interfaceName]['operations']['process']
219 if (nodeTemplate['interfaces'][interfaceName]['operations']['process']['inputs']) {
220 /* tslint:disable:no-string-literal */
221 this.suggestedInputs = Object.keys(nodeTemplate['interfaces']
222 [interfaceName]['operations']['process']['inputs']);
224 if (nodeTemplate['interfaces'][interfaceName]['operations']['process']['outputs']) {
225 /* tslint:disable:no-string-literal */
226 this.suggestedOutputs = Object.keys(nodeTemplate['interfaces']
227 [interfaceName]['operations']['process']['outputs']);
228 console.log(this.suggestedInputs);
235 console.log(nodeTemplate);
239 console.log('something');
242 addTempInput(suggestedInput: string) {
243 this.addAttribute(this.tempInputs, suggestedInput);
244 this.deleteAttribute(this.suggestedInputs, suggestedInput);
245 this.addAttribute(this.functionAndAttributesInput.get(this.currentTargetFunctionName), suggestedInput);
248 addTempOutput(suggestedOutput: string) {
249 this.addAttribute(this.tempOutputs, suggestedOutput);
250 this.deleteAttribute(this.suggestedOutputs, suggestedOutput);
251 this.addAttribute(this.functionAndAttributesOutput.get(this.currentTargetFunctionName), suggestedOutput);
254 deleteAttribute(container: string[], suggestedAttribute: string) {
255 if (container && suggestedAttribute && container.includes(suggestedAttribute)) {
256 const index: number = container.indexOf(suggestedAttribute);
258 container.splice(index, 1);
263 addAttribute(container: string[], suggestedAttribute: string) {
264 if (container && suggestedAttribute && !container.includes(suggestedAttribute)) {
265 container.push(suggestedAttribute);
270 submitTempAttributes() {
271 this.writeSelectedAttributeInputs();
272 this.writeSelectedAttributeOutputs();
275 private writeSelectedAttributeOutputs() {
276 this.functionAndAttributesOutput.forEach((key, value) => {
277 const nodeTemplate = this.getNodeTemplate(value);
278 this.functions.serverFunctions
279 /* tslint:disable:no-string-literal */
280 .filter(currentFunction => currentFunction.modelName.includes(nodeTemplate['type']))
281 .forEach(currentFunction => {
282 if (currentFunction['definition'] && currentFunction['definition']['interfaces']
283 [Object.keys(currentFunction['definition'] && currentFunction['definition']['interfaces'])]
284 ['operations']['process']['outputs']) {
286 const outputs = currentFunction['definition'] && currentFunction['definition']['interfaces']
287 [Object.keys(currentFunction['definition'] && currentFunction['definition']['interfaces'])]
288 ['operations']['process']['outputs'];
289 key.forEach(attribute => {
290 newOutputs += '"' + attribute + '": ' + this.convertToString(outputs[attribute]) + ',';
292 if (key.length > 0) {
293 newOutputs = this.removeTheLastComma(newOutputs);
294 const originalOutputs = this.convertToString(this.designerState.template.workflows[this.actionName]['outputs']);
295 console.log(originalOutputs.substr(0, originalOutputs.length - 1) + ',' + newOutputs + '}');
296 this.designerState.template.workflows[this.actionName]['outputs'] =
297 this.convertToObject(originalOutputs.substr(0, originalOutputs.length - 1) + ',' + newOutputs + '}');
305 private writeSelectedAttributeInputs() {
306 this.functionAndAttributesInput.forEach((key, value) => {
307 const nodeTemplate = this.getNodeTemplate(value);
308 this.functions.serverFunctions
309 /* tslint:disable:no-string-literal */
310 .filter(currentFunction => currentFunction.modelName.includes(nodeTemplate['type']))
311 .forEach(currentFunction => {
312 if (currentFunction['definition'] && currentFunction['definition']['interfaces']
313 [Object.keys(currentFunction['definition'] && currentFunction['definition']['interfaces'])]
314 ['operations']['process']['inputs']) {
316 const inputs = currentFunction['definition'] && currentFunction['definition']['interfaces']
317 [Object.keys(currentFunction['definition'] && currentFunction['definition']['interfaces'])]
318 ['operations']['process']['inputs'];
319 key.forEach(attribute => {
320 newInputs += '"' + attribute + '": ' + this.convertToString(inputs[attribute]) + ',';
322 if (key.length > 0) {
323 newInputs = this.removeTheLastComma(newInputs);
324 const originalInputs = this.convertToString(this.designerState.template.workflows[this.actionName]['inputs']);
325 console.log(originalInputs.substr(0, originalInputs.length - 1) + ',' + newInputs + '}');
326 this.designerState.template.workflows[this.actionName]['inputs'] =
327 this.convertToObject(originalInputs.substr(0, originalInputs.length - 1) + ',' + newInputs + '}');
336 private removeTheLastComma = (newInputs: string) => {
337 if (newInputs.endsWith(',')) {
338 newInputs = newInputs.substr(0, newInputs.length - 1);
343 private convertToString = object => JSON.stringify(object);
345 private convertToObject = stringValue => JSON.parse(stringValue);
347 private getNodeTemplate = (value: string) => this.designerState.template.node_templates[value];