Change to enable SDC list type input
[sdc.git] / catalog-ui / src / app / ng2 / components / logic / properties-table / properties-table.component.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2017 Huawei Intellectual Property. All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 import { Component, Input, Output, EventEmitter} from "@angular/core";
23 import {PropertyFEModel, DerivedFEProperty, InstanceFePropertiesMap} from "app/models";
24 import {PropertiesService} from "../../../services/properties.service";
25 import {ModalService} from "../../../services/modal.service";
26 import { InstanceFeDetails } from "../../../../models/instance-fe-details";
27
28 @Component({
29     selector: 'properties-table',
30     templateUrl: './properties-table.component.html',
31     styleUrls: ['./properties-table.component.less']
32 })
33 export class PropertiesTableComponent {
34
35     @Input() fePropertiesMap: InstanceFePropertiesMap;
36     @Input() feInstanceNamesMap: Map<string, InstanceFeDetails>;
37     @Input() selectedPropertyId: string;
38     @Input() propertyNameSearchText:string;
39     @Input() searchTerm:string;
40     @Input() readonly:boolean;
41     @Input() isLoading:boolean;
42     @Input() hasDeclareOption:boolean;
43     @Input() hidePropertyType:boolean;
44     @Input() showDelete:boolean;
45     
46     @Output('propertyChanged') emitter: EventEmitter<PropertyFEModel> = new EventEmitter<PropertyFEModel>();
47     @Output() selectPropertyRow: EventEmitter<PropertyRowSelectedEvent> = new EventEmitter<PropertyRowSelectedEvent>();
48     @Output() updateCheckedPropertyCount: EventEmitter<boolean> = new EventEmitter<boolean>();//only for hasDeclareOption and hasDeclareListOption
49     @Output() updateCheckedChildPropertyCount: EventEmitter<boolean> = new EventEmitter<boolean>();//only for hasDeclareListOption
50     @Output() deleteProperty: EventEmitter<PropertyFEModel> = new EventEmitter<PropertyFEModel>();
51     private selectedPropertyToDelete: PropertyFEModel;
52
53     sortBy: String;
54     reverse: boolean;
55     direction: number;
56     path:string[];
57
58     sort(sortBy){
59         this.reverse = (this.sortBy === sortBy) ? !this.reverse : true;
60         this.direction = this.reverse ? 1 : -1;
61         this.sortBy = sortBy;
62         this.path = sortBy.split('.');
63     }
64
65     constructor (private propertiesService:PropertiesService, private modalService: ModalService){
66     }
67     
68     ngOnInit() {
69     }
70
71     onPropertyChanged = (property) => {
72         this.emitter.emit(property);
73     };
74
75     // Click on main row (row of propertyFEModel)
76     onClickPropertyRow = (property:PropertyFEModel, instanceName:string, event?) => {
77         //event && event.stopPropagation();
78         this.selectedPropertyId = property.name;
79         let propertyRowSelectedEvent:PropertyRowSelectedEvent = new PropertyRowSelectedEvent(property, instanceName);
80         this.selectPropertyRow.emit(propertyRowSelectedEvent);
81     };
82
83     // Click on inner row (row of DerivedFEProperty)
84     onClickPropertyInnerRow = (property:DerivedFEProperty, instanceName:string) => {
85         let propertyRowSelectedEvent:PropertyRowSelectedEvent = new PropertyRowSelectedEvent(property, instanceName);
86         this.selectPropertyRow.emit(propertyRowSelectedEvent);
87     }
88
89     propertyChecked = (prop: PropertyFEModel, childPropName?: string) => {
90         let isChecked: boolean = (!childPropName)? prop.isSelected : prop.flattenedChildren.find(prop => prop.propertiesName == childPropName).isSelected;
91
92         if (!isChecked) {
93             this.propertiesService.undoDisableRelatedProperties(prop, childPropName);
94         } else {
95             this.propertiesService.disableRelatedProperties(prop, childPropName);
96         }
97         this.updateCheckedPropertyCount.emit(isChecked);
98
99         if (childPropName) {
100             let isCount: boolean = (isChecked)? true : false ;
101             this.updateCheckedChildPropertyCount.emit(isCount);
102         }
103     }
104
105     onDeleteProperty = () => {
106         this.deleteProperty.emit(this.selectedPropertyToDelete);
107         this.modalService.closeCurrentModal();
108     };
109
110     openDeleteModal = (property:PropertyFEModel) => {
111         this.selectedPropertyToDelete = property;
112         this.modalService.createActionModal("Delete Property", "Are you sure you want to delete this property?",
113             "Delete", this.onDeleteProperty, "Close").instance.open();
114     }
115
116 }
117
118 export class PropertyRowSelectedEvent {
119     propertyModel:PropertyFEModel | DerivedFEProperty;
120     instanceName:string;
121     constructor ( propertyModel:PropertyFEModel | DerivedFEProperty, instanceName:string ){
122         this.propertyModel = propertyModel;
123         this.instanceName = instanceName;
124     }
125 }
126