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;
37 constructor(private designerStore: DesignerStore, private functionsStore: FunctionsStore) {
42 this.designerStore.state$.subscribe(designerState => {
43 this.designerState = designerState;
44 if (this.designerState && this.designerState.actionName) {
45 this.actionName = this.designerState.actionName;
46 const action = this.designerState.template.workflows[this.actionName] as Action;
48 const steps = Object.keys(action.steps);
49 if (steps && steps.length > 0) {
50 this.isFunctionAttributeActive = true;
52 this.isFunctionAttributeActive = false;
55 this.suggestedOutputs = [];
56 this.suggestedInputs = [];
57 /*steps.forEach(step => {
58 const target = action.steps[step].target;
59 this.getInputs(target);
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.inputs.push(insertedInputActionAttribute);
104 addOutput(output: OutputActionAttribute) {
106 if (output && output.type && output.name) {
107 const insertedOutputActionAttribute = Object.assign({}, output);
108 this.outputs.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 this.addOutput(this.outputActionAttribute);
137 this.clearFormInputs();
138 this.designerStore.setInputsAndOutputsToSpecificWorkflow(this.storeInputs(this.inputs)
139 , this.storeOutputs(this.outputs), this.actionName);
142 private clearFormInputs() {
143 this.inputActionAttribute = new InputActionAttribute();
144 this.outputActionAttribute = new OutputActionAttribute();
145 this.outputOtherType = '';
146 this.inputOtherType = '';
149 private storeInputs(InputActionAttributes: InputActionAttribute[]) {
152 InputActionAttributes.forEach(input => {
153 inputs += this.appendAttributes(input);
156 if (inputs.endsWith(',')) {
157 inputs = inputs.substr(0, inputs.length - 1);
159 return JSON.parse('{' + inputs + '}');
162 private storeOutputs(OutputActionAttributes: OutputActionAttribute[]) {
164 OutputActionAttributes.forEach(output => {
165 outputs += this.appendAttributes(output);
167 if (outputs.endsWith(',')) {
168 outputs = outputs.substr(0, outputs.length - 1);
170 return JSON.parse('{' + outputs + '}');
173 private appendAttributes(output: OutputActionAttribute) {
174 return '"' + output.name + '" : {\n' +
175 ' "required" : ' + output.required + ',\n' +
176 ' "type" : "' + output.type + '",\n' +
177 ' "description" : "' + output.description + '"\n' +
181 setInputAndOutputs(targetName) {
182 console.log(targetName);
183 const nodeTemplate = this.designerState.template.node_templates[targetName];
184 console.log(this.designerState.template.node_templates);
185 console.log(nodeTemplate);
186 /* tslint:disable:no-string-literal */
187 console.log(nodeTemplate['type']);
188 this.functions.serverFunctions
189 /* tslint:disable:no-string-literal */
190 .filter(currentFunction => currentFunction.modelName.includes(nodeTemplate['type']))
191 .forEach(currentFunction => {
192 console.log(currentFunction);
193 /* tslint:disable:no-string-literal */
194 if (currentFunction['definition'] && currentFunction['definition']['interfaces']) {
195 const interfaces = Object.keys(currentFunction['definition']['interfaces']);
196 if (interfaces && interfaces.length > 0) {
197 const interfaceName = interfaces[0];
198 console.log(interfaceName);
199 this.currentInterfaceName = interfaceName;
201 if (nodeTemplate['interfaces'] &&
202 nodeTemplate['interfaces'][interfaceName]['operations'] &&
203 nodeTemplate['interfaces'][interfaceName]['operations']['process']
206 if (nodeTemplate['interfaces'][interfaceName]['operations']['process']['inputs']) {
207 /* tslint:disable:no-string-literal */
208 this.suggestedInputs = Object.keys(nodeTemplate['interfaces']
209 [interfaceName]['operations']['process']['inputs']);
210 console.log(this.suggestedInputs);
212 if (nodeTemplate['interfaces'][interfaceName]['operations']['process']['outputs']) {
213 /* tslint:disable:no-string-literal */
214 this.suggestedOutputs = Object.keys(nodeTemplate['interfaces']
215 [interfaceName]['operations']['process']['outputs']);
216 console.log(this.suggestedInputs);
223 console.log(nodeTemplate);
227 console.log('something');
230 addTempInput(suggestedInput: string) {
231 this.addAttribute(this.tempInputs, suggestedInput);
232 this.deleteAttribute(this.suggestedInputs, suggestedInput);
235 addTempOutput(suggestedOutput: string) {
236 this.addAttribute(this.tempOutputs, suggestedOutput);
237 this.deleteAttribute(this.suggestedOutputs, suggestedOutput);
240 deleteAttribute(container: string[], suggestedAttribute: string) {
241 if (container && suggestedAttribute && container.includes(suggestedAttribute)) {
242 const index: number = container.indexOf(suggestedAttribute);
244 container.splice(index, 1);
249 addAttribute(container: string[], suggestedAttribute: string) {
250 if (container && suggestedAttribute && !container.includes(suggestedAttribute)) {
251 container.push(suggestedAttribute);
256 submitTempAttributes() {
257 if (this.tempInputs && this.tempInputs.length > 0) {
259 this.tempInputs.forEach(tempAttribute => {
260 const currentInputNode: string = this.designerState.template.node_templates[this.actionName]['interfaces']
261 [this.currentInterfaceName]['operations']['process']['inputs'][tempAttribute];
262 const currentInputNodeAsString = JSON.stringify(currentInputNode);
263 newInputs += '"' + tempAttribute + '": ' + currentInputNodeAsString + ',';
266 if (newInputs.endsWith(',')) {
267 newInputs = newInputs.substr(0, newInputs.length - 1);
269 const originalInputs = JSON.stringify(this.designerState.template.workflows[this.actionName]['inputs']);
270 console.log(originalInputs.substr(0, originalInputs.length - 1) + ',' + newInputs + '}');
271 this.designerState.template.workflows[this.actionName]['inputs'] =
272 JSON.parse(originalInputs.substr(0, originalInputs.length - 1) + ',' + newInputs + '}');
275 if (this.tempOutputs && this.tempOutputs.length > 0) {
276 this.tempOutputs.forEach(tempAttribute => {
277 const currentOutputNode = this.designerState.template.node_templates[this.actionName]['interfaces']
278 [this.currentInterfaceName]['operations']['process']['outputs'][tempAttribute];
279 console.log(currentOutputNode);