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 console.log(this.actionName);
50 const action = this.designerState.template.workflows[this.actionName] as Action;
52 const steps = Object.keys(action.steps);
53 this.isFunctionAttributeActive = steps && steps.length > 0;
55 this.suggestedOutputs = [];
56 this.suggestedInputs = [];
61 const namesOfInput = Object.keys(action.inputs);
62 this.inputs = this.extractFields(namesOfInput, action.inputs);
66 const namesOfOutput = Object.keys(action.outputs);
67 this.outputs = this.extractFields(namesOfOutput, action.outputs);
72 this.functionsStore.state$.subscribe(functions => {
73 this.functions = functions;
78 private extractFields(namesOfOutput: string[], container: {}) {
80 for (const nameOutput of namesOfOutput) {
81 const fieldAttribute = new OutputActionAttribute();
82 fieldAttribute.name = nameOutput;
83 fieldAttribute.description = container[nameOutput].description;
84 fieldAttribute.required = container[nameOutput].required;
85 fieldAttribute.type = container[nameOutput].type;
86 const insertedOutputActionAttribute = Object.assign({}, fieldAttribute);
87 fields.push(insertedOutputActionAttribute);
92 addInput(input: InputActionAttribute) {
93 if (input && input.type && input.name) {
94 const insertedInputActionAttribute = Object.assign({}, input);
95 this.inputs.push(insertedInputActionAttribute);
99 addOutput(output: OutputActionAttribute) {
101 if (output && output.type && output.name) {
102 const insertedOutputActionAttribute = Object.assign({}, output);
103 this.outputs.push(insertedOutputActionAttribute);
107 setInputType(type: string) {
108 this.inputActionAttribute.type = type;
109 this.isInputOtherType = this.checkIfTypeIsOther(type);
112 setInputRequired(isRequired) {
113 this.inputActionAttribute.required = isRequired;
116 setOutputRequired(isRequired) {
117 this.outputActionAttribute.required = isRequired;
120 setOutputType(type: string) {
121 this.outputActionAttribute.type = type;
122 this.isOutputOtherType = this.checkIfTypeIsOther(type);
125 checkIfTypeIsOther(type) {
126 return type.includes('Other');
130 this.addInput(this.inputActionAttribute);
131 this.addOutput(this.outputActionAttribute);
132 this.clearFormInputs();
133 this.designerStore.setInputsAndOutputsToSpecificWorkflow(this.storeInputs(this.inputs)
134 , this.storeOutputs(this.outputs), this.actionName);
137 private clearFormInputs() {
138 this.inputActionAttribute = new InputActionAttribute();
139 this.outputActionAttribute = new OutputActionAttribute();
140 this.outputOtherType = '';
141 this.inputOtherType = '';
144 private storeInputs(InputActionAttributes: InputActionAttribute[]) {
147 InputActionAttributes.forEach(input => {
148 inputs += this.appendAttributes(input);
151 if (inputs.endsWith(',')) {
152 inputs = inputs.substr(0, inputs.length - 1);
154 return this.convertToObject('{' + inputs + '}');
157 private storeOutputs(OutputActionAttributes: OutputActionAttribute[]) {
159 OutputActionAttributes.forEach(output => {
160 outputs += this.appendAttributes(output);
162 if (outputs.endsWith(',')) {
163 outputs = outputs.substr(0, outputs.length - 1);
165 return this.convertToObject('{' + outputs + '}');
168 private appendAttributes(output: OutputActionAttribute) {
169 return '"' + output.name + '" : {\n' +
170 ' "required" : ' + output.required + ',\n' +
171 ' "type" : "' + output.type + '",\n' +
172 ' "description" : "' + output.description + '"\n' +
176 setInputAndOutputs(targetName) {
177 console.log(targetName);
178 const nodeTemplate = this.designerState.template.node_templates[targetName];
179 console.log(this.designerState.template.node_templates);
180 console.log(nodeTemplate);
181 /* tslint:disable:no-string-literal */
182 console.log(nodeTemplate['type']);
183 this.functions.serverFunctions
184 /* tslint:disable:no-string-literal */
185 .filter(currentFunction => currentFunction.modelName.includes(nodeTemplate['type']))
186 .forEach(currentFunction => {
187 console.log(currentFunction);
188 /* tslint:disable:no-string-literal */
189 if (currentFunction['definition'] && currentFunction['definition']['interfaces']) {
190 const interfaces = Object.keys(currentFunction['definition']['interfaces']);
191 if (interfaces && interfaces.length > 0) {
192 const interfaceName = interfaces[0];
193 console.log(interfaceName);
194 this.currentInterfaceName = interfaceName;
196 if (!this.functionAndAttributesInput.has(targetName)) {
197 this.currentTargetFunctionName = targetName;
198 this.functionAndAttributesInput.set(targetName, []);
201 if (!this.functionAndAttributesOutput.has(targetName)) {
202 this.currentTargetFunctionName = targetName;
203 this.functionAndAttributesOutput.set(targetName, []);
206 if (nodeTemplate['interfaces'] &&
207 nodeTemplate['interfaces'][interfaceName]['operations'] &&
208 nodeTemplate['interfaces'][interfaceName]['operations']['process']
211 if (nodeTemplate['interfaces'][interfaceName]['operations']['process']['inputs']) {
212 /* tslint:disable:no-string-literal */
213 this.suggestedInputs = Object.keys(nodeTemplate['interfaces']
214 [interfaceName]['operations']['process']['inputs']);
216 if (nodeTemplate['interfaces'][interfaceName]['operations']['process']['outputs']) {
217 /* tslint:disable:no-string-literal */
218 this.suggestedOutputs = Object.keys(nodeTemplate['interfaces']
219 [interfaceName]['operations']['process']['outputs']);
220 console.log(this.suggestedInputs);
227 console.log(nodeTemplate);
231 console.log('something');
234 addTempInput(suggestedInput: string) {
235 this.addAttribute(this.tempInputs, suggestedInput);
236 this.deleteAttribute(this.suggestedInputs, suggestedInput);
237 this.addAttribute(this.functionAndAttributesInput.get(this.currentTargetFunctionName), suggestedInput);
240 addTempOutput(suggestedOutput: string) {
241 this.addAttribute(this.tempOutputs, suggestedOutput);
242 this.deleteAttribute(this.suggestedOutputs, suggestedOutput);
243 this.addAttribute(this.functionAndAttributesOutput.get(this.currentTargetFunctionName), suggestedOutput);
246 deleteAttribute(container: string[], suggestedAttribute: string) {
247 if (container && suggestedAttribute && container.includes(suggestedAttribute)) {
248 const index: number = container.indexOf(suggestedAttribute);
250 container.splice(index, 1);
255 addAttribute(container: string[], suggestedAttribute: string) {
256 if (container && suggestedAttribute && !container.includes(suggestedAttribute)) {
257 container.push(suggestedAttribute);
262 submitTempAttributes() {
263 this.writeSelectedAttribute(this.functionAndAttributesInput, 'inputs');
264 this.writeSelectedAttribute(this.functionAndAttributesOutput, 'outputs');
267 private writeSelectedAttribute(map: Map<string, string[]>, attributeType: string) {
268 map.forEach((value, key) => {
269 const nodeTemplate = this.getNodeTemplate(key);
270 this.functions.serverFunctions
271 /* tslint:disable:no-string-literal */
272 .filter(currentFunction => currentFunction.modelName.includes(nodeTemplate['type']))
273 .forEach(currentFunction => {
275 if (currentFunction['definition'] && currentFunction['definition']['interfaces']
276 [Object.keys(currentFunction['definition'] && currentFunction['definition']['interfaces'])]
277 ['operations']['process'][attributeType]) {
278 let newAttributes = '';
279 const attributes = currentFunction['definition'] && currentFunction['definition']['interfaces']
280 [Object.keys(currentFunction['definition'] && currentFunction['definition']['interfaces'])]
281 ['operations']['process'][attributeType];
282 value.forEach(attribute => {
283 newAttributes += '"' + attribute + '": ' + this.convertToString(attributes[attribute]) + ',';
285 if (value.length > 0) {
286 newAttributes = this.removeTheLastComma(newAttributes);
287 const originalAttributes = this.convertToString(this.designerState.template.workflows[this.actionName]
289 console.log(originalAttributes.substr(0, originalAttributes.length - 1) + ',' + newAttributes + '}');
290 this.designerState.template.workflows[this.actionName][attributeType] =
291 this.convertToObject(originalAttributes.substr(0, originalAttributes.length - 1)
292 + ',' + newAttributes + '}');
299 private removeTheLastComma = (newInputs: string) => {
300 if (newInputs.endsWith(',')) {
301 newInputs = newInputs.substr(0, newInputs.length - 1);
306 private convertToString = object => JSON.stringify(object);
308 private convertToObject = stringValue => JSON.parse(stringValue);
310 private getNodeTemplate = (value: string) => this.designerState.template.node_templates[value];