[SDC-31] add mising script got Comformance fix
[sdc.git] / catalog-ui / src / app / ng2 / components / properties-table / list-property / list-property.component.ts
1 /**
2  * Created by rc2122 on 4/23/2017.
3  */
4 import {Component, Input, Output, EventEmitter} from "@angular/core";
5 import { PropertyFEModel} from "app/models";
6 import {PropertiesService} from "app/ng2/services/properties.service";
7 import { ContentAfterLastDotPipe } from "app/ng2/pipes/contentAfterLastDot.pipe";
8 import {UUID} from "angular2-uuid";
9 import {ComponentType} from "app/utils";
10
11 @Component({
12     selector: 'list-property',
13     templateUrl: './list-property.component.html',
14     styleUrls: ['../properties-value-inner-table/properties-value-inner-table.component.less', './list-property.component.less']
15 })
16 export class ListPropertyComponent {
17
18     @Input() property: PropertyFEModel;
19     @Input() selectedPropertyId: string;
20     @Input() propertyNameSearchText:string;
21     
22     @Output() valueChanged: EventEmitter<any> = new EventEmitter<any>();
23     @Output() selectChildProperty: EventEmitter<any> = new EventEmitter<PropertyFEModel>();
24
25     constructor ( private propertiesService:PropertiesService, private contentAfterLastDotPipe:ContentAfterLastDotPipe ){
26     }
27
28     propValueChanged = () => {
29         this.valueChanged.emit(this.property);
30     };
31
32     onChildPropertySelected = (property) => {
33         this.selectChildProperty.emit(property);
34     };
35
36     getNumber = (valueObjectRef: any): Array<any> => {
37         let num: number = (valueObjectRef) ? valueObjectRef.length : 0;
38         return new Array(num);
39     }
40
41     createNewChildProperty = ():void => {
42         let newProperty: PropertyFEModel = new PropertyFEModel(this.contentAfterLastDotPipe.transform(this.property.schema.property.type),
43             this.property.schema.property.type,
44             UUID.UUID(),
45             this.property,
46             this.property.valueObjectRef[this.property.childrenProperties.length]
47         );
48         this.propertiesService.createPropertiesTreeForProp(newProperty);
49         this.property.childrenProperties.push(newProperty);
50     }
51
52     addListItem = ():void => {
53         this.property.valueObjectRef = this.property.valueObjectRef || [];
54         this.property.childrenProperties = this.property.childrenProperties || [];
55         if (this.property.schema.property.isSimpleType){
56             if( this.property.valueObjectRef.indexOf("") == -1 ) {//prevent insert multiple empty simple type items to list
57                 this.property.valueObjectRef.push("");
58             }
59         }else{
60             this.property.valueObjectRef[this.property.childrenProperties.length] = {};
61             this.property.childrenProperties = this.property.childrenProperties || [];
62             this.createNewChildProperty();
63             this.valueChanged.emit(this.property);
64         }
65     }
66
67     deleteListItem = (indexInList:number):void => {
68         this.property.valueObjectRef.splice(indexInList, 1);
69         if(this.property.childrenProperties){
70             this.property.childrenProperties.splice(indexInList, 1);
71         }
72         if (!this.property.valueObjectRef.length) {//only when user removes all items from list - put the default
73             if ( this.property.defaultValue ) {
74                 angular.copy(JSON.parse(this.property.defaultValue), this.property.valueObjectRef);
75                 if (this.property.schema.property.isDataType){
76                     _.forEach(this.property.valueObjectRef, () => {
77                         this.createNewChildProperty();
78                     });
79                 }
80             }
81         }
82         this.valueChanged.emit(this.property);
83     }
84
85 }