Fix VFC map or list property update
[sdc.git] / catalog-ui / src / app / view-models / workspace / tabs / properties / properties-view-model.ts
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 'use strict';
22 import { IWorkspaceViewModelScope } from "app/view-models/workspace/workspace-view-model";
23 import { PropertyModel } from "app/models";
24 import { ModalsHandler } from "app/utils";
25 import { ComponentGenericResponse } from "../../../../ng2/services/responses/component-generic-response";
26 import { ComponentServiceNg2 } from "../../../../ng2/services/component-services/component.service";
27 import { SdcUiCommon, SdcUiServices, SdcUiComponents } from "onap-ui-angular";
28
29 interface IPropertiesViewModelScope extends IWorkspaceViewModelScope {
30     tableHeadersList:Array<any>;
31     reverse:boolean;
32     sortBy:string;
33     filteredProperties:Array<PropertyModel>;
34
35     addOrUpdateProperty(property?:PropertyModel):void;
36     delete(property:PropertyModel):void;
37     sort(sortBy:string):void;
38 }
39
40 export class PropertiesViewModel {
41
42     static '$inject' = [
43         '$scope',
44         '$filter',
45         'ModalsHandler',
46         'ComponentServiceNg2',
47         'ModalServiceSdcUI'
48     ];
49
50
51     constructor(private $scope:IPropertiesViewModelScope,
52                 private $filter:ng.IFilterService,
53                 private ModalsHandler:ModalsHandler,
54                 private ComponentServiceNg2:ComponentServiceNg2,
55                 private modalService: SdcUiServices.ModalService) {
56         this.initComponentProperties();
57     }
58
59     private initComponentProperties = ():void => {
60
61         if(!this.$scope.component.properties) {
62             this.$scope.isLoading = true;
63             this.ComponentServiceNg2.getComponentProperties(this.$scope.component).subscribe((response:ComponentGenericResponse) => {
64                 this.$scope.component.properties = response.properties;
65                 this.initScope();
66                 this.$scope.isLoading = false;
67             }, () => {
68                 this.$scope.isLoading = false;
69             });
70         } else {
71             this.initScope();
72         }
73     }
74
75     private openEditPropertyModal = (property:PropertyModel):void => {
76         this.ModalsHandler.openEditPropertyModal(property, this.$scope.component, this.$scope.filteredProperties, false, 'component', this.$scope.component.uniqueId);
77     };
78
79     private initScope = ():void => {
80         this.$scope.filteredProperties = this.$scope.component.properties;
81         this.$scope.sortBy = 'name';
82         this.$scope.reverse = false;
83         this.$scope.setValidState(true);
84         this.$scope.tableHeadersList = [
85             {title: 'Name', property: 'name'},
86             {title: 'Type', property: 'type'},
87             {title: 'Schema', property: 'schema.property.type'},
88             {title: 'Description', property: 'description'},
89         ];
90         this.$scope.sort = (sortBy:string):void => {
91             this.$scope.reverse = (this.$scope.sortBy === sortBy) ? !this.$scope.reverse : false;
92             this.$scope.sortBy = sortBy;
93         };
94
95
96         this.$scope.addOrUpdateProperty = (property?:PropertyModel):void => {
97             this.openEditPropertyModal(property ? property : new PropertyModel());
98         };
99
100         this.$scope.delete = (property:PropertyModel):void => {
101
102             let onOk: Function = ():void => {
103                 this.$scope.component.deleteProperty(property.uniqueId);
104             };
105             let title:string = this.$filter('translate')("PROPERTY_VIEW_DELETE_MODAL_TITLE");
106             let message:string = this.$filter('translate')("PROPERTY_VIEW_DELETE_MODAL_TEXT", "{'name': '" + property.name + "'}");
107             const okButton = {testId: "OK", text: "OK", type: SdcUiCommon.ButtonType.info, callback: onOk, closeModal: true} as SdcUiComponents.ModalButtonComponent;
108             this.modalService.openInfoModal(title, message, 'delete-modal', [okButton]);
109         };
110     }
111 }