[sdc] rebase 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 {COMPONENT_FIELDS} from "../../../../utils/constants";
26 import {ComponentGenericResponse} from "../../../../ng2/services/responses/component-generic-response";
27 import {ComponentServiceNg2} from "../../../../ng2/services/component-services/component.service";
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     ];
48
49
50     constructor(private $scope:IPropertiesViewModelScope,
51                 private $filter:ng.IFilterService,
52                 private ModalsHandler:ModalsHandler,
53                 private ComponentServiceNg2:ComponentServiceNg2) {
54         this.initComponentProperties();
55         this.$scope.updateSelectedMenuItem();
56     }
57
58     private initComponentProperties = ():void => {
59
60         if(!this.$scope.component.properties) {
61             this.$scope.isLoading = true;
62             this.ComponentServiceNg2.getComponentProperties(this.$scope.component).subscribe((response:ComponentGenericResponse) => {
63                 this.$scope.component.properties = response.properties;
64                 this.initScope();
65                 this.$scope.isLoading = false;
66             }, () => {
67                 this.$scope.isLoading = false;
68             });
69         } else {
70             this.initScope();
71         }
72     }
73
74     private openEditPropertyModal = (property:PropertyModel):void => {
75         this.ModalsHandler.openEditPropertyModal(property, this.$scope.component, this.$scope.filteredProperties, false).then(() => {
76         });
77     };
78
79     private initScope = ():void => {
80
81         //let self = this;
82         this.$scope.filteredProperties = this.$scope.component.properties;
83         this.$scope.sortBy = 'name';
84         this.$scope.reverse = false;
85         this.$scope.setValidState(true);
86         this.$scope.tableHeadersList = [
87             {title: 'Name', property: 'name'},
88             {title: 'Type', property: 'type'},
89             {title: 'Schema', property: 'schema.property.type'},
90             {title: 'Description', property: 'description'},
91         ];
92         this.$scope.sort = (sortBy:string):void => {
93             this.$scope.reverse = (this.$scope.sortBy === sortBy) ? !this.$scope.reverse : false;
94             this.$scope.sortBy = sortBy;
95         };
96
97
98         this.$scope.addOrUpdateProperty = (property?:PropertyModel):void => {
99             this.openEditPropertyModal(property ? property : new PropertyModel());
100         };
101
102         this.$scope.delete = (property:PropertyModel):void => {
103
104             let onOk = ():void => {
105                 this.$scope.component.deleteProperty(property.uniqueId);
106             };
107             let title:string = this.$filter('translate')("PROPERTY_VIEW_DELETE_MODAL_TITLE");
108             let message:string = this.$filter('translate')("PROPERTY_VIEW_DELETE_MODAL_TEXT", "{'name': '" + property.name + "'}");
109             this.ModalsHandler.openConfirmationModal(title, message, false).then(onOk);
110         };
111     }
112 }