blob: 47b99dfffd0dbc50821a7ffdb9d864f8ebc0d29e [file] [log] [blame]
Michael Landoed64b5e2017-06-09 03:19:04 +03001/**
2 * Created by obarda on 9/12/2016.
3 */
4'use strict';
5import {InputPropertyBase} from "./input-property-base";
6import {PropertyModel} from "./properties";
7import {InputModel} from "./inputs";
8
9export class InstancesInputsOrPropertiesMapData {
10 [instanceId:string]:Array<InputPropertyBase>;
11}
12
13export class InstancesInputsPropertiesMap {
14 componentInstanceProperties:InstancesInputsOrPropertiesMapData;
15 componentInstanceInputsMap:InstancesInputsOrPropertiesMapData;
16
17 constructor(componentInstanceInputsMapData:InstancesInputsOrPropertiesMapData, componentInstanceInputsPropertiesMapData:InstancesInputsOrPropertiesMapData) {
18 this.componentInstanceInputsMap = componentInstanceInputsMapData;
19 this.componentInstanceProperties = componentInstanceInputsPropertiesMapData;
20 }
21
22 private removeUnnecessaryData = (properties:Array<InputPropertyBase>, instanceId:string, mapData:any) => {
23 mapData[instanceId] = [];
24 if (properties && properties.length > 0) {
25 _.forEach(properties, (propertyOrInput:InputPropertyBase) => {
26 if (propertyOrInput instanceof PropertyModel) { // Handle Properties
27 if (propertyOrInput && !propertyOrInput.isAlreadySelected) {
28 mapData[instanceId].push(propertyOrInput);
29 }
30 }
31 if (propertyOrInput instanceof InputModel) { // Handle Inputs
32 if (propertyOrInput && !propertyOrInput.isAlreadySelected) {
33 mapData[instanceId].push(propertyOrInput);
34 }
35 }
36 });
37 if (mapData[instanceId].length === 0) {
38 delete mapData[instanceId];
39 }
40 } else {
41 delete mapData[instanceId];
42 }
43 }
44
45 /*
46 In the toJson we remove all inputs and property already selected (The check box selected but they are disable)
47 also we remove empty array in order to prevent Backend error
48 */
49
50 public cleanUnnecessaryDataBeforeSending = ():InstancesInputsPropertiesMap => {
51
52 let map:InstancesInputsPropertiesMap = new InstancesInputsPropertiesMap(new InstancesInputsOrPropertiesMapData(), new InstancesInputsOrPropertiesMapData());
53 angular.copy(this, map);
54
55 //Removing unnecessary data from inputs map
56 _.forEach(map.componentInstanceInputsMap, (inputs:Array<InputModel>, instanceId:string) => {
57 this.removeUnnecessaryData(inputs, instanceId, map.componentInstanceInputsMap);
58 });
59
60 //Removing unnecessary data from properties map
61 _.forEach(map.componentInstanceProperties, (properties:Array<PropertyModel>, instanceId:string) => {
62 this.removeUnnecessaryData(properties, instanceId, map.componentInstanceProperties);
63 });
64
65 return map;
66 };
67}